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
|
|
|
|
2021-09-30 20:44:14 +03:00
|
|
|
"github.com/gin-contrib/sessions"
|
|
|
|
"github.com/gin-contrib/sessions/cookie"
|
|
|
|
csrf "github.com/utrack/gin-csrf"
|
|
|
|
|
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()
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
}
|
2021-09-30 20:44:14 +03:00
|
|
|
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-30 20:44:14 +03:00
|
|
|
|
|
|
|
sessionStore := cookie.NewStore([]byte("secret"))
|
|
|
|
router.Use(sessions.Sessions("mysession", sessionStore))
|
|
|
|
router.Use(csrf.Middleware(csrf.Options{
|
|
|
|
Secret: "secret123",
|
|
|
|
TokenGetter: func(c *gin.Context) string {
|
|
|
|
token, err := c.Cookie("csrf")
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return token
|
|
|
|
},
|
|
|
|
ErrorFunc: func(c *gin.Context) {
|
|
|
|
c.String(400, "CSRF token mismatch")
|
|
|
|
c.Abort()
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
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)
|
|
|
|
}
|
2021-08-28 18:41:34 +03:00
|
|
|
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)
|
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
|
|
|
}
|