Skip to content
Snippets Groups Projects
Verified Commit cf8ed7bd authored by Ophestra's avatar Ophestra
Browse files

fix: clean shutdown via signal


The `app.Listen` method only returns when `app.Shutdown` is called.

Signed-off-by: default avatarOphestra Umiker <cat@ophivana.moe>
parent 1b9c228e
Branches
Tags
1 merge request!1Clean up project code and optimise performance
package main package main
import ( import (
"fmt"
"log" "log"
"os"
"time" "time"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
...@@ -9,7 +11,7 @@ import ( ...@@ -9,7 +11,7 @@ import (
"github.com/gofiber/fiber/v2/middleware/limiter" "github.com/gofiber/fiber/v2/middleware/limiter"
) )
func serve() error { func serve(sig chan os.Signal) error {
app := fiber.New() app := fiber.New()
// cors // cors
...@@ -38,5 +40,14 @@ func serve() error { ...@@ -38,5 +40,14 @@ func serve() error {
return c.Next() return c.Next()
}) })
// graceful shutdown
go func() {
<-sig
log.Println("shutting down")
if err := app.Shutdown(); err != nil {
fmt.Printf("cannot shutdown: %v", err)
}
}()
return app.Listen(conf[listenAddr]) return app.Listen(conf[listenAddr])
} }
...@@ -2,11 +2,11 @@ package main ...@@ -2,11 +2,11 @@ package main
import ( import (
"log" "log"
"regexp" "os"
"os/signal"
"syscall"
) )
var emailRegex = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
func main() { func main() {
if err := InitDB(conf[dbPath]); err != nil { if err := InitDB(conf[dbPath]); err != nil {
...@@ -15,8 +15,11 @@ func main() { ...@@ -15,8 +15,11 @@ func main() {
defer CloseDB() defer CloseDB()
if err := serve(); err != nil { sig := make(chan os.Signal, 1)
log.Printf("cannot serve: %s", err) signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
if err := serve(sig); err != nil {
log.Printf("cannot serve: %v", err)
} }
log.Println("application exit") log.Println("application exit")
......
...@@ -2,17 +2,21 @@ package main ...@@ -2,17 +2,21 @@ package main
import ( import (
"log" "log"
"regexp"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
var emailRegexp = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
type registration struct {
Email string `json:"email"`
}
// Waitlist registration route // Waitlist registration route
func routeRegister(app *fiber.App) { func routeRegister(app *fiber.App) {
app.Post("/register", func(c *fiber.Ctx) error { app.Post("/register", func(c *fiber.Ctx) error {
type Request struct { req := new(registration)
Email string `json:"email"`
}
req := new(Request)
if err := c.BodyParser(req); err != nil { if err := c.BodyParser(req); err != nil {
log.Printf("Invalid request body: %v", err) log.Printf("Invalid request body: %v", err)
...@@ -22,7 +26,7 @@ func routeRegister(app *fiber.App) { ...@@ -22,7 +26,7 @@ func routeRegister(app *fiber.App) {
} }
// Validate email format // Validate email format
if !emailRegex.MatchString(req.Email) { if !emailRegexp.MatchString(req.Email) {
log.Printf("Invalid email format: %s", req.Email) log.Printf("Invalid email format: %s", req.Email)
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "Invalid email format", "message": "Invalid email format",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment