Skip to content
Permalink
Browse files

Add flags, fix controllers compiling

  • Loading branch information...
Raggaer committed Jun 10, 2019
1 parent cb29bf2 commit c36923a94a28ed49411f4e3da8086ae21679d8fa
Showing with 30 additions and 19 deletions.
  1. +13 −9 app/controllers/controller.go
  2. +17 −10 main.go
@@ -18,30 +18,34 @@ import (

// Handler main fasthttp handler
type Handler struct {
Config *config.Config
Routes []*router.Route
Files map[string]*glua.FunctionProto
Tpl *tpl.Template
Cache *cache.Cache
Config *config.Config
Routes []*router.Route
Files map[string]*glua.FunctionProto
Tpl *tpl.Template
Cache *cache.Cache
ControllersPath string
RouterPath string
ConfigPath string
ViewsPath string
}

// MainRoute handles all http requests
func (h *Handler) MainRoute(ctx *fasthttp.RequestCtx) {
// If we are running under development mode reload stuff
if h.Config.DevMode {
routes, err := router.LoadRoutes(filepath.Join("app", "router", "router.lua"))
routes, err := router.LoadRoutes(h.RouterPath)
if err != nil {
ctx.Error("Unable to reload routes", 500)
return
}
h.Routes = routes
luaFiles, err := lua.CompileFiles(filepath.Join("app", "controllers"))
luaFiles, err := lua.CompileFiles(h.ControllersPath)
if err != nil {
ctx.Error("Unable to reload controllers", 500)
return
}
h.Files = luaFiles
tpl, err := template.LoadTemplates(filepath.Join("app", "views"), &template.TemplateFuncData{
tpl, err := template.LoadTemplates(h.ViewsPath, &template.TemplateFuncData{
Config: h.Config,
Files: h.Files,
})
@@ -63,7 +67,7 @@ func (h *Handler) MainRoute(ctx *fasthttp.RequestCtx) {
route := router.RetrieveCurrentRoute(params, string(ctx.Method()), string(ctx.Path()), h.Routes)

// Retrieve compiled file for this route
p := filepath.Join("app", "controllers", route.File)
p := filepath.Join(h.ControllersPath, route.File)
if h.Config.TestMode {
p = filepath.Join("controllers", route.File)
}
27 main.go
@@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"log"
"net/http"
"path/filepath"
"time"
@@ -37,7 +36,8 @@ func main() {
}
config, err := config.LoadConfig(configPath)
if err != nil {
log.Fatal(err)
fmt.Printf("Unable to load config.lua file: %v", err)
return
}

// Compile all lua files
@@ -46,7 +46,8 @@ func main() {
}
files, err := lua.CompileFiles(controllersPath)
if err != nil {
log.Fatal(err)
fmt.Printf("Unable to compile controller files: %v", err)
return
}

// Load all templates
@@ -58,7 +59,8 @@ func main() {
Files: files,
})
if err != nil {
log.Fatal(err)
fmt.Printf("Unable to load views files: %v", err)
return
}

// Create fasthttp router
@@ -68,16 +70,21 @@ func main() {
r := fasthttprouter.New()
routes, err := router.LoadRoutes(routerPath)
if err != nil {
log.Fatal(err)
fmt.Printf("Unable to load routes: %v", err)
return
}

// Create fasthttp server
handler := &controllers.Handler{
Config: config,
Routes: routes,
Files: files,
Tpl: tpl,
Cache: cache.New(time.Minute*5, time.Minute*10),
Config: config,
Routes: routes,
Files: files,
Tpl: tpl,
Cache: cache.New(time.Minute*5, time.Minute*10),
ControllersPath: controllersPath,
RouterPath: routerPath,
ViewsPath: viewsPath,
ConfigPath: configPath,
}

for _, rx := range routes {

0 comments on commit c36923a

Please sign in to comment.
You can’t perform that action at this time.