-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (46 loc) · 1.38 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
package main
import (
"log"
"net/http"
"bitbucket.org/guardrails-go/handler"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/render"
)
func main() {
s := CreateNewServer()
s.MountHandlers()
log.Printf("%v\n", "server running on port: 8000")
http.ListenAndServe(":8000", s.Router)
}
type Server struct {
Router *chi.Mux
// Db, config can be added here
}
func CreateNewServer() *Server {
s := &Server{}
s.Router = chi.NewRouter()
return s
}
func (s *Server) MountHandlers() {
//CORS
s.Router.Use(cors.Handler(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
AllowedOrigins: []string{"https://*", "http://*"},
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
// set content-type to json
s.Router.Use(render.SetContentType(render.ContentTypeJSON))
s.Router.Use(middleware.Heartbeat("/"))
// Mount all Middleware here
s.Router.Use(middleware.Logger)
s.Router.Group(func(r chi.Router) {
handler.NewHandler(r)
})
}