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:
+12
-3
@@ -4,9 +4,11 @@ Copyright © 2025 Hazem Krimi me@hazemkrimi.tech
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/hazemKrimi/crimson-vault/internal/api"
|
"github.com/hazemKrimi/crimson-vault/internal/api"
|
||||||
|
"github.com/hazemKrimi/crimson-vault/internal/lib"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,8 +25,17 @@ to quickly create a Cobra application.`,
|
|||||||
// Uncomment the following line if your bare application
|
// Uncomment the following line if your bare application
|
||||||
// has an action associated with it:
|
// has an action associated with it:
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
server := api.API{}
|
dir, err := lib.GetConfigDirectory()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
server := api.API{ConfigDirectory: dir}
|
||||||
server.Initialize()
|
server.Initialize()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -50,5 +61,3 @@ func init() {
|
|||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+9
-3
@@ -1,13 +1,18 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/hazemKrimi/crimson-vault/internal/models"
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
|
|
||||||
|
"github.com/hazemKrimi/crimson-vault/internal/lib"
|
||||||
|
"github.com/hazemKrimi/crimson-vault/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type API struct {
|
type API struct {
|
||||||
|
ConfigDirectory string
|
||||||
instance *echo.Echo
|
instance *echo.Echo
|
||||||
db *models.DB
|
db *models.DB
|
||||||
}
|
}
|
||||||
@@ -20,7 +25,7 @@ func (api *API) Initialize() {
|
|||||||
ech := echo.New()
|
ech := echo.New()
|
||||||
ech.Validator = &CustomValidator{validator: validator}
|
ech.Validator = &CustomValidator{validator: validator}
|
||||||
|
|
||||||
db.Connect()
|
db.Connect(api.ConfigDirectory)
|
||||||
db.MigrateClients()
|
db.MigrateClients()
|
||||||
db.MigrateUsers()
|
db.MigrateUsers()
|
||||||
|
|
||||||
@@ -28,8 +33,9 @@ func (api *API) Initialize() {
|
|||||||
api.db = db
|
api.db = db
|
||||||
|
|
||||||
api.ClientRoutes()
|
api.ClientRoutes()
|
||||||
|
api.UserRoutes()
|
||||||
api.instance.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
api.instance.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||||
AllowOrigins: []string{"*"},
|
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() {
|
func (api *API) ClientRoutes() {
|
||||||
clients := api.instance.Group("/clients")
|
clients := api.instance.Group("/clients")
|
||||||
users := api.instance.Group("/users")
|
|
||||||
|
|
||||||
clients.GET("/", api.GetAllClientsHandler)
|
clients.GET("/", api.GetAllClientsHandler)
|
||||||
clients.POST("/", api.CreateClientHandler)
|
clients.POST("/", api.CreateClientHandler)
|
||||||
clients.GET("/:id", api.GetClientHandler)
|
clients.GET("/:id", api.GetClientHandler)
|
||||||
clients.PUT("/:id", api.UpdateClientHandler)
|
clients.PUT("/:id", api.UpdateClientHandler)
|
||||||
clients.DELETE("/:id", api.DeleteClientHandler)
|
clients.DELETE("/:id", api.DeleteClientHandler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) UserRoutes() {
|
||||||
|
users := api.instance.Group("/users")
|
||||||
|
|
||||||
users.GET("/", api.GetAllUsersHandler)
|
users.GET("/", api.GetAllUsersHandler)
|
||||||
users.POST("/", api.CreateUserHandler)
|
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!")
|
return context.String(http.StatusBadRequest, "Uploaded file is not a valid image!")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.MkdirAll(user.Username, os.ModePerm)
|
if err := os.MkdirAll(filepath.Join(api.ConfigDirectory, user.Username), os.ModePerm); err != nil {
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
||||||
return context.String(http.StatusInternalServerError, "Unexpected error while updating logo for User!")
|
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 {
|
if err != nil {
|
||||||
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
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 (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -11,8 +12,8 @@ type DB struct {
|
|||||||
instance *gorm.DB
|
instance *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wrapper *DB) Connect() {
|
func (wrapper *DB) Connect(configDir string) {
|
||||||
db, err := gorm.Open(sqlite.Open("crimson_vault.db"), &gorm.Config{})
|
db, err := gorm.Open(sqlite.Open(filepath.Join(configDir, "crimson_vault.db")), &gorm.Config{})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user