This commit is contained in:
Sky Johnson 2025-03-06 21:39:36 -06:00
parent af8fd397ea
commit c48ab8c433
2 changed files with 64 additions and 0 deletions

32
core/utils/dirs.go Normal file
View File

@ -0,0 +1,32 @@
package utils
import (
"os"
"path/filepath"
)
// EnsureDir checks if a directory exists and creates it if it doesn't.
// Returns any error encountered during directory creation.
func EnsureDir(path string) error {
// Clean the path to handle any malformed input
path = filepath.Clean(path)
// Check if the directory exists
info, err := os.Stat(path)
// If no error, check if it's a directory
if err == nil {
if info.IsDir() {
return nil // Directory already exists
}
return os.ErrExist // Path exists but is not a directory
}
// If the error is not that the path doesn't exist, return it
if !os.IsNotExist(err) {
return err
}
// Create the directory with default permissions (0755)
return os.MkdirAll(path, 0755)
}

View File

@ -3,6 +3,8 @@ package main
import (
"git.sharkk.net/Sky/Moonshark/core/config"
"git.sharkk.net/Sky/Moonshark/core/logger"
"git.sharkk.net/Sky/Moonshark/core/routers"
"git.sharkk.net/Sky/Moonshark/core/utils"
)
func main() {
@ -23,6 +25,36 @@ func main() {
// Get port from config or use default
port := cfg.GetInt("port", 3117)
// Initialize routers
routesDir := cfg.GetString("routes_dir", "./routes")
staticDir := cfg.GetString("static_dir", "./static")
// Ensure the Lua routes directory exists
err = utils.EnsureDir(routesDir)
if err != nil {
log.Fatal("Routes directory doesn't exist, and could not create it. %v", err)
}
// Ensure the static directory exists
err = utils.EnsureDir(staticDir)
if err != nil {
log.Fatal("Static directory doesn't exist, and could not create it. %v", err)
}
// Initialize Lua router for dynamic routes
_, err = routers.NewLuaRouter(routesDir)
if err != nil {
log.Fatal("Failed to initialize Lua router: %v", err)
}
log.Info("Lua router initialized with routes from %s", routesDir)
// Initialize static file router
_, err = routers.NewStaticRouter(staticDir)
if err != nil {
log.Fatal("Failed to initialize static router: %v", err)
}
log.Info("Static router initialized with files from %s", staticDir)
// Output the port number
log.Info("Moonshark server listening on port %d", port)
}