-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (40 loc) · 1.43 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
package main
import (
"log"
"net/http"
"github.com/richardbertozzo/first-api-go/animals"
"github.com/richardbertozzo/first-api-go/config"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
)
func routes(config *config.Config) *chi.Mux {
router := chi.NewRouter()
router.Use(
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
)
router.Route("/v1", func(r chi.Router) {
r.Mount("/api/animals", animals.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
}
log.Print("Server running at PORT:" + configuration.Constants.PORT)
log.Fatal(http.ListenAndServe(":"+configuration.Constants.PORT, router))
}