-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
246 lines (192 loc) · 5.66 KB
/
app.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package goadmin
import (
"embed"
"fmt"
"net/url"
"os"
"path/filepath"
"sort"
"github.com/CloudyKit/jet/v6"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/partyzanex/go-admin-bootstrap/assets"
migrations "github.com/partyzanex/go-admin-bootstrap/db/migrations/postgres"
"github.com/partyzanex/go-admin-bootstrap/views"
)
type App struct {
config *Config
echo *echo.Echo
static *echo.Group
admin *echo.Group
baseURL *url.URL
}
func New(config *Config) (*App, error) {
if err := config.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid config")
}
if err := migrations.Up(config.DBConfig.DB, config.DBConfig.MigrationsTable); err != nil {
return nil, errors.Wrap(err, "cannot up migrations")
}
baseURL, err := url.Parse(config.BaseURL)
if err != nil {
return nil, errors.Wrapf(err, "cannot parse %q as base url", config.BaseURL)
}
e := echo.New()
e.HTTPErrorHandler = errorHandler
app := new(App)
app.config = config.Clone()
app.echo = e
app.baseURL = baseURL
app.setStaticGroup()
app.setDefaultRoutes()
app.setDefaultMiddleware()
app.setDefaultRenderer()
err = app.CreateAssets()
if err != nil {
return nil, errors.Wrap(err, "cannot create sources")
}
return app, nil
}
func (app *App) Static() *echo.Group {
return app.static
}
func (app *App) Admin() *echo.Group {
return app.admin
}
func (app *App) Echo() *echo.Echo {
return app.echo
}
func (app *App) Serve() error {
return app.echo.Start(app.getAddr())
}
func (app *App) Close() error {
return app.echo.Close()
}
func (app *App) CreateAssets() error {
assetsByKind := make(map[AssetKind][]*Asset)
for _, source := range app.config.Assets {
_, ok := assetsByKind[source.Kind]
if !ok {
assetsByKind[source.Kind] = []*Asset{source}
continue
}
assetsByKind[source.Kind] = append(assetsByKind[source.Kind], source)
}
var (
javascriptSources = JS
stylesheetSources = CSS
viewSources = Views
)
if sources, ok := assetsByKind[JavaScript]; ok {
javascriptSources = append(javascriptSources, sources...)
}
sort.Slice(javascriptSources, func(i, j int) bool {
return javascriptSources[i].SortOrder < javascriptSources[j].SortOrder
})
for _, source := range javascriptSources {
err := app.createSource(app.config.AssetsPath, source, &assets.JS)
if err != nil {
return errors.Wrapf(err, "cannot create source %s", source.Path)
}
}
if sources, ok := assetsByKind[Stylesheet]; ok {
stylesheetSources = append(stylesheetSources, sources...)
}
sort.Slice(stylesheetSources, func(i, j int) bool {
return stylesheetSources[i].SortOrder < stylesheetSources[j].SortOrder
})
for _, source := range stylesheetSources {
err := app.createSource(app.config.AssetsPath, source, &assets.CSS)
if err != nil {
return errors.Wrapf(err, "cannot create source %q", source.Path)
}
}
if sources, ok := assetsByKind[View]; ok {
viewSources = append(viewSources, sources...)
}
for _, source := range viewSources {
err := app.createSource(app.config.ViewsPath, source, &views.Sources)
if err != nil {
// todo: wrap error
return err
}
}
return nil
}
func (*App) createSource(path string, source *Asset, fs *embed.FS) (err error) {
sourcePath := filepath.Join(path, source.Path)
stat, err := os.Stat(sourcePath)
if err != nil && !os.IsNotExist(err) {
return errors.Wrapf(err, "loading asset %s source failed", source.Path)
}
if stat != nil {
return nil
}
var (
b []byte
sourceDir = filepath.Dir(sourcePath)
)
b, err = fs.ReadFile(source.Path)
if err != nil {
return errors.Wrapf(err, "cannot read file %q", source.Path)
}
err = os.MkdirAll(sourceDir, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "make assets dir %s failed", sourceDir)
}
err = os.WriteFile(sourcePath, b, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "cannot write file %q", sourcePath)
}
return nil
}
func (app *App) getAddr() string {
return fmt.Sprintf("%s:%d", app.config.Host, app.config.Port)
}
func (app *App) setStaticGroup() {
if app.config.AssetsPath == "" {
app.config.AssetsPath = DefaultAssetsPath
}
if app.config.ViewsPath == "" {
app.config.ViewsPath = DefaultViewsPath
}
app.static = app.echo.Group(app.baseURL.Path + assetsRelativePath)
app.static.Static("/", app.config.AssetsPath)
}
func (app *App) setDefaultRoutes() {
app.admin = app.echo.Group(app.baseURL.Path, withViewData)
app.admin.GET(LoginURL, WrapHandler(Login))
app.admin.POST(LoginURL, WrapHandler(Login))
app.admin.Any(LogoutURL, WrapHandler(Logout), AuthByCookie)
app.admin.GET(DashboardURL, WrapHandler(Dashboard), AuthByCookie)
app.admin.GET(UserListURL, WrapHandler(UserList), AuthByCookie)
app.admin.GET(UserCreateURL, WrapHandler(UserCreate), AuthByCookie)
app.admin.POST(UserCreateURL, WrapHandler(UserCreate), AuthByCookie)
app.admin.GET(UserDeleteURL, WrapHandler(UserDelete), AuthByCookie)
app.admin.GET(UserUpdateURL, WrapHandler(UserUpdate), AuthByCookie)
app.admin.POST(UserUpdateURL, WrapHandler(UserUpdate), AuthByCookie)
app.admin.GET(FaviconPrefix, Favicon)
}
func (app *App) setDefaultMiddleware() {
for _, mw := range app.config.Middleware {
app.echo.Use(mw)
}
app.echo.Use(withAppContext(app))
}
func (app *App) setDefaultRenderer() {
opts := make([]jet.Option, 0)
if app.config.DevMode {
opts = append(opts, jet.InDevelopmentMode())
}
renderer := &Renderer{
Views: jet.NewSet(
jet.NewOSFileSystemLoader(app.config.ViewsPath),
opts...,
),
}
renderer.Views.AddGlobal(adminPathVar, app.baseURL.Path)
renderer.Views.AddGlobal(loginURLVar, LoginURL)
renderer.Views.AddGlobal(logoutURLVar, LogoutURL)
renderer.Views.AddGlobal(userListURLVar, UserListURL)
app.echo.Renderer = renderer
}