write base server

This commit is contained in:
2026-09-02 18:16:35 +08:00
parent b4bfb50ae3
commit 3004676239
7 changed files with 234 additions and 0 deletions

64
main.go
View File

@@ -1 +1,65 @@
//go:generate go run gen/gen.go
package main
import (
"log"
"os"
"regexp"
"slices"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gookit/color"
"github.com/joho/godotenv"
"bdgrp_go/db"
"bdgrp_go/routes"
)
const PORT = ":8482"
var servers = []string{"TW", "JP", "GL", "CN"}
func main() {
if _, err := os.Stat(".env"); os.IsNotExist(err) {
log.Fatal("Error: .env not found.")
}
godotenv.Load()
server := os.Getenv("SERVER")
valid := slices.Contains(servers, server)
if !valid {
log.Fatal("Error: Unknown Server.")
}
for _, s := range servers {
os.MkdirAll("resp/"+s, 0755)
}
db.Init(servers)
app := fiber.New(fiber.Config{
StrictRouting: true,
ETag: false,
})
app.Use(logger.New())
re := regexp.MustCompile(`/+`)
app.Use(func(c *fiber.Ctx) error {
c.Path(re.ReplaceAllString(c.Path(), "/"))
return c.Next()
})
routes.LoadAll(app)
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(os.Getenv("SERVER"))
})
log.Printf("Running on %s", PORT)
color.HEX("#00AABB").Printf("Server: %s\n", server)
app.Listen(PORT)
}