micrach/main.go

104 lines
2.3 KiB
Go
Raw Permalink Normal View History

2021-08-26 16:16:50 +03:00
package main
import (
"context"
2021-08-26 16:16:50 +03:00
"log"
"strconv"
2022-04-11 09:31:17 +03:00
"strings"
2021-08-26 16:16:50 +03:00
2021-08-26 17:32:19 +03:00
_ "github.com/joho/godotenv/autoload"
2021-08-26 16:16:50 +03:00
2026-07-16 15:25:23 +03:00
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/compress"
"github.com/gofiber/fiber/v3/middleware/limiter"
"github.com/gofiber/fiber/v3/middleware/recover"
"github.com/gofiber/fiber/v3/middleware/static"
"github.com/gofiber/template/html/v3"
"micrach/build"
"micrach/config"
"micrach/controllers"
"micrach/db"
"micrach/gateway"
"micrach/repositories"
"micrach/templates"
"micrach/utils"
2021-08-26 16:16:50 +03:00
)
2026-07-16 15:58:06 +03:00
// Reserve 1 MiB for multipart boundaries, filenames, title, and text.
const multipartBodyLimit = 4*utils.FILE_SIZE_IN_BYTES + (1 << 20)
2026-07-16 15:32:54 +03:00
2026-07-16 15:22:15 +03:00
func skipRateLimit(isLocal bool, path string) bool {
isRequestForStatic := strings.Contains(path, "/static") ||
strings.Contains(path, "/uploads") ||
strings.Contains(path, "/captcha")
return isRequestForStatic || isLocal
}
func registerStaticRoutes(app *fiber.App) {
2026-07-16 15:25:23 +03:00
app.Get("/uploads/*", static.New("./uploads"))
app.Get("/static/*", static.New("./static"))
2026-07-16 15:22:15 +03:00
}
2021-08-26 16:16:50 +03:00
func main() {
ctx := context.Background()
config.Init()
db.Init(ctx)
db.Migrate(ctx)
defer db.Pool.Close()
if config.App.IsDbSeeded {
repositories.Seed(ctx)
2021-09-08 15:36:13 +03:00
}
2021-08-26 16:16:50 +03:00
if config.App.Env == "production" {
build.RenameCss()
}
err := utils.CreateUploadsFolder()
2021-09-04 21:53:48 +03:00
if err != nil {
log.Panicln(err)
}
engine := html.New("./templates", ".html")
engine.AddFunc("Iterate", templates.Iterate)
engine.AddFunc("NotNil", templates.NotNil)
2021-09-08 16:56:03 +03:00
2026-07-16 15:32:54 +03:00
app := fiber.New(fiber.Config{
Views: engine,
BodyLimit: multipartBodyLimit,
})
app.Use(recover.New())
2022-05-18 10:45:53 +03:00
if config.App.IsRateLimiterEnabled {
app.Use(limiter.New(limiter.Config{
2026-07-16 15:25:23 +03:00
Next: func(c fiber.Ctx) bool {
2026-07-16 15:22:15 +03:00
return skipRateLimit(c.IsFromLocal(), c.Path())
2022-05-18 10:45:53 +03:00
},
Max: 50,
}))
}
app.Use(compress.New())
2026-07-16 15:22:15 +03:00
registerStaticRoutes(app)
app.Get("/", controllers.GetThreads)
app.Post("/", controllers.CreateThread)
app.Get("/:threadID", controllers.GetThread)
app.Post("/:threadID", controllers.UpdateThread)
app.Get("/captcha/:captchaID", controllers.GetCaptcha)
if config.App.Gateway.Url != "" {
app.Get("/api/ping", gateway.Ping)
gateway.Connect()
}
2021-08-26 16:16:50 +03:00
log.Println("app - online, port -", strconv.Itoa(config.App.Port))
2021-08-26 16:16:50 +03:00
log.Println("all systems nominal")
err = app.Listen(":" + strconv.Itoa(config.App.Port))
if err != nil {
log.Println("app - ofline")
log.Panicln(err)
}
2021-08-26 16:16:50 +03:00
}