-
Notifications
You must be signed in to change notification settings - Fork 326
/
main.go
121 lines (109 loc) · 3.85 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
112
113
114
115
116
117
118
119
120
121
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/qor/admin"
"github.com/qor/publish2"
"github.com/qor/qor"
"github.com/qor/qor-example/app/account"
adminapp "github.com/qor/qor-example/app/admin"
"github.com/qor/qor-example/app/api"
"github.com/qor/qor-example/app/enterprise"
"github.com/qor/qor-example/app/home"
"github.com/qor/qor-example/app/orders"
"github.com/qor/qor-example/app/pages"
"github.com/qor/qor-example/app/products"
"github.com/qor/qor-example/app/static"
"github.com/qor/qor-example/config"
"github.com/qor/qor-example/config/application"
"github.com/qor/qor-example/config/auth"
"github.com/qor/qor-example/config/bindatafs"
"github.com/qor/qor-example/config/db"
_ "github.com/qor/qor-example/config/db/migrations"
"github.com/qor/qor-example/utils/funcmapmaker"
"github.com/qor/qor/utils"
)
func main() {
cmdLine := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
compileTemplate := cmdLine.Bool("compile-templates", false, "Compile Templates")
cmdLine.Parse(os.Args[1:])
var (
Router = chi.NewRouter()
Admin = admin.New(&admin.AdminConfig{
SiteName: "QOR DEMO",
Auth: auth.AdminAuth{},
DB: db.DB.Set(publish2.VisibleMode, publish2.ModeOff).Set(publish2.ScheduleMode, publish2.ModeOff),
})
Application = application.New(&application.Config{
Router: Router,
Admin: Admin,
DB: db.DB,
})
)
funcmapmaker.AddFuncMapMaker(auth.Auth.Config.Render)
Router.Use(func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// for demo, don't use this for your production site
w.Header().Add("Access-Control-Allow-Origin", "*")
handler.ServeHTTP(w, req)
})
})
Router.Use(func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.Header.Del("Authorization")
handler.ServeHTTP(w, req)
})
})
Router.Use(middleware.RealIP)
Router.Use(middleware.Logger)
Router.Use(middleware.Recoverer)
Router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var (
tx = db.DB
qorContext = &qor.Context{Request: req, Writer: w}
)
if locale := utils.GetLocale(qorContext); locale != "" {
tx = tx.Set("l10n:locale", locale)
}
ctx := context.WithValue(req.Context(), utils.ContextDBName, publish2.PreviewByDB(tx, qorContext))
next.ServeHTTP(w, req.WithContext(ctx))
})
})
Application.Use(api.New(&api.Config{}))
Application.Use(adminapp.New(&adminapp.Config{}))
Application.Use(home.New(&home.Config{}))
Application.Use(products.New(&products.Config{}))
Application.Use(account.New(&account.Config{}))
Application.Use(orders.New(&orders.Config{}))
Application.Use(pages.New(&pages.Config{}))
Application.Use(enterprise.New(&enterprise.Config{}))
Application.Use(static.New(&static.Config{
Prefixs: []string{"/system"},
Handler: utils.FileServer(http.Dir(filepath.Join(config.Root, "public"))),
}))
Application.Use(static.New(&static.Config{
Prefixs: []string{"javascripts", "stylesheets", "images", "dist", "fonts", "vendors", "favicon.ico"},
Handler: bindatafs.AssetFS.FileServer(http.Dir("public"), "javascripts", "stylesheets", "images", "dist", "fonts", "vendors", "favicon.ico"),
}))
if *compileTemplate {
bindatafs.AssetFS.Compile()
} else {
fmt.Printf("Listening on: %v\n", config.Config.Port)
if config.Config.HTTPS {
if err := http.ListenAndServeTLS(fmt.Sprintf(":%d", config.Config.Port), "config/local_certs/server.crt", "config/local_certs/server.key", Application.NewServeMux()); err != nil {
panic(err)
}
} else {
if err := http.ListenAndServe(fmt.Sprintf(":%d", config.Config.Port), Application.NewServeMux()); err != nil {
panic(err)
}
}
}
}