package app import ( "context" "errors" "net/http" "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/logger" "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 } func New( database storage.Database, filesEngine *filesengine.FilesEngine, ) *DrainCloud { mux := gin.Default() d := &DrainCloud{ database: database, filesEngine: filesEngine, } // Built-in auth component of DrainCloud-Core authGroup := mux.Group("/auth") { authGroup.POST("/register", d.Register) authGroup.POST("/login", d.Login) } filesGroup := mux.Group("/files") { filesGroup.POST("/upload", func(ctx *gin.Context) { err := d.uploadFile(ctx) if err != nil { logger.Error(ctx.Request.Context(), "", logger.Err(err)) ctx.String(400, err.Error()) } }) } 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", }) } }