jokes-bapak2/api/main.go

58 lines
1.3 KiB
Go

package main
import (
"jokes-bapak2-api/app"
"log"
"os"
"os/signal"
"github.com/gofiber/fiber/v2"
_ "github.com/joho/godotenv/autoload"
)
func main() {
a := app.New()
// Start server (with or without graceful shutdown).
if os.Getenv("ENV") == "development" {
StartServer(a)
} else {
StartServerWithGracefulShutdown(a)
}
}
// StartServerWithGracefulShutdown function for starting server with a graceful shutdown.
func StartServerWithGracefulShutdown(a *fiber.App) {
// Create channel for idle connections.
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt) // Catch OS signals.
<-sigint
// Received an interrupt signal, shutdown.
if err := a.Shutdown(); err != nil {
// Error from closing listeners, or context timeout:
log.Printf("Oops... Server is not shutting down! Reason: %v", err)
}
close(idleConnsClosed)
}()
// Run server.
if err := a.Listen(os.Getenv("HOST") + ":" + os.Getenv("PORT")); err != nil {
log.Printf("Oops... Server is not running! Reason: %v", err)
}
<-idleConnsClosed
}
// StartServer func for starting a simple server.
func StartServer(a *fiber.App) {
// Run server.
if err := a.Listen(os.Getenv("HOST") + ":" + os.Getenv("PORT")); err != nil {
log.Printf("Oops... Server is not running! Reason: %v", err)
}
}