mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
chore: setup some defaults
This commit is contained in:
+11
-5
@@ -1,15 +1,20 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/hazemKrimi/crimson-vault/internal/models"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
|
||||
"github.com/hazemKrimi/crimson-vault/internal/lib"
|
||||
"github.com/hazemKrimi/crimson-vault/internal/models"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
instance *echo.Echo
|
||||
db *models.DB
|
||||
ConfigDirectory string
|
||||
instance *echo.Echo
|
||||
db *models.DB
|
||||
}
|
||||
|
||||
func (api *API) Initialize() {
|
||||
@@ -20,7 +25,7 @@ func (api *API) Initialize() {
|
||||
ech := echo.New()
|
||||
ech.Validator = &CustomValidator{validator: validator}
|
||||
|
||||
db.Connect()
|
||||
db.Connect(api.ConfigDirectory)
|
||||
db.MigrateClients()
|
||||
db.MigrateUsers()
|
||||
|
||||
@@ -28,8 +33,9 @@ func (api *API) Initialize() {
|
||||
api.db = db
|
||||
|
||||
api.ClientRoutes()
|
||||
api.UserRoutes()
|
||||
api.instance.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||
AllowOrigins: []string{"*"},
|
||||
}))
|
||||
api.instance.Logger.Fatal(api.instance.Start(":5000"))
|
||||
api.instance.Logger.Fatal(api.instance.Start(fmt.Sprintf(":%d", lib.DEFAULT_PORT)))
|
||||
}
|
||||
|
||||
@@ -4,13 +4,16 @@ import "github.com/labstack/echo/v4/middleware"
|
||||
|
||||
func (api *API) ClientRoutes() {
|
||||
clients := api.instance.Group("/clients")
|
||||
users := api.instance.Group("/users")
|
||||
|
||||
clients.GET("/", api.GetAllClientsHandler)
|
||||
clients.POST("/", api.CreateClientHandler)
|
||||
clients.GET("/:id", api.GetClientHandler)
|
||||
clients.PUT("/:id", api.UpdateClientHandler)
|
||||
clients.DELETE("/:id", api.DeleteClientHandler)
|
||||
}
|
||||
|
||||
func (api *API) UserRoutes() {
|
||||
users := api.instance.Group("/users")
|
||||
|
||||
users.GET("/", api.GetAllUsersHandler)
|
||||
users.POST("/", api.CreateUserHandler)
|
||||
|
||||
@@ -199,14 +199,12 @@ func (api *API) UpdateUserLogoHandler(context echo.Context) error {
|
||||
return context.String(http.StatusBadRequest, "Uploaded file is not a valid image!")
|
||||
}
|
||||
|
||||
err = os.MkdirAll(user.Username, os.ModePerm)
|
||||
|
||||
if err != nil {
|
||||
if err := os.MkdirAll(filepath.Join(api.ConfigDirectory, user.Username), os.ModePerm); err != nil {
|
||||
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error while updating logo for User!")
|
||||
}
|
||||
|
||||
path, err := filepath.Abs(filepath.Join(user.Username, fmt.Sprintf("logo%s", ext)))
|
||||
path, err := filepath.Abs(filepath.Join(api.ConfigDirectory, user.Username, fmt.Sprintf("logo%s", ext)))
|
||||
|
||||
if err != nil {
|
||||
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package lib
|
||||
|
||||
const DEFAULT_PORT int = 6900
|
||||
const DEFAULT_CONFIG_DIRECTORY string = ".local/state/crimson-vault"
|
||||
@@ -0,0 +1,18 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func GetConfigDirectory() (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
config, err := filepath.Abs(filepath.Join(home, DEFAULT_CONFIG_DIRECTORY))
|
||||
|
||||
return config, nil
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"log"
|
||||
"path/filepath"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
@@ -11,8 +12,8 @@ type DB struct {
|
||||
instance *gorm.DB
|
||||
}
|
||||
|
||||
func (wrapper *DB) Connect() {
|
||||
db, err := gorm.Open(sqlite.Open("crimson_vault.db"), &gorm.Config{})
|
||||
func (wrapper *DB) Connect(configDir string) {
|
||||
db, err := gorm.Open(sqlite.Open(filepath.Join(configDir, "crimson_vault.db")), &gorm.Config{})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user