Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.33 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "github.com/zebresel-com/mongodm"
  6.     "gopkg.in/mgo.v2/bson"
  7.     "html/template"
  8.     "io/ioutil"
  9.     "log"
  10.     "net/http"
  11. )
  12.  
  13. type User struct {
  14.     mongodm.DocumentBase `json:",inline" bson:",inline"`
  15.  
  16.     Username     string `json:"username" bson:"username"`
  17.     PasswordHash string `json:"passwordHash" bson:"passwordHash"`
  18. }
  19.  
  20. var UserModel *mongodm.Model
  21.  
  22. func index(w http.ResponseWriter, r *http.Request) {
  23.     t, _ := template.ParseFiles("index.html")
  24.     t.Execute(w, nil)
  25. }
  26.  
  27. func auth(w http.ResponseWriter, r *http.Request) {
  28.     user := &User{}
  29.     UserModel.FindOne(bson.M{"username": "admin", "passwordHash": "something"}).Exec(user)
  30.     log.Printf("%+v\n", user)
  31. }
  32.  
  33. func main() {
  34.     file, err := ioutil.ReadFile("locals.json")
  35.  
  36.     if err != nil {
  37.         log.Panicf("File error: %v\n", err)
  38.     }
  39.  
  40.     var localMap map[string]map[string]string
  41.     json.Unmarshal(file, &localMap)
  42.  
  43.     dbConfig := &mongodm.Config{
  44.         DatabaseHost: "127.0.0.1",
  45.         DatabaseName: "tester",
  46.         Locals:       localMap["en-US"],
  47.     }
  48.  
  49.     connection, err := mongodm.Connect(dbConfig)
  50.     if err != nil {
  51.         log.Panic("Database connection error: %v", err)
  52.     }
  53.  
  54.     connection.Register(&User{}, "users")
  55.     UserModel = connection.Model("User")
  56.  
  57.     http.HandleFunc("/", index)
  58.     http.HandleFunc("/auth", auth)
  59.  
  60.     log.Fatal(http.ListenAndServe(":3000", nil))
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement