-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (82 loc) · 1.87 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"log"
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/julienschmidt/httprouter"
"net/http"
"ldh/Cors"
"ldh/BasicAuth"
"ldh/Models"
"encoding/json"
"ldh/Mdb"
"os"
)
var configuration interface{}
var Version = "5.0.0.0"
func main() {
fmt.Printf("Coolpy Version: %s\n", Version)
if _, err := os.Stat("conf.json"); err != nil {
log.Fatal("Config file is missing: ", "conf.json")
os.Exit(1)
}
file, _ := os.Open("conf.json")
decoder := json.NewDecoder(file)
if err := decoder.Decode(&configuration); err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
conf, ok := configuration.(map[string]interface{})
if ok {
for k, v := range conf {
switch v2 := v.(type) {
case string:
if k == "mongo" {
Mdb.DatabaseAddress = v2
}
default:
fmt.Println(k, v)
}
}
}
router := httprouter.New()
router.GET("/", Index)
router.POST("/", IndexPost)
router.GET("/hello/:name", Basicauth.Auth(Hello))
if err := http.ListenAndServe(":8080", Cors.CORS(router)); err != nil {
log.Fatal(err)
}
}
func IndexPost(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
decoder := json.NewDecoder(r.Body)
var p Models.Person
err := decoder.Decode(&p)
if err != nil {
w.WriteHeader(http.StatusNotFound)
log.Fatal(err)
return
}
Mdb.M("people", func(c *mgo.Collection) {
err = c.Insert(p)
if err != nil {
w.WriteHeader(http.StatusNotFound)
log.Fatal(err)
return
}
json.NewEncoder(w).Encode(p)
})
}
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
Mdb.M("people", func(c *mgo.Collection) {
result := []Models.Person{}
err := c.Find(bson.M{"name": "Ale"}).All(&result)
if err != nil {
log.Fatal(err)
}
json.NewEncoder(w).Encode(result)
})
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}