feat: setup Fiber HTTP server
- graceful shutdown - logging with zap
This commit is contained in:
parent
ecdf67f371
commit
3ee3be9e7f
5 changed files with 129 additions and 8 deletions
64
cmd/yggdrasil/main.go
Normal file
64
cmd/yggdrasil/main.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
fiberzap "github.com/gofiber/contrib/fiberzap/v2"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var port = 3000
|
||||
|
||||
func main() {
|
||||
shutdownCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
logger := zap.Must(zap.NewDevelopment())
|
||||
defer logger.Sync()
|
||||
sugared := logger.Sugar()
|
||||
|
||||
config := fiber.Config{
|
||||
DisableStartupMessage: true,
|
||||
|
||||
IdleTimeout: 10 * time.Second,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
app := fiber.New(config)
|
||||
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return c.SendString("Hello, World!")
|
||||
})
|
||||
|
||||
app.Use(fiberzap.New(fiberzap.Config{
|
||||
Logger: logger,
|
||||
}))
|
||||
|
||||
go func() {
|
||||
err := app.Listen(fmt.Sprintf(":%d", port))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
sugared.Infow("Yggdrasil is now running", "port", port)
|
||||
|
||||
<-shutdownCtx.Done()
|
||||
stop()
|
||||
|
||||
sugared.Infoln("Shutting down gracefully...")
|
||||
err := app.ShutdownWithTimeout(10 * time.Second)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Cleanup code here
|
||||
|
||||
sugared.Infoln("Yggdrasil shut down gracefully.")
|
||||
}
|
||||
Reference in a new issue