-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
63 lines (56 loc) · 1.24 KB
/
api.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
package main
import (
"net/http"
"github.com/FederationOfFathers/consul"
"github.com/NYTimes/gziphandler"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
nocache "github.com/rabeesh/negroni-nocache"
"github.com/rs/cors"
"go.uber.org/zap"
)
var router = mux.NewRouter()
var mwh = negroni.New()
var logger *zap.Logger
var cmw = cors.New(cors.Options{
AllowedOrigins: []string{
"http://127.0.0.1",
"http://localhost",
},
AllowedMethods: []string{
"GET",
"PUT",
},
AllowCredentials: true,
})
func init() {
l, _ := zap.NewProduction()
logger = l.With(zap.String("module", "user-registry"))
}
func mw(fn func(w http.ResponseWriter, r *http.Request)) http.Handler {
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:*", "http://127.0.0.*"},
AllowCredentials: true,
})
return gziphandler.GzipHandler(
c.Handler(
negroni.New(
&httpLogger{},
negroni.NewRecovery(),
nocache.New(true),
negroni.Wrap(
http.HandlerFunc(fn),
),
),
),
)
}
func mindHTTP() {
if err := consul.RegisterOn("user-registry", listenOn); err != nil {
panic(err)
}
logger.Fatal(
"error starting API http server",
zap.String("listenOn", listenOn),
zap.Error(http.ListenAndServe(listenOn, router)))
}