-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgot.go
151 lines (118 loc) · 3.9 KB
/
got.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Time-stamp: <[got.go] Elivoa @ Friday, 2015-06-05 23:17:49>
TODO:
- Add Hooks: OnAppStart, AfterAppStart, ...
*/
package got
import (
"fmt"
"net/http"
"github.com/elivoa/got/builtin"
"github.com/elivoa/got/config"
"github.com/elivoa/got/core"
"github.com/elivoa/got/parser"
"github.com/elivoa/got/register"
"github.com/elivoa/got/route"
"github.com/elivoa/got/templates"
"github.com/elivoa/got/utils"
"github.com/gorilla/context"
)
// build phrase. only set config.
var Config *config.Configure
func init() {
Config = config.Config
}
// BuildStart generates Start code and run server.
func BuildStart() {
// register built-in module
config.Config.RegisterModule(builtin.BuiltinModule)
// config.Config.RegisterModulePath(builtin.BuiltinModule.Path(), "BuiltinModule", false)
printRegisteredModulePaths()
// * Generate startup codes.
// generate proton register sourcecode and compile and run.
timer := utils.NewTimer()
fmt.Println("> Generating startup codes...")
// * HackSource
app, err := parser.HackSource(Config.Modules)
if err != nil {
panic(fmt.Sprintf("build error: %v", err.Error()))
}
timer.Log("Generating startup codes Done!")
// Start the server.
// TODO: Make my own startup codes.
app.Port = config.Config.Port
fmt.Println("\n>>>> Run Application")
appcmd := app.Cmd()
appcmd.Run() // run and not return
// >>> process goes out here, to generated main.go
}
func printRegisteredModulePaths() {
// print registered modules.
fmt.Println("> Registered Module paths:")
for _, module := range Config.Modules {
fmt.Printf(" - module: %v.%v\n", module.PackageName, module.VarName)
}
}
// <<< called by generated code, start the server. (called by generated/main.go)
func Start() {
welcome()
// processing modules
fmt.Println("\n!> GOT: Register Modules:")
var startupModuleKey string
var startupModule *core.Module
for key, module := range register.Modules.Map() {
// register startup module later.(the last one)
if module.IsStartupModule {
startupModuleKey = key
startupModule = module
continue
}
fmt.Printf(" [Module] %s\n", key)
if module.Register != nil {
module.Register()
}
}
// register startup module
fmt.Printf(" [Module] %s (Startup)\n", startupModuleKey)
if startupModule.Register != nil {
startupModule.Register()
}
fmt.Println("\n!> GOT: Register static file paths:")
// mapping static paths.
for _, pair := range config.Config.StaticResources {
fmt.Printf(" [Static Path] %s -> %s (dir: %s)\n", pair[0], pair[1], http.Dir(pair[1]))
path := http.StripPrefix(pair[0], http.FileServer(http.Dir(pair[1])))
http.Handle(pair[0], path)
}
// Template initialize
templates.FinalInitialize()
// got url matcher
http.HandleFunc("/", route.RouteHandler)
fmt.Printf("\n>> Server Started. Port:%d\n", Config.Port)
fmt.Printf("%s ❯ ", startupModuleKey)
// The second parameter is to clear gorilla/session to prevent memory leak
http.ListenAndServe(fmt.Sprintf(":%v", Config.Port), context.ClearHandler(http.DefaultServeMux))
}
// welcome print welcome message to screen.
func welcome() {
fmt.Println("")
fmt.Println("``````````````````````````````````````````````````")
fmt.Println("` GOT WebFramework (EARLY BUILD 4) `")
fmt.Println("` `")
fmt.Println("``````````````````````````````````````````````````")
PrintRegistry()
}
// ________________________________________________________________________________
// Print GOT Evnironment
//
func PrintRegistry() {
register.Modules.PrintALL()
fmt.Println("\n---- Pages ---------------------")
register.Pages.PrintALL()
fmt.Println("\n---- Components ---------------------")
register.Components.PrintALL()
fmt.Println("\n---- Mixins ---------------------")
fmt.Println("... no mixins avaliable ...")
fmt.Println("--------------------------------------------------------------------------------")
fmt.Println()
}