2024-02-01 15:26:17 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-03-30 04:56:18 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg"
|
2024-02-01 15:26:17 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
2024-04-13 11:26:44 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/favicon"
|
2024-02-01 15:26:17 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/idempotency"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
2024-03-30 04:56:18 +00:00
|
|
|
"github.com/gofiber/template/html/v2"
|
2024-02-01 15:26:17 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/viper"
|
2024-04-13 11:26:44 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2024-02-01 15:26:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var A *fiber.App
|
|
|
|
|
|
|
|
func NewServer() {
|
2024-03-30 04:56:18 +00:00
|
|
|
templates := html.NewFileSystem(http.FS(pkg.FS), ".gohtml")
|
|
|
|
|
2024-02-01 15:26:17 +00:00
|
|
|
A = fiber.New(fiber.Config{
|
|
|
|
DisableStartupMessage: true,
|
|
|
|
EnableIPValidation: true,
|
|
|
|
ServerHeader: "Hydrogen.Interactive",
|
|
|
|
AppName: "Hydrogen.Interactive",
|
|
|
|
ProxyHeader: fiber.HeaderXForwardedFor,
|
|
|
|
JSONEncoder: jsoniter.ConfigCompatibleWithStandardLibrary.Marshal,
|
|
|
|
JSONDecoder: jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal,
|
2024-02-06 12:02:47 +00:00
|
|
|
BodyLimit: 50 * 1024 * 1024,
|
2024-03-22 16:41:32 +00:00
|
|
|
EnablePrintRoutes: viper.GetBool("debug.print_routes"),
|
2024-03-30 04:56:18 +00:00
|
|
|
Views: templates,
|
|
|
|
ViewsLayout: "views/index",
|
2024-02-01 15:26:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
A.Use(idempotency.New())
|
|
|
|
A.Use(cors.New(cors.Config{
|
|
|
|
AllowCredentials: true,
|
|
|
|
AllowMethods: strings.Join([]string{
|
|
|
|
fiber.MethodGet,
|
|
|
|
fiber.MethodPost,
|
|
|
|
fiber.MethodHead,
|
|
|
|
fiber.MethodOptions,
|
|
|
|
fiber.MethodPut,
|
|
|
|
fiber.MethodDelete,
|
|
|
|
fiber.MethodPatch,
|
|
|
|
}, ","),
|
|
|
|
AllowOriginsFunc: func(origin string) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
A.Use(logger.New(logger.Config{
|
|
|
|
Format: "${status} | ${latency} | ${method} ${path}\n",
|
|
|
|
Output: log.Logger,
|
|
|
|
}))
|
|
|
|
|
|
|
|
A.Get("/.well-known", getMetadata)
|
|
|
|
|
|
|
|
api := A.Group("/api").Name("API")
|
|
|
|
{
|
2024-02-21 14:58:51 +00:00
|
|
|
api.Get("/users/me", authMiddleware, getUserinfo)
|
2024-02-03 11:22:50 +00:00
|
|
|
api.Get("/users/:accountId", getOthersInfo)
|
2024-02-01 16:53:22 +00:00
|
|
|
|
2024-03-03 11:10:08 +00:00
|
|
|
api.Get("/feed", listFeed)
|
2024-02-05 07:51:31 +00:00
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
posts := api.Group("/posts").Name("Posts API")
|
2024-03-05 15:40:54 +00:00
|
|
|
{
|
|
|
|
posts.Get("/", listPost)
|
2024-05-19 12:05:44 +00:00
|
|
|
posts.Get("/:post", getPost)
|
2024-05-15 11:45:49 +00:00
|
|
|
posts.Post("/", authMiddleware, createPost)
|
2024-05-19 12:05:44 +00:00
|
|
|
posts.Post("/:post/react", authMiddleware, reactPost)
|
2024-05-15 11:45:49 +00:00
|
|
|
posts.Put("/:postId", authMiddleware, editPost)
|
|
|
|
posts.Delete("/:postId", authMiddleware, deletePost)
|
2024-03-02 17:23:11 +00:00
|
|
|
|
2024-05-19 12:05:44 +00:00
|
|
|
posts.Get("/:post/replies", listReplies)
|
2024-03-03 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2024-03-02 17:23:11 +00:00
|
|
|
api.Get("/categories", listCategories)
|
2024-02-21 14:58:51 +00:00
|
|
|
api.Post("/categories", authMiddleware, newCategory)
|
|
|
|
api.Put("/categories/:categoryId", authMiddleware, editCategory)
|
|
|
|
api.Delete("/categories/:categoryId", authMiddleware, deleteCategory)
|
2024-02-01 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 04:56:18 +00:00
|
|
|
A.Use(favicon.New(favicon.Config{
|
|
|
|
FileSystem: http.FS(pkg.FS),
|
|
|
|
File: "views/favicon.png",
|
|
|
|
URL: "/favicon.png",
|
2024-02-01 15:26:17 +00:00
|
|
|
}))
|
2024-03-30 04:56:18 +00:00
|
|
|
|
|
|
|
A.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.Render("views/open", fiber.Map{
|
|
|
|
"frontend": viper.GetString("frontend"),
|
|
|
|
})
|
|
|
|
})
|
2024-02-01 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Listen() {
|
|
|
|
if err := A.Listen(viper.GetString("bind")); err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("An error occurred when starting server...")
|
|
|
|
}
|
|
|
|
}
|