-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (57 loc) · 1.75 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
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
"github.com/mui-storyblok/mui-theme-server/internal/api"
)
func main() {
app, err := api.NewApp()
if err != nil {
panic(err)
}
// Define cors rules
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
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
})
r := chi.NewRouter()
r.Use(
cors.Handler, // Allow * origins
// set content-type headers as application/json
render.SetContentType(render.ContentTypeJSON),
// log api request calls
middleware.Logger,
// strip slashes to no slash URL versions
middleware.StripSlashes,
// recover from panics without crashing server
middleware.Recoverer,
// Set a timeout value on the request context (ctx), that will signal through ctx.Done()
// that the request has timed out and furtherprocessing should be stopped.
middleware.Timeout(30*time.Second),
)
r.Route("/api", api.Router(r, app))
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 all the routes
return nil
}
if err := chi.Walk(r, walkFunc); err != nil {
log.Panicf("Error Log: %s\n", err.Error()) // panic if there is an error
}
log.Printf("Serving application on PORT : %s", os.Getenv("PORT"))
log.Fatal(http.ListenAndServe(":" + os.Getenv("PORT"), r))
}