🎉 Initial Commit
This commit is contained in:
29
pkg/internal/server/api/index.go
Normal file
29
pkg/internal/server/api/index.go
Normal file
@ -0,0 +1,29 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func MapAPIs(app *fiber.App, baseURL string) {
|
||||
api := app.Group(baseURL).Name("API")
|
||||
{
|
||||
products := api.Group("/products")
|
||||
{
|
||||
products.Get("/", listProduct)
|
||||
products.Get("/created", listCreatedProduct)
|
||||
products.Get("/:productId", getProduct)
|
||||
products.Post("/", createProduct)
|
||||
products.Put("/:productId", updateProduct)
|
||||
products.Delete("/:productId", deleteProduct)
|
||||
|
||||
releases := products.Group("/:productId/releases")
|
||||
{
|
||||
releases.Get("/", listRelease)
|
||||
releases.Get("/:releaseId", getRelease)
|
||||
releases.Post("/", createRelease)
|
||||
releases.Put("/:releaseId", updateRelease)
|
||||
releases.Delete("/:releaseId", deleteRelease)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
175
pkg/internal/server/api/products_api.go
Normal file
175
pkg/internal/server/api/products_api.go
Normal file
@ -0,0 +1,175 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/models"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func listProduct(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
|
||||
count, err := services.CountProduct()
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListProduct(take, offset)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
||||
|
||||
func listCreatedProduct(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
|
||||
count, err := services.CountCreatedProduct(user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListCreatedProduct(user.ID, take, offset)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
||||
|
||||
func getProduct(c *fiber.Ctx) error {
|
||||
alias := c.Params("productId")
|
||||
|
||||
var item models.Product
|
||||
if numericId, err := strconv.Atoi(alias); err == nil {
|
||||
item, err = services.GetProduct(uint(numericId))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
} else {
|
||||
item, err = services.GetProduct(uint(numericId))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return c.JSON(item)
|
||||
}
|
||||
|
||||
func createProduct(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Name string `json:"name" validate:"required,max=256"`
|
||||
Description string `json:"description" validate:"max=4096"`
|
||||
Introduction string `json:"introduction"`
|
||||
Alias string `json:"alias" validate:"required"`
|
||||
Tags []string `json:"tags"`
|
||||
Attachments []string `json:"attachments"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
product := models.Product{
|
||||
Name: data.Name,
|
||||
Alias: data.Alias,
|
||||
Description: data.Description,
|
||||
Tags: data.Tags,
|
||||
Meta: models.ProductMeta{
|
||||
Introduction: data.Introduction,
|
||||
Attachments: data.Attachments,
|
||||
},
|
||||
AccountID: user.ID,
|
||||
}
|
||||
|
||||
if product, err := services.NewProduct(product); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(product)
|
||||
}
|
||||
}
|
||||
|
||||
func updateProduct(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("productId", 0)
|
||||
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Icon string `json:"icon"`
|
||||
Name string `json:"name" validate:"required,max=256"`
|
||||
Description string `json:"description" validate:"max=4096"`
|
||||
Introduction string `json:"introduction"`
|
||||
Alias string `json:"alias" validate:"required"`
|
||||
Tags []string `json:"tags"`
|
||||
Previews []string `json:"previews"`
|
||||
Attachments []string `json:"attachments"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
product, err := services.GetProductWithUser(uint(id), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
product.Icon = data.Icon
|
||||
product.Name = data.Name
|
||||
product.Description = data.Description
|
||||
product.Tags = data.Tags
|
||||
product.Previews = data.Previews
|
||||
product.Meta.Introduction = data.Introduction
|
||||
product.Meta.Attachments = data.Attachments
|
||||
|
||||
if product, err := services.UpdateProduct(product); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(product)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteProduct(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("productId", 0)
|
||||
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
product, err := services.GetProductWithUser(uint(id), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
if _, err := services.DeleteProduct(product); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
167
pkg/internal/server/api/releases_api.go
Normal file
167
pkg/internal/server/api/releases_api.go
Normal file
@ -0,0 +1,167 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/models"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
func listRelease(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
id, _ := c.ParamsInt("productId", 0)
|
||||
|
||||
count, err := services.CountRelease(id)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListRelease(id, take, offset)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
||||
|
||||
func getRelease(c *fiber.Ctx) error {
|
||||
productId, _ := c.ParamsInt("productId", 0)
|
||||
id, _ := c.ParamsInt("releaseId", 0)
|
||||
|
||||
if item, err := services.GetReleaseWithProduct(uint(id), uint(productId)); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
} else {
|
||||
return c.JSON(item)
|
||||
}
|
||||
}
|
||||
|
||||
func createRelease(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
productId, _ := c.ParamsInt("productId", 0)
|
||||
|
||||
var data struct {
|
||||
Version string `json:"version" validate:"required"`
|
||||
Type int `json:"type" validate:"required"`
|
||||
Channel string `json:"channel" validate:"required"`
|
||||
Title string `json:"title" validate:"required,max=1024"`
|
||||
Description string `json:"description" validate:"required,max=4096"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Assets map[string]any `json:"assets" validate:"required"`
|
||||
Attachments []string `json:"attachments"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
product, err := services.GetProductWithUser(uint(productId), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
release := models.ProductRelease{
|
||||
Version: data.Version,
|
||||
Type: models.ProductReleaseType(data.Type),
|
||||
Channel: data.Channel,
|
||||
Assets: datatypes.NewJSONType(data.Assets),
|
||||
ProductID: product.ID,
|
||||
Meta: models.ProductReleaseMeta{
|
||||
Title: data.Title,
|
||||
Description: data.Description,
|
||||
Content: data.Content,
|
||||
Attachments: data.Attachments,
|
||||
},
|
||||
}
|
||||
|
||||
if release, err := services.NewRelease(release); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(release)
|
||||
}
|
||||
}
|
||||
|
||||
func updateRelease(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
productId, _ := c.ParamsInt("productId", 0)
|
||||
id, _ := c.ParamsInt("releaseId", 0)
|
||||
|
||||
var data struct {
|
||||
Version string `json:"version" validate:"required"`
|
||||
Type int `json:"type" validate:"required"`
|
||||
Channel string `json:"channel" validate:"required"`
|
||||
Title string `json:"title" validate:"required,max=1024"`
|
||||
Description string `json:"description" validate:"required,max=4096"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Assets map[string]any `json:"assets" validate:"required"`
|
||||
Attachments []string `json:"attachments"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
product, err := services.GetProductWithUser(uint(productId), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
release, err := services.GetReleaseWithProduct(uint(id), product.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
release.Version = data.Version
|
||||
release.Type = models.ProductReleaseType(data.Type)
|
||||
release.Channel = data.Channel
|
||||
release.Assets = datatypes.NewJSONType(data.Assets)
|
||||
release.Meta.Title = data.Title
|
||||
release.Meta.Description = data.Description
|
||||
release.Meta.Content = data.Content
|
||||
release.Meta.Attachments = data.Attachments
|
||||
|
||||
if release, err := services.UpdateRelease(release); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(release)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteRelease(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
productId, _ := c.ParamsInt("productId", 0)
|
||||
id, _ := c.ParamsInt("releaseId", 0)
|
||||
|
||||
product, err := services.GetProductWithUser(uint(productId), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
release, err := services.GetReleaseWithProduct(uint(id), product.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
if _, err := services.DeleteRelease(release); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
}
|
18
pkg/internal/server/exts/utils.go
Normal file
18
pkg/internal/server/exts/utils.go
Normal file
@ -0,0 +1,18 @@
|
||||
package exts
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var validation = validator.New(validator.WithRequiredStructEnabled())
|
||||
|
||||
func BindAndValidate(c *fiber.Ctx, out any) error {
|
||||
if err := c.BodyParser(out); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else if err := validation.Struct(out); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
71
pkg/internal/server/server.go
Normal file
71
pkg/internal/server/server.go
Normal file
@ -0,0 +1,71 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/server/api"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/gofiber/fiber/v2/middleware/idempotency"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var IReader *sec.InternalTokenReader
|
||||
|
||||
type App struct {
|
||||
app *fiber.App
|
||||
}
|
||||
|
||||
func NewServer() *App {
|
||||
app := fiber.New(fiber.Config{
|
||||
DisableStartupMessage: true,
|
||||
EnableIPValidation: true,
|
||||
ServerHeader: "Matrix.Nucleus",
|
||||
AppName: "Matrix.Nucleus",
|
||||
ProxyHeader: fiber.HeaderXForwardedFor,
|
||||
JSONEncoder: jsoniter.ConfigCompatibleWithStandardLibrary.Marshal,
|
||||
JSONDecoder: jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal,
|
||||
BodyLimit: 512 * 1024 * 1024 * 1024, // 512 TiB
|
||||
EnablePrintRoutes: viper.GetBool("debug.print_routes"),
|
||||
})
|
||||
|
||||
app.Use(idempotency.New())
|
||||
app.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
|
||||
},
|
||||
}))
|
||||
|
||||
app.Use(logger.New(logger.Config{
|
||||
Format: "${status} | ${latency} | ${method} ${path}\n",
|
||||
Output: log.Logger,
|
||||
}))
|
||||
|
||||
app.Use(sec.ContextMiddleware(IReader))
|
||||
|
||||
api.MapAPIs(app, "/api")
|
||||
|
||||
return &App{app}
|
||||
}
|
||||
|
||||
func (v *App) Listen() {
|
||||
if err := v.app.Listen(viper.GetString("bind")); err != nil {
|
||||
log.Fatal().Err(err).Msg("An error occurred when starting server...")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user