-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
70 lines (57 loc) · 1.38 KB
/
application.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
package application
import (
"net/http"
"github.com/go-chi/chi"
"github.com/jinzhu/gorm"
"github.com/qor/admin"
"github.com/qor/assetfs"
"github.com/qor/middlewares"
"github.com/qor/wildcard_router"
)
// MicroAppInterface micro app interface
type MicroAppInterface interface {
ConfigureApplication(*Application)
}
// Application main application
type Application struct {
*Config
}
// Config application config
type Config struct {
Router *chi.Mux
Handlers []http.Handler
AssetFS assetfs.Interface
Admin *admin.Admin
DB *gorm.DB
}
// New new application
func New(cfg *Config) *Application {
if cfg == nil {
cfg = &Config{}
}
if cfg.Router == nil {
cfg.Router = chi.NewRouter()
}
if cfg.AssetFS == nil {
cfg.AssetFS = assetfs.AssetFS()
}
return &Application{
Config: cfg,
}
}
// Use mount router into micro app
func (application *Application) Use(app MicroAppInterface) {
app.ConfigureApplication(application)
}
// NewServeMux allocates and returns a new ServeMux.
func (application *Application) NewServeMux() http.Handler {
if len(application.Config.Handlers) == 0 {
return middlewares.Apply(application.Config.Router)
}
wildcardRouter := wildcard_router.New()
for _, handler := range application.Config.Handlers {
wildcardRouter.AddHandler(handler)
}
wildcardRouter.AddHandler(application.Config.Router)
return middlewares.Apply(wildcardRouter)
}