84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
jwtware "github.com/gofiber/contrib/jwt"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
"github.com/gofiber/fiber/v2/middleware/limiter"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"nim.jasinco.work/app/internal"
|
|
"nim.jasinco.work/app/internal/handlers"
|
|
"nim.jasinco.work/app/nimdb"
|
|
)
|
|
|
|
func main() {
|
|
|
|
err := internal.ReadFromENV()
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
|
|
dbpool, err := pgxpool.New(context.Background(), internal.POSTGRES_URL)
|
|
if err != nil {
|
|
log.Fatal("Failed to open connection to db")
|
|
}
|
|
defer dbpool.Close()
|
|
internal.POOL = dbpool
|
|
internal.NIMDB = nimdb.New(dbpool)
|
|
|
|
if internal.IGAPI_ACTIVATE {
|
|
conn, client := internal.IGAPI_establish()
|
|
defer conn.Close()
|
|
go internal.IGAPI_chan_exec(client)
|
|
}
|
|
|
|
app := fiber.New(fiber.Config{Prefork: internal.PREFORK})
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
return c.SendString("Hello, World!")
|
|
})
|
|
|
|
app.Use(limiter.New(limiter.Config{
|
|
Next: func(c *fiber.Ctx) bool {
|
|
return c.IP() == "127.0.0.1"
|
|
},
|
|
Max: 50,
|
|
Expiration: 30 * time.Second,
|
|
KeyGenerator: func(c *fiber.Ctx) string {
|
|
return c.Get("x-forwarded-for")
|
|
},
|
|
LimitReached: func(c *fiber.Ctx) error {
|
|
return c.SendFile("./toofast.html")
|
|
},
|
|
}))
|
|
app.Use(cors.New(cors.Config{
|
|
AllowOrigins: internal.CORS_ALLOW,
|
|
AllowCredentials: true,
|
|
}))
|
|
app.Get("/api/post", handlers.Fetch_post)
|
|
app.Post("/api/post", handlers.Insert_Post)
|
|
app.Delete("/api/post", handlers.Delete_post)
|
|
app.Get("/api/heart", handlers.Add_heart)
|
|
app.Post("/api/admin/login", handlers.Admin_Login)
|
|
app.Static("/static", "./static/")
|
|
|
|
app.Get("/admin", func(c *fiber.Ctx) error {
|
|
return c.Redirect("/admin/login")
|
|
})
|
|
app.Get("/admin/*", handlers.AdminSendPage)
|
|
app.Use(jwtware.New(jwtware.Config{
|
|
SigningKey: jwtware.SigningKey{Key: []byte(internal.JWT_SECRET)},
|
|
TokenLookup: "cookie:token",
|
|
}))
|
|
|
|
app.Get("/api/admin/new_totp", handlers.Admin_new_totp_code)
|
|
app.Get("/api/admin/fetch_post", handlers.Admin_Fetch_Post)
|
|
app.Put("/api/admin/verify_post", handlers.AdminVerify)
|
|
app.Post("/api/admin/create", handlers.Admin_create)
|
|
|
|
log.Panic(app.Listen(":3000"))
|
|
}
|