-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy paththink.go
101 lines (80 loc) · 2.04 KB
/
think.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
package thinkgo
import (
"fmt"
"github.com/forgoer/thinkgo/log"
"github.com/forgoer/thinkgo/log/record"
"net/http"
"time"
"github.com/forgoer/thinkgo/config"
"github.com/forgoer/thinkgo/helper"
"github.com/forgoer/thinkgo/router"
"github.com/forgoer/thinkgo/think"
)
type registerRouteFunc func(route *router.Route)
type registerConfigFunc func()
type Think struct {
App *think.Application
handlers []think.HandlerFunc
}
// New Create The Application
func New() *Think {
application := think.NewApplication()
application.Logger = log.NewLogger("develop", record.DEBUG)
t := &Think{
App: application,
}
//t.bootView()
t.bootRoute()
return t
}
// RegisterRoute Register Route
func (th *Think) RegisterRoute(register registerRouteFunc) {
route := th.App.GetRoute()
defer route.Register()
register(route)
}
// RegisterConfig Register Config
func (th *Think) RegisterConfig(register registerConfigFunc) {
register()
}
// RegisterConfig Register Config
func (th *Think) RegisterHandler(handler think.HandlerFunc) {
th.handlers = append(th.handlers, handler)
}
// Run thinkgo application.
// Run() default run on HttpPort
// Run("localhost")
// Run(":9011")
// Run("127.0.0.1:9011")
func (th *Think) Run(params ...string) {
var err error
var endRunning = make(chan bool, 1)
var addrs = helper.ParseAddr(params...)
// register route handler
th.RegisterHandler(think.NewRouteHandler)
pipeline := NewPipeline()
for _, h := range th.handlers {
pipeline.Pipe(h(th.App))
}
th.App.Logger.Debug("\r\nLoaded routes:\r\n%s",string(th.App.GetRoute().Dump()))
go func() {
th.App.Logger.Debug("ThinkGo server running on http://%s", addrs)
err = http.ListenAndServe(addrs, pipeline)
if err != nil {
fmt.Println(err.Error())
time.Sleep(100 * time.Microsecond)
endRunning <- true
}
}()
<-endRunning
}
//func (th *Think) bootView() {
// v := view.NewView()
// v.SetPath(config.View.Path)
// th.App.RegisterView(v)
//}
func (th *Think) bootRoute() {
r := router.New()
r.Statics(config.Route.Static)
th.App.RegisterRoute(r)
}