1
0
Fork 0
This repository has been archived on 2025-09-02. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
mareshq-yggdrasil/cmd/yggdrasil/main.go
Vojtech Mares 3ee3be9e7f
feat: setup Fiber HTTP server
- graceful shutdown
- logging with zap
2024-05-04 10:01:48 +02:00

64 lines
1.1 KiB
Go

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.")
}