change directory behavior
This commit is contained in:
parent
d50081ef55
commit
04450779f2
@ -99,7 +99,7 @@ func (s *Server) handleRequest(ctx *fasthttp.RequestCtx) {
|
||||
}
|
||||
}
|
||||
|
||||
// processRequest processes the actual request
|
||||
// In server.go, modify the processRequest method
|
||||
func (s *Server) processRequest(ctx *fasthttp.RequestCtx) {
|
||||
method := string(ctx.Method())
|
||||
path := string(ctx.Path())
|
||||
@ -130,10 +130,12 @@ func (s *Server) processRequest(ctx *fasthttp.RequestCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
if s.staticRouter != nil {
|
||||
if _, found := s.staticRouter.Match(path); found {
|
||||
s.staticRouter.ServeHTTP(ctx)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.SetContentType("text/html; charset=utf-8")
|
||||
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
||||
|
@ -4,13 +4,13 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"Moonshark/core/http"
|
||||
"Moonshark/core/routers"
|
||||
"Moonshark/core/runner"
|
||||
"Moonshark/core/sessions"
|
||||
"Moonshark/core/utils"
|
||||
"Moonshark/core/utils/config"
|
||||
"Moonshark/core/utils/logger"
|
||||
"Moonshark/core/watchers"
|
||||
@ -28,35 +28,37 @@ type Moonshark struct {
|
||||
cleanupFuncs []func() error
|
||||
}
|
||||
|
||||
// dirExists checks if a directory exists without creating it
|
||||
func dirExists(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return info.IsDir()
|
||||
}
|
||||
|
||||
// NewMoonshark creates a new Moonshark server instance
|
||||
func NewMoonshark(configPath string) (*Moonshark, error) {
|
||||
// Initialize server struct
|
||||
server := &Moonshark{}
|
||||
|
||||
// Load configuration
|
||||
if err := server.loadConfig(configPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Configure logging
|
||||
server.setupLogging()
|
||||
|
||||
// Initialize routers
|
||||
if err := server.initRouters(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Initialize Lua runner
|
||||
if err := server.initRunner(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Set up file watchers
|
||||
if err := server.setupWatchers(); err != nil {
|
||||
logger.Warning("Error setting up watchers: %v", err)
|
||||
}
|
||||
|
||||
// Create HTTP server
|
||||
server.HTTPServer = http.New(
|
||||
server.LuaRouter,
|
||||
server.StaticRouter,
|
||||
@ -74,7 +76,6 @@ func NewMoonshark(configPath string) (*Moonshark, error) {
|
||||
func (s *Moonshark) loadConfig(configPath string) error {
|
||||
var err error
|
||||
|
||||
// Load configuration from file
|
||||
s.Config, err = config.Load(configPath)
|
||||
if err != nil {
|
||||
logger.Warning("Wipeout! Couldn't load config file: %v", err)
|
||||
@ -87,7 +88,6 @@ func (s *Moonshark) loadConfig(configPath string) error {
|
||||
|
||||
// setupLogging configures the logger based on config settings
|
||||
func (s *Moonshark) setupLogging() {
|
||||
// Set log level from config
|
||||
switch s.Config.Server.LogLevel {
|
||||
case "warn":
|
||||
logger.SetLevel(logger.LevelWarning)
|
||||
@ -101,7 +101,6 @@ func (s *Moonshark) setupLogging() {
|
||||
logger.SetLevel(logger.LevelInfo)
|
||||
}
|
||||
|
||||
// Set debug mode if configured
|
||||
if s.Config.Server.Debug {
|
||||
logger.EnableDebug() // Force debug logs regardless of level
|
||||
logger.Debug("Debug mode is ready to party, bro")
|
||||
@ -110,13 +109,9 @@ func (s *Moonshark) setupLogging() {
|
||||
|
||||
// initRouters initializes the Lua and static routers
|
||||
func (s *Moonshark) initRouters() error {
|
||||
// Ensure directories exist
|
||||
if err := utils.EnsureDir(s.Config.Dirs.Routes); err != nil {
|
||||
return fmt.Errorf("routes directory doesn't exist and could not be created: %v", err)
|
||||
}
|
||||
|
||||
if err := utils.EnsureDir(s.Config.Dirs.Static); err != nil {
|
||||
return fmt.Errorf("static directory doesn't exist and could not be created: %v", err)
|
||||
if !dirExists(s.Config.Dirs.Routes) {
|
||||
logger.Fatal("Routes directory doesn't exist: %s", s.Config.Dirs.Routes)
|
||||
return fmt.Errorf("routes directory doesn't exist: %s", s.Config.Dirs.Routes)
|
||||
}
|
||||
|
||||
// Initialize Lua router for dynamic routes
|
||||
@ -141,36 +136,34 @@ func (s *Moonshark) initRouters() error {
|
||||
}
|
||||
logger.Info("Lua router is stoked and riding routes from %s", s.Config.Dirs.Routes)
|
||||
|
||||
// Initialize static file router
|
||||
if dirExists(s.Config.Dirs.Static) {
|
||||
s.StaticRouter, err = routers.NewStaticRouter(s.Config.Dirs.Static)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize static router: %v", err)
|
||||
}
|
||||
logger.Info("Static router catching waves with files from %s", s.Config.Dirs.Static)
|
||||
s.StaticRouter.EnableDebugLog()
|
||||
} else {
|
||||
logger.Warning("Static directory doesn't exist: %s - static file serving disabled", s.Config.Dirs.Static)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// initRunner initializes the Lua runner
|
||||
func (s *Moonshark) initRunner() error {
|
||||
// Ensure override directory exists
|
||||
if err := utils.EnsureDir(s.Config.Dirs.Override); err != nil {
|
||||
logger.Warning("Override directory doesn't exist and could not be created: %v", err)
|
||||
s.Config.Dirs.Override = "" // Disable overrides if directory can't be created
|
||||
if !dirExists(s.Config.Dirs.Override) {
|
||||
logger.Warning("Override directory doesn't exist: %s", s.Config.Dirs.Override)
|
||||
s.Config.Dirs.Override = ""
|
||||
}
|
||||
|
||||
// Ensure lib directories exist
|
||||
for _, dir := range s.Config.Dirs.Libs {
|
||||
if err := utils.EnsureDir(dir); err != nil {
|
||||
logger.Warning("Lib directory doesn't exist and could not be created: %v", err)
|
||||
if !dirExists(dir) {
|
||||
logger.Warning("Lib directory doesn't exist: %s", dir)
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize session manager
|
||||
sessionManager := sessions.GlobalSessionManager
|
||||
|
||||
// Configure session cookies
|
||||
sessionManager.SetCookieOptions(
|
||||
"MoonsharkSID", // name
|
||||
"/", // path
|
||||
@ -180,13 +173,11 @@ func (s *Moonshark) initRunner() error {
|
||||
86400, // maxAge (1 day)
|
||||
)
|
||||
|
||||
// Set up runner options
|
||||
runnerOpts := []runner.RunnerOption{
|
||||
runner.WithPoolSize(s.Config.Runner.PoolSize),
|
||||
runner.WithLibDirs(s.Config.Dirs.Libs...),
|
||||
}
|
||||
|
||||
// Initialize the runner
|
||||
var err error
|
||||
s.LuaRunner, err = runner.NewRunner(runnerOpts...)
|
||||
if err != nil {
|
||||
|
@ -35,7 +35,6 @@ func NewStaticRouter(rootDir string) (*StaticRouter, error) {
|
||||
return nil, errors.New("root path is not a directory")
|
||||
}
|
||||
|
||||
// Create the FS handler with optimized settings
|
||||
fs := &fasthttp.FS{
|
||||
Root: rootDir,
|
||||
IndexNames: []string{"index.html"},
|
||||
@ -50,14 +49,13 @@ func NewStaticRouter(rootDir string) (*StaticRouter, error) {
|
||||
|
||||
r := &StaticRouter{
|
||||
fs: fs,
|
||||
urlPrefix: "/static", // Default prefix
|
||||
urlPrefix: "/",
|
||||
rootDir: rootDir,
|
||||
log: false,
|
||||
useBrotli: true,
|
||||
useZstd: true,
|
||||
}
|
||||
|
||||
// Set up the path rewrite based on the prefix
|
||||
r.updatePathRewrite()
|
||||
|
||||
return r, nil
|
||||
@ -78,7 +76,6 @@ func (r *StaticRouter) SetCompression(useGzip, useBrotli, useZstd bool) {
|
||||
r.useBrotli = useBrotli
|
||||
r.useZstd = useZstd
|
||||
|
||||
// Update handler to reflect changes
|
||||
r.fsHandler = r.fs.NewRequestHandler()
|
||||
}
|
||||
|
||||
@ -117,7 +114,6 @@ func (r *StaticRouter) DisableDebugLog() {
|
||||
func (r *StaticRouter) ServeHTTP(ctx *fasthttp.RequestCtx) {
|
||||
path := string(ctx.Path())
|
||||
|
||||
// Check if path starts with the prefix
|
||||
if !strings.HasPrefix(path, r.urlPrefix) {
|
||||
ctx.NotFound()
|
||||
return
|
||||
@ -127,21 +123,17 @@ func (r *StaticRouter) ServeHTTP(ctx *fasthttp.RequestCtx) {
|
||||
logger.Debug("[StaticRouter] Serving: %s", path)
|
||||
}
|
||||
|
||||
// Handle the request with the FS handler
|
||||
r.fsHandler(ctx)
|
||||
}
|
||||
|
||||
// Match finds a file path for the given URL path
|
||||
func (r *StaticRouter) Match(urlPath string) (string, bool) {
|
||||
// Check if path starts with the prefix
|
||||
if !strings.HasPrefix(urlPath, r.urlPrefix) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Strip prefix
|
||||
urlPath = strings.TrimPrefix(urlPath, r.urlPrefix)
|
||||
|
||||
// Make sure path starts with /
|
||||
if !strings.HasPrefix(urlPath, "/") {
|
||||
urlPath = "/" + urlPath
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ func (s *Sandbox) AddModule(name string, module any) {
|
||||
// Setup initializes the sandbox in a Lua state
|
||||
func (s *Sandbox) Setup(state *luajit.State, verbose bool) error {
|
||||
if verbose {
|
||||
logger.Server("Setting up sandbox...")
|
||||
logger.Debug("Setting up sandbox...")
|
||||
}
|
||||
|
||||
if err := loadSandboxIntoState(state, verbose); err != nil {
|
||||
@ -77,7 +77,7 @@ func (s *Sandbox) Setup(state *luajit.State, verbose bool) error {
|
||||
s.mu.RUnlock()
|
||||
|
||||
if verbose {
|
||||
logger.Server("Sandbox setup complete")
|
||||
logger.Debug("Sandbox setup complete")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
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 {
|
||||
path = filepath.Clean(path)
|
||||
|
||||
info, err := os.Stat(path)
|
||||
|
||||
if err == nil {
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
return os.ErrExist
|
||||
}
|
||||
|
||||
if !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.MkdirAll(path, 0755)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user