micrach/main.go

82 lines
1.8 KiB
Go
Raw Normal View History

2021-08-26 16:16:50 +03:00
package main
import (
2021-09-10 02:17:45 +03:00
"html/template"
2021-08-26 16:16:50 +03:00
"log"
"strconv"
2021-09-08 16:56:03 +03:00
"time"
2021-08-26 16:16:50 +03:00
"github.com/gin-gonic/gin"
2021-08-26 17:32:19 +03:00
_ "github.com/joho/godotenv/autoload"
2021-09-08 16:56:03 +03:00
limiter "github.com/ulule/limiter/v3"
mgin "github.com/ulule/limiter/v3/drivers/middleware/gin"
memory "github.com/ulule/limiter/v3/drivers/store/memory"
2021-08-26 16:16:50 +03:00
Build "micrach/build"
2021-08-26 16:16:50 +03:00
Config "micrach/config"
2021-08-26 20:23:55 +03:00
Controllers "micrach/controllers"
2021-09-03 02:34:45 +03:00
Db "micrach/db"
2021-08-30 12:09:27 +03:00
Repositories "micrach/repositories"
2021-09-04 21:53:48 +03:00
Utils "micrach/utils"
2021-08-26 16:16:50 +03:00
)
func main() {
Config.Init()
2021-09-03 02:34:45 +03:00
Db.Init()
Db.Migrate()
2021-09-03 02:34:45 +03:00
defer Db.Pool.Close()
2021-08-26 16:16:50 +03:00
gin.SetMode(Config.App.Env)
2021-09-08 15:36:13 +03:00
if Config.App.SeedDb {
Repositories.Seed()
}
2021-08-26 16:16:50 +03:00
if Config.App.Env == "release" {
Build.RenameCss()
}
2021-09-04 21:53:48 +03:00
err := Utils.CreateUploadsFolder()
if err != nil {
log.Panicln(err)
}
2021-09-08 16:56:03 +03:00
rate := limiter.Rate{
Period: 1 * time.Hour,
Limit: 1000,
}
rateLimiterStore := memory.NewStore()
instance := limiter.New(rateLimiterStore, rate)
2021-09-08 16:56:03 +03:00
middleware := mgin.NewMiddleware(instance)
2021-09-11 17:57:57 +03:00
router := gin.New()
router.Use(gin.Recovery())
2021-09-10 02:17:45 +03:00
router.SetFuncMap(template.FuncMap{
"Iterate": func(count int) []int {
var i int
var Items []int
2021-09-10 14:52:56 +03:00
for i = 1; i < count+1; i++ {
2021-09-10 02:17:45 +03:00
Items = append(Items, i)
}
return Items
},
})
2021-08-28 17:34:58 +03:00
router.LoadHTMLGlob("templates/*.html")
2021-09-08 16:56:03 +03:00
router.ForwardedByClientIP = true
2021-09-08 18:54:46 +03:00
if Config.App.IsRateLimiterEnabled {
router.Use(middleware)
}
router.Static("/uploads", "./uploads")
2021-08-30 23:29:51 +03:00
router.Static("/static", "./static")
2021-08-26 20:23:55 +03:00
router.GET("/", Controllers.GetThreads)
router.POST("/", Controllers.CreateThread)
2022-01-26 20:37:14 +03:00
router.GET("/api", Controllers.GetThreadsApi)
2021-09-05 00:36:55 +03:00
router.GET("/:threadID", Controllers.GetThread)
router.POST("/:threadID", Controllers.UpdateThread)
2021-10-03 11:48:59 +03:00
router.GET("/captcha/:captchaID", Controllers.GetCaptcha)
2021-08-26 16:16:50 +03:00
2021-08-28 19:30:14 +03:00
log.Println("port", Config.App.Port, "- online")
2021-08-26 16:16:50 +03:00
log.Println("all systems nominal")
2021-08-28 19:30:14 +03:00
router.Run(":" + strconv.Itoa(Config.App.Port))
2021-08-26 16:16:50 +03:00
}