-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
58 lines (47 loc) · 1.22 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
package main
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
internal "github.com/miguno/golang-docker-build-tutorial/internal/pkg"
)
type Server struct {
Router *chi.Mux
// Other configs such as DB settings can be added here
}
func CreateNewServer() *Server {
s := &Server{}
s.Router = chi.NewRouter()
// Uncomment to enable logging of incoming HTTP requests to STDOUT.
// Requires `import "github.com/go-chi/chi/v5/middleware"`.
//s.Router.Use(middleware.Logger)
return s
}
// StatusResponse is just a basic example.
type StatusResponse struct {
Status string `json:"status,omitempty"`
}
func (s *Server) MountHandlers() {
// Mount all Middleware here
s.Router.Use(middleware.Logger)
// Mount all handlers here
s.Router.Get("/status", func(w http.ResponseWriter, r *http.Request) {
// Create a Response object
var response StatusResponse
if internal.IsIdleToyFunction() {
response = StatusResponse{Status: "idle"}
} else {
response = StatusResponse{Status: "busy"}
}
render.JSON(w, r, response)
})
}
func main() {
s := CreateNewServer()
s.MountHandlers()
err := http.ListenAndServe(":8123", s.Router)
if err != nil {
panic(err)
}
}