draincloud-core/internal/app/app.go

82 lines
2.0 KiB
Go

package app
import (
"context"
"errors"
"net/http"
"git.optclblast.xyz/draincloud/draincloud-core/internal/app/handlers"
"git.optclblast.xyz/draincloud/draincloud-core/internal/domain"
filesengine "git.optclblast.xyz/draincloud/draincloud-core/internal/files_engine"
"git.optclblast.xyz/draincloud/draincloud-core/internal/processor"
resolvedispatcher "git.optclblast.xyz/draincloud/draincloud-core/internal/resolve_dispatcher"
"git.optclblast.xyz/draincloud/draincloud-core/internal/storage"
"github.com/gin-gonic/gin"
)
type DrainCloud struct {
mux *gin.Engine
database storage.Database
filesEngine *filesengine.FilesEngine
ginProcessor processor.Processor[gin.HandlerFunc]
}
func New(
database storage.Database,
filesEngine *filesengine.FilesEngine,
) *DrainCloud {
mux := gin.Default()
dispatcher := resolvedispatcher.New()
d := &DrainCloud{
database: database,
filesEngine: filesEngine,
ginProcessor: processor.NewGinProcessor(database, dispatcher),
}
// Built-in auth component of DrainCloud-Core
authGroup := mux.Group("/auth")
{
// authGroup.POST("/register", d.Register)
authGroup.POST("/register", d.ginProcessor.Process(
handlers.NewRegisterHandler(database),
))
authGroup.POST("/logon", d.Login)
}
filesGroup := mux.Group("/files")
{
filesGroup.POST("/upload", d.UploadFile)
}
d.mux = mux
return d
}
func (d *DrainCloud) Run(ctx context.Context) error {
return d.mux.Run()
}
func writeError(ctx *gin.Context, err error) {
switch {
case errors.Is(err, ErrorAccessDenied):
ctx.JSON(http.StatusInternalServerError, domain.ErrorJson{
Code: http.StatusForbidden,
Message: err.Error(),
})
case errors.Is(err, ErrorSessionExpired):
ctx.JSON(http.StatusInternalServerError, domain.ErrorJson{
Code: http.StatusForbidden,
Message: err.Error(),
})
default:
ctx.JSON(http.StatusInternalServerError, domain.ErrorJson{
Code: http.StatusInternalServerError,
Message: "Internal Error",
})
}
}