-
Notifications
You must be signed in to change notification settings - Fork 0
/
blueberry.go
68 lines (57 loc) · 1.12 KB
/
blueberry.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
package blueberry
import (
"fmt"
"net/http"
"github.com/danieledaccurso/blueberry/glue"
"github.com/danieledaccurso/blueberry/router"
)
type AppConfig struct {
Server struct {
TLS struct {
Enabled bool
CertFile string
KeyFile string
}
ListenHost string
}
}
type App struct {
router *router.Router
AppConfig *AppConfig
}
func New() *App {
a := new(App)
a.router = router.NewRouter()
a.AppConfig = new(AppConfig)
return a
}
func CreateDefault() *App {
a := New()
a.EnableCORS()
a.EnableJSONResponse()
a.EnableParams()
return a
}
func (a *App) EnableJSONResponse() *App {
a.router.AppendPostRequestEvent(new(glue.RenderResponseEvent))
return a
}
func (a *App) EnableCORS() *App {
a.router.AppendPreRequestEvent(new(glue.CORSEvent))
return a
}
func (a *App) EnableParams() *App {
a.router.AddInjector(new(glue.ParameterInjector))
return a
}
func (a *App) Router() *router.Router {
return a.router
}
func (a *App) ListenAndServe() {
if !a.AppConfig.Server.TLS.Enabled {
err := http.ListenAndServe(a.AppConfig.Server.ListenHost, a.Router)
if err != nil {
fmt.Println(err.Error())
}
}
}