-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
211 lines (177 loc) · 5.86 KB
/
Copy pathmain.go
File metadata and controls
211 lines (177 loc) · 5.86 KB
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
package main
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/fatih/color"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
"github.com/parkuman/pocketcms/ui"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/models/schema"
"github.com/pocketbase/pocketbase/tools/types"
)
const pcmsAdminPath = "/cms/"
const pcmsUsersCollection = "pcms__users"
const pcmsCollectionsCollection = "pcms__collections"
func createPCMSCollectionsCollection(app core.App) error {
collection := &models.Collection{}
collection.MarkAsNew()
collection.Id = pcmsCollectionsCollection
collection.Name = pcmsCollectionsCollection
collection.Type = models.CollectionTypeView
collection.ListRule = types.Pointer("")
collection.ViewRule = types.Pointer("")
collection.SetOptions(models.CollectionViewOptions{
Query: `
SELECT id, name, schema, listRule, viewRule, createRule, updateRule, deleteRule
FROM _collections
WHERE name LIKE 'pcms|_%' ESCAPE '|'
AND name NOT LIKE 'pcms|_|_%' ESCAPE '|';
`,
})
return app.Dao().SaveCollection(collection)
}
func createPCMSUsersCollections(app core.App) error {
usersCollection := &models.Collection{}
usersCollection.MarkAsNew()
usersCollection.Id = pcmsUsersCollection
usersCollection.Name = pcmsUsersCollection
usersCollection.Type = models.CollectionTypeAuth
usersCollection.ListRule = types.Pointer("id = @request.auth.id")
usersCollection.ViewRule = types.Pointer("id = @request.auth.id")
usersCollection.CreateRule = types.Pointer("")
usersCollection.UpdateRule = types.Pointer("id = @request.auth.id")
usersCollection.DeleteRule = types.Pointer("id = @request.auth.id")
// set auth options
usersCollection.SetOptions(models.CollectionAuthOptions{
ManageRule: nil,
AllowOAuth2Auth: true,
AllowUsernameAuth: true,
AllowEmailAuth: true,
MinPasswordLength: 8,
RequireEmail: false,
})
// set optional default fields
usersCollection.Schema = schema.NewSchema(
&schema.SchemaField{
Id: fmt.Sprintf("%s_name", pcmsUsersCollection),
Type: schema.FieldTypeText,
Name: "name",
Required: true,
Options: &schema.TextOptions{},
}, &schema.SchemaField{
Id: fmt.Sprintf("%s_avatar", pcmsUsersCollection),
Type: schema.FieldTypeFile,
Name: "avatar",
Options: &schema.FileOptions{
MaxSelect: 1,
MaxSize: 5242880,
MimeTypes: []string{
"image/jpeg",
"image/png",
"image/svg+xml",
"image/gif",
"image/webp",
},
},
},
)
return app.Dao().SaveCollection(usersCollection)
}
func setupPCMSCollections(app core.App) error {
_, err := app.Dao().FindCollectionByNameOrId(pcmsUsersCollection)
if err != nil {
if usersErr := createPCMSUsersCollections(app); usersErr != nil {
return usersErr
}
}
_, err = app.Dao().FindCollectionByNameOrId(pcmsCollectionsCollection)
if err != nil {
if collectionsErr := createPCMSCollectionsCollection(app); collectionsErr != nil {
return collectionsErr
}
}
return nil
}
// ripped straight from pocketbases base.go file
// installerRedirect redirects the user to the installer CMS admin UI page
// when the application needs some preliminary configurations to be done.
func installerRedirect(app core.App) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// skip redirect checks for non-root level index.html requests
path := c.Request().URL.Path
if path != pcmsAdminPath && path != pcmsAdminPath+"index.html" {
return next(c)
}
_, hasInstallerParam := c.Request().URL.Query()["installer"]
installerStage := c.Request().URL.Query().Get("installer")
totalPBAdmins, err := app.Dao().TotalAdmins()
if err != nil {
return err
}
hasPBAdmin := totalPBAdmins > 0
const stagePB = "pb"
if !hasPBAdmin && installerStage != stagePB {
// if pocketbase admin is not even set up yet, tell the user to do that first
return c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("?installer=%s#", stagePB))
}
// if somehow these collections were deleted during runtime
setupPCMSCollections(app)
// attempt to find a user in the pcmsUsersCollection
var pcmsUserId string = ""
var hasPCMSUser bool
err = app.Dao().DB().NewQuery(fmt.Sprintf("SELECT id FROM {{%s}}", pcmsUsersCollection)).Row(&pcmsUserId)
if err != nil {
hasPCMSUser = false
}
if pcmsUserId != "" {
hasPCMSUser = true
}
const stagePCMS = "pcms"
if hasPBAdmin && !hasPCMSUser && installerStage != stagePCMS {
// if pocketbase has an admin and no cms user, tell the user to now create their first pcms user
return c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("?installer=%s#", stagePCMS))
}
if hasPBAdmin && hasPCMSUser && hasInstallerParam {
// clear the installer param
return c.Redirect(http.StatusTemporaryRedirect, "?")
}
return next(c)
}
}
}
func bindStaticCMSAdminUI(app core.App, e *core.ServeEvent) error {
// redirect to trailing slash to ensure that relative urls will still work properly
e.Router.GET(
strings.TrimRight(pcmsAdminPath, "/"),
func(c echo.Context) error {
return c.Redirect(http.StatusTemporaryRedirect, strings.TrimLeft(pcmsAdminPath, "/"))
},
)
// serves static files from the /ui/dist directory
e.Router.GET(
pcmsAdminPath+"*",
echo.StaticDirectoryHandler(ui.DistDirFS, false),
installerRedirect(app),
middleware.Gzip(),
)
regular := color.New()
regular.Printf("├─ CMS Admin UI: %s\n", color.CyanString("%s://%s%s", "https", e.Server.Addr, pcmsAdminPath))
return nil
}
func main() {
app := pocketbase.New()
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
setupPCMSCollections(app)
bindStaticCMSAdminUI(app, e)
return nil
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}