draincloud-core/internal/app/app.go

82 lines
2.0 KiB
Go
Raw Permalink Normal View History

2024-09-27 22:37:58 +00:00
package app
import (
2024-10-10 21:36:51 +00:00
"context"
2024-11-23 08:52:06 +00:00
"errors"
2024-09-30 22:21:37 +00:00
"net/http"
"git.optclblast.xyz/draincloud/draincloud-core/internal/app/handlers"
2024-10-10 21:36:51 +00:00
"git.optclblast.xyz/draincloud/draincloud-core/internal/domain"
2024-11-23 08:52:06 +00:00
filesengine "git.optclblast.xyz/draincloud/draincloud-core/internal/files_engine"
2024-12-29 23:15:44 +00:00
"git.optclblast.xyz/draincloud/draincloud-core/internal/processor"
resolvedispatcher "git.optclblast.xyz/draincloud/draincloud-core/internal/resolve_dispatcher"
2024-10-10 21:36:51 +00:00
"git.optclblast.xyz/draincloud/draincloud-core/internal/storage"
2024-09-27 22:37:58 +00:00
"github.com/gin-gonic/gin"
)
type DrainCloud struct {
2024-11-23 08:52:06 +00:00
mux *gin.Engine
database storage.Database
filesEngine *filesengine.FilesEngine
2024-12-29 23:15:44 +00:00
ginProcessor processor.Processor[gin.HandlerFunc]
2024-09-27 22:37:58 +00:00
}
2024-10-20 09:12:54 +00:00
func New(
2024-11-23 08:52:06 +00:00
database storage.Database,
filesEngine *filesengine.FilesEngine,
2024-10-20 09:12:54 +00:00
) *DrainCloud {
2024-09-27 22:37:58 +00:00
mux := gin.Default()
dispatcher := resolvedispatcher.New()
2024-10-20 09:12:54 +00:00
d := &DrainCloud{
database: database,
filesEngine: filesEngine,
ginProcessor: processor.NewGinProcessor(database, dispatcher),
2024-10-20 09:12:54 +00:00
}
2024-09-27 22:37:58 +00:00
2024-10-20 09:12:54 +00:00
// Built-in auth component of DrainCloud-Core
2024-09-27 22:37:58 +00:00
authGroup := mux.Group("/auth")
{
2024-12-29 23:15:44 +00:00
// authGroup.POST("/register", d.Register)
authGroup.POST("/register", d.ginProcessor.Process(
handlers.NewRegisterHandler(database),
))
2024-12-15 16:56:03 +00:00
authGroup.POST("/logon", d.Login)
2024-11-23 08:52:06 +00:00
}
filesGroup := mux.Group("/files")
{
2024-11-29 05:23:56 +00:00
filesGroup.POST("/upload", d.UploadFile)
2024-09-27 22:37:58 +00:00
}
d.mux = mux
return d
}
2024-10-10 21:36:51 +00:00
func (d *DrainCloud) Run(ctx context.Context) error {
return d.mux.Run()
}
2024-11-23 08:52:06 +00:00
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(),
2024-09-30 22:21:37 +00:00
})
2024-11-23 08:52:06 +00:00
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",
2024-09-30 22:21:37 +00:00
})
}
2024-09-27 22:37:58 +00:00
}