From c48ab8c4336d9550a7faae4648efdd1220ff5a6d Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Thu, 6 Mar 2025 21:39:36 -0600 Subject: [PATCH] utils --- core/utils/dirs.go | 32 ++++++++++++++++++++++++++++++++ moonshark.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 core/utils/dirs.go diff --git a/core/utils/dirs.go b/core/utils/dirs.go new file mode 100644 index 0000000..c4def88 --- /dev/null +++ b/core/utils/dirs.go @@ -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) +} diff --git a/moonshark.go b/moonshark.go index 9db73aa..fac0ef7 100644 --- a/moonshark.go +++ b/moonshark.go @@ -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) }