generated from wisdom-oss/microservice-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
66 lines (54 loc) · 1.91 KB
/
service.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
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/go-chi/chi/v5"
chiMiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog"
"github.com/rs/zerolog/log"
wisdomMiddleware "github.com/wisdom-oss/microservice-middlewares/v3"
"microservice/routes"
"microservice/globals"
)
// the main function bootstraps the http server and handlers used for this
// microservice
func main() {
// create a new logger for the main function
l := log.With().Str("step", "main").Logger()
l.Info().Msgf("starting %s service", globals.ServiceName)
// create a new router
router := chi.NewRouter()
// add some middlewares to the router to allow identifying requests
router.Use(wisdomMiddleware.ErrorHandler(globals.ServiceName, globals.Errors))
router.Use(chiMiddleware.RequestID)
router.Use(chiMiddleware.RealIP)
router.Use(httplog.Handler(l))
// now add the authorization middleware to the router
router.Use(wisdomMiddleware.Authorization(globals.AuthorizationConfiguration, globals.ServiceName))
// now mount the admin router
router.HandleFunc("/", routes.BasicHandler)
router.HandleFunc("/internal-error", routes.BasicWithErrorHandling)
// now boot up the service
// Configure the HTTP server
server := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%s", globals.Environment["LISTEN_PORT"]),
WriteTimeout: time.Second * 600,
ReadTimeout: time.Second * 600,
IdleTimeout: time.Second * 600,
Handler: router,
}
// Start the server and log errors that happen while running it
go func() {
if err := server.ListenAndServe(); err != nil {
l.Fatal().Err(err).Msg("An error occurred while starting the http server")
}
}()
// Set up the signal handling to allow the server to shut down gracefully
cancelSignal := make(chan os.Signal, 1)
signal.Notify(cancelSignal, os.Interrupt)
// Block further code execution until the shutdown signal was received
<-cancelSignal
}