-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
101 lines (87 loc) · 3 KB
/
server.go
File metadata and controls
101 lines (87 loc) · 3 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"flag"
"net/http"
"net/http/httputil"
"github.com/golang/glog"
"github.com/gorilla/mux"
"github.com/pruh/api/config"
apihttp "github.com/pruh/api/http"
"github.com/pruh/api/http/middleware"
"github.com/pruh/api/messages"
"github.com/pruh/api/mongo"
"github.com/pruh/api/networks"
"github.com/pruh/api/notifications"
"github.com/pruh/api/providers"
"github.com/urfave/negroni"
)
func main() {
flag.Parse()
err := flag.Lookup("logtostderr").Value.Set("true")
if err != nil {
glog.Warningf("Cannot set a flag. %s", err)
}
config, err := config.NewFromEnv()
if err != nil {
panic(err)
}
apiV1Path := "/api/v1"
router := mux.NewRouter().StrictSlash(false)
apiV1Router := mux.NewRouter().PathPrefix(apiV1Path).Subrouter()
n := negroni.New(
negroni.NewRecovery(),
negroni.NewLogger(),
negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
glog.Infoln(err)
}
glog.Infoln(string(requestDump))
next(w, r)
}),
negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
middleware.AuthMiddleware(w, r, next, config)
}),
negroni.Wrap(apiV1Router),
)
router.PathPrefix(apiV1Path).Handler(n)
// messages controller
tc := &messages.Controller{
Config: config,
HTTPClient: apihttp.NewHTTPClient(),
}
apiV1Router.HandleFunc("/telegram/messages/send", tc.SendMessage).Methods(http.MethodPost)
mongoClient := mongo.NewClient(config)
// notifications controller
repo := ¬ifications.Repository{
Mongo: mongoClient,
}
notif := ¬ifications.Controller{
Repository: repo,
}
apiV1Router.HandleFunc(notifications.GetPath, notif.GetAll).Methods(http.MethodGet)
apiV1Router.HandleFunc(notifications.SingleGetPath, notif.Get).Methods(http.MethodGet)
apiV1Router.HandleFunc(notifications.CreatePath, notif.Create).Methods(http.MethodPost)
apiV1Router.HandleFunc(notifications.DeletePath, notif.Delete).Methods(http.MethodDelete)
cleaner := notifications.Cleaner{
Repository: repo,
}
cleaner.StartPeriodicCleaner()
// providers controller
provRepo := &providers.Repository{
Mongo: mongoClient,
}
provController := providers.NewController(provRepo)
apiV1Router.HandleFunc(providers.GetPath, provController.GetAll).Methods(http.MethodGet)
apiV1Router.HandleFunc(providers.SingleGetPath, provController.Get).Methods(http.MethodGet)
apiV1Router.HandleFunc(providers.CreatePath, provController.Create).Methods(http.MethodPost)
apiV1Router.HandleFunc(providers.DeletePath, provController.Delete).Methods(http.MethodDelete)
// networks controller
if config.OmadaUrl != nil {
networksController := networks.NewController(config)
apiV1Router.HandleFunc(networks.Wifis, networksController.GetWifi).Methods(http.MethodGet)
apiV1Router.HandleFunc(networks.Wifis, networksController.UpdateWifi).Methods(http.MethodPatch)
}
glog.Infof("listening on :%s", *config.Port)
glog.Fatal(http.ListenAndServe(":"+*config.Port, router))
}