utils
This commit is contained in:
parent
af8fd397ea
commit
c48ab8c433
32
core/utils/dirs.go
Normal file
32
core/utils/dirs.go
Normal 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)
|
||||||
|
}
|
32
moonshark.go
32
moonshark.go
|
@ -3,6 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"git.sharkk.net/Sky/Moonshark/core/config"
|
"git.sharkk.net/Sky/Moonshark/core/config"
|
||||||
"git.sharkk.net/Sky/Moonshark/core/logger"
|
"git.sharkk.net/Sky/Moonshark/core/logger"
|
||||||
|
"git.sharkk.net/Sky/Moonshark/core/routers"
|
||||||
|
"git.sharkk.net/Sky/Moonshark/core/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -23,6 +25,36 @@ func main() {
|
||||||
// Get port from config or use default
|
// Get port from config or use default
|
||||||
port := cfg.GetInt("port", 3117)
|
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
|
// Output the port number
|
||||||
log.Info("Moonshark server listening on port %d", port)
|
log.Info("Moonshark server listening on port %d", port)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user