89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"Moonshark/config"
|
|
"Moonshark/http"
|
|
"Moonshark/logger"
|
|
"Moonshark/metadata"
|
|
"Moonshark/router"
|
|
"Moonshark/runner"
|
|
"Moonshark/sessions"
|
|
"bytes"
|
|
"flag"
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.sharkk.net/Go/Color"
|
|
|
|
fin "git.sharkk.net/Sharkk/Fin"
|
|
"github.com/valyala/fasthttp"
|
|
)
|
|
|
|
var (
|
|
cfg *config.Config // Server config from Fin file
|
|
rtr *router.Router // Lua file router
|
|
rnr *runner.Runner // Lua runner
|
|
svr *fasthttp.Server // FastHTTP server
|
|
pub fasthttp.RequestHandler // Public asset handler
|
|
snm *sessions.SessionManager // Session data manager
|
|
dbg bool // Debug mode flag
|
|
pubPfx []byte // Cached public asset prefix
|
|
)
|
|
|
|
func main() {
|
|
cfgPath := flag.String("config", "config", "Path to Fin config file")
|
|
dbgFlag := flag.Bool("debug", false, "Force debug mode")
|
|
sptPath := flag.String("script", "", "Path to Lua script to execute once")
|
|
flag.Parse()
|
|
|
|
sptMode := *sptPath != ""
|
|
color.SetColors(color.DetectShellColors())
|
|
banner(sptMode)
|
|
|
|
cfg = config.New(fin.LoadFromFile(*cfgPath))
|
|
dbg = *dbgFlag || cfg.Server.Debug
|
|
logger.Debug(dbg)
|
|
if dbg {
|
|
logger.Debugf("Debug logging enabled")
|
|
}
|
|
|
|
svr = http.NewHttpServer(cfg, requestMux, dbg)
|
|
pub = http.NewPublicHandler(cfg.Dirs.Public, cfg.Server.PublicPrefix)
|
|
pubPfx = []byte(cfg.Server.PublicPrefix)
|
|
}
|
|
|
|
// This is the primary request handler mux - determines whether we need to handle a Lua
|
|
// route or if we're serving a static file.
|
|
func requestMux(ctx *fasthttp.RequestCtx) {
|
|
start := time.Now()
|
|
method := ctx.Method()
|
|
path := ctx.Path()
|
|
|
|
// Handle static file request
|
|
if bytes.HasPrefix(path, pubPfx) {
|
|
pub(ctx)
|
|
if cfg.Server.HTTPLogging {
|
|
logger.Request(ctx.Response.StatusCode(), string(method), string(path), time.Since(start))
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
// Print our super-awesome banner with the current version!
|
|
func banner(scriptMode bool) {
|
|
if scriptMode {
|
|
fmt.Println(color.Blue(fmt.Sprintf("Moonshark %s << Script Mode >>", metadata.Version)))
|
|
return
|
|
}
|
|
|
|
banner := `
|
|
_____ _________.__ __
|
|
/ \ ____ ____ ____ / _____/| |__ _____ _______| | __
|
|
/ \ / \ / _ \ / _ \ / \ \_____ \ | | \\__ \\_ __ \ |/ /
|
|
/ Y ( <_> | <_> ) | \/ \| Y \/ __ \| | \/ <
|
|
\____|__ /\____/ \____/|___| /_______ /|___| (____ /__| |__|_ \ %s
|
|
\/ \/ \/ \/ \/ \/
|
|
`
|
|
fmt.Println(color.Blue(fmt.Sprintf(banner, metadata.Version)))
|
|
}
|