main(feat) add logging

This commit is contained in:
Theo Technicguy 2023-08-22 17:48:11 +02:00
parent 391ff179d0
commit 0a80a5bb82
Signed by: theo.technicguy
GPG Key ID: 3BC661201BCA53D9
1 changed files with 17 additions and 5 deletions

22
main.go
View File

@ -9,33 +9,44 @@ import (
"git.licolas.net/hoffman/server/api"
"git.licolas.net/hoffman/server/database"
"git.licolas.net/hoffman/server/logger"
"git.licolas.net/hoffman/server/logic"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
)
func main() {
logger.Setup()
logger.SetLogLevel(zerolog.DebugLevel)
logger := logger.GetLogger("main")
logger.Info().Msg("starting hoffman-server")
logger.Debug().Msg("connecting to datasource")
dsrc, err := database.Connect("./db.db")
if err != nil {
panic(err)
logger.Fatal().Err(err)
os.Exit(1)
}
logger.Debug().Msg("fetching logic")
lgc := logic.UseDatasource(dsrc)
logger.Debug().Msg("setting http server routes")
router := mux.NewRouter()
apiRouter := router.PathPrefix("/api").Subrouter()
api.SetLogicProvider(lgc)
api.SetRoutes(apiRouter)
fmt.Println("Starting server")
srv := &http.Server{
Addr: "[::1]:8080",
Handler: router,
}
logger.Info().Str("addr", srv.Addr).Msg("starting http server")
go func() {
if err := srv.ListenAndServe(); err != nil {
fmt.Println(err)
logger.Error().Err(err)
}
}()
@ -43,11 +54,12 @@ func main() {
signal.Notify(c, os.Interrupt)
<-c
fmt.Println()
ctx, cancel := context.WithTimeout(context.Background(), 10)
defer cancel()
fmt.Println("Closing everything")
logger.Info().Msg("shutting down")
srv.Shutdown(ctx)
dsrc.Disconnect()
}