draincloud-core/internal/app/app.go

78 lines
1.7 KiB
Go
Raw 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"
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-10-10 21:36:51 +00:00
"git.optclblast.xyz/draincloud/draincloud-core/internal/logger"
"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-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()
2024-10-20 09:12:54 +00:00
d := &DrainCloud{
2024-11-23 08:52:06 +00:00
database: database,
filesEngine: filesEngine,
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")
{
authGroup.POST("/register", d.Register)
2024-09-30 22:21:37 +00:00
authGroup.POST("/login", d.Login)
2024-11-23 08:52:06 +00:00
}
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())
}
})
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
}