-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
111 lines (97 loc) · 2.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"flag"
"fmt"
"net/http"
"path/filepath"
"time"
"github.com/Raggaer/bison/app/config"
"github.com/Raggaer/bison/app/controllers"
"github.com/Raggaer/bison/app/lua"
"github.com/Raggaer/bison/app/router"
"github.com/Raggaer/bison/app/template"
"github.com/buaazp/fasthttprouter"
cache "github.com/patrickmn/go-cache"
"github.com/valyala/fasthttp"
)
func main() {
var controllersPath string
flag.StringVar(&controllersPath, "controllers", "", "Filepath for your controllers folder. Default 'app/controllers'")
var configPath string
flag.StringVar(&configPath, "config", "", "Filepath for your config file. Default 'app/config/config.lua'")
var viewsPath string
flag.StringVar(&viewsPath, "views", "", "Filepath for your views folder. Default 'app/views'")
var routerPath string
flag.StringVar(&routerPath, "router", "", "Filepath for your router file. Default 'app/router/router.lua'")
flag.Parse()
// Load config file
if configPath == "" {
configPath = filepath.Join("app", "config", "config.lua")
}
config, err := config.LoadConfig(configPath)
if err != nil {
fmt.Printf("Unable to load config.lua file: %v", err)
return
}
// Compile all lua files
if controllersPath == "" {
controllersPath = filepath.Join("app", "controllers")
}
files, err := lua.CompileFiles(controllersPath)
if err != nil {
fmt.Printf("Unable to compile controller files: %v", err)
return
}
cacheStorage := cache.New(time.Minute*5, time.Minute*10)
// Load all templates
if viewsPath == "" {
viewsPath = filepath.Join("app", "views")
}
tpl, err := template.LoadTemplates(viewsPath, &template.TemplateFuncData{
Config: config,
Cache: cacheStorage,
Files: files,
ControllersPath: controllersPath,
})
if err != nil {
fmt.Printf("Unable to load views files: %v", err)
return
}
// Create fasthttp router
if routerPath == "" {
routerPath = filepath.Join("app", "router", "router.lua")
}
r := fasthttprouter.New()
routes, err := router.LoadRoutes(routerPath)
if err != nil {
fmt.Printf("Unable to load routes: %v", err)
return
}
// Create fasthttp server
handler := &controllers.Handler{
Config: config,
Routes: routes,
Files: files,
Tpl: tpl,
Cache: cacheStorage,
ControllersPath: controllersPath,
RouterPath: routerPath,
ViewsPath: viewsPath,
ConfigPath: configPath,
}
// Register routes
for _, rx := range routes {
if rx.Method == http.MethodGet {
r.GET(rx.Path, handler.MainRoute)
}
if rx.Method == http.MethodPost {
r.POST(rx.Path, handler.MainRoute)
}
}
if config.DevMode {
fmt.Println("Running development mode - bison listening on address '" + config.Address + "'")
} else {
fmt.Println("bison listening on address '" + config.Address + "'")
}
fasthttp.ListenAndServe(config.Address, r.Handler)
}