-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (67 loc) · 2.47 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
package main
import (
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
// CONFIG
"skaffolder/Manage_Film_Example/config"
"skaffolder/Manage_Film_Example/security"
// start import model
// APIs
"skaffolder/Manage_Film_Example/api/Manage_Film_Example_db/actor"
"skaffolder/Manage_Film_Example/api/Manage_Film_Example_db/film"
"skaffolder/Manage_Film_Example/api/Manage_Film_Example_db/filmmaker"
"skaffolder/Manage_Film_Example/api/Manage_Film_Example_db/user"
// end import model
)
func Routes(configuration *config.Config) *chi.Mux {
router := chi.NewRouter()
// Basic CORS
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
MaxAge: 300,
})
router.Use(
cors.Handler,
render.SetContentType(render.ContentTypeJSON), // Set content-Type headers as application/json
middleware.Logger, // Log API request calls
middleware.DefaultCompress, // Compress results, mostly gzipping assets and json
middleware.RedirectSlashes, // Redirect slashes to no slash URL versions
middleware.Recoverer, // Recover from panics without crashing server
)
// Routing
router.Route("/api", func(r chi.Router) {
r.Mount("/", security.New(configuration).Routes())
r.Mount("/actor", actor.New(configuration).Routes())
r.Mount("/film", film.New(configuration).Routes())
r.Mount("/filmmaker", filmmaker.New(configuration).Routes())
r.Mount("/user", user.New(configuration).Routes())
})
return router
}
func main() {
configuration, err := config.New()
if err != nil {
log.Panicln("Configuration error", err)
}
router := Routes(configuration)
walkFunc := func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
log.Printf("%s %s\n", method, route) // Walk and print out all routes
return nil
}
if err := chi.Walk(router, walkFunc); err != nil {
log.Panicf("Logging err: %s\n", err.Error()) // panic if there is an error
}
// Static filer
fs := http.FileServer(http.Dir(configuration.Constants.PUBLIC))
router.Handle("/*", http.StripPrefix("/", fs))
// Start
log.Println("Serving application at PORT :" + configuration.Constants.PORT)
log.Fatal(http.ListenAndServe(":"+configuration.Constants.PORT, router))
}