forked from presentator/presentator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (119 loc) · 3.54 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"log"
"os"
"path/filepath"
"strings"
"github.com/pocketbase/pocketbase/plugins/ghupdate"
"github.com/pocketbase/pocketbase/plugins/jsvm"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
"github.com/presentator/presentator/v3"
)
func main() {
app := presentator.New()
// ---------------------------------------------------------------
// Optional flags:
// ---------------------------------------------------------------
var hooksDir string
app.RootCmd.PersistentFlags().StringVar(
&hooksDir,
"hooksDir",
"",
"the directory with the JS app hooks",
)
var hooksWatch bool
app.RootCmd.PersistentFlags().BoolVar(
&hooksWatch,
"hooksWatch",
true,
"auto restart the app on pb_hooks file change",
)
var hooksPool int
app.RootCmd.PersistentFlags().IntVar(
&hooksPool,
"hooksPool",
25,
"the total prewarm goja.Runtime instances for the JS app hooks execution",
)
var migrationsDir string
if isGoRun() {
migrationsDir = filepath.Join(app.DataDir(), "../../migrations")
}
app.RootCmd.PersistentFlags().StringVar(
&migrationsDir,
"migrationsDir",
migrationsDir,
"the directory with the user defined migrations (default to pb_data/../pb_migrations)",
)
var allowHotspotsUrl bool
app.RootCmd.PersistentFlags().BoolVar(
&allowHotspotsUrl,
"allowHotspotsUrl",
true,
"allow or disallow the hotspot url type to prevent abuse as in issue#147",
)
var footerLinks []string
app.RootCmd.PersistentFlags().StringSliceVar(
&footerLinks,
"footerLinks",
nil,
"comma separated footer links in the format 'title1|url1, title2|url2, ...'\nexample: 'Privacy policy|https://example.com/policy,Contacts|https://example.com/contacts'",
)
var termsUrl []string
app.RootCmd.PersistentFlags().StringSliceVar(
&termsUrl,
"termsUrl",
nil,
"URL to your Terms and Conditions page that is referenced during users registration",
)
app.RootCmd.ParseFlags(os.Args[1:])
// update app options
app.Store().Set(presentator.OptionFooterLinks, arrayLinksToMap(footerLinks))
app.Store().Set(presentator.OptionAllowHotspotsUrl, allowHotspotsUrl)
app.Store().Set(presentator.OptionTermsUrl, termsUrl)
// ---------------------------------------------------------------
// Plugins and hooks:
// ---------------------------------------------------------------
// load jsvm (hooks and migrations)
jsvm.MustRegister(app, jsvm.Config{
MigrationsDir: migrationsDir,
HooksDir: hooksDir,
HooksWatch: hooksWatch,
HooksPoolSize: hooksPool,
})
// migrate command
// use Go templates with "go run", otherwise JS
migrateConfig := migratecmd.Config{Dir: migrationsDir}
if !isGoRun() {
migrateConfig.TemplateLang = migratecmd.TemplateLangJS
}
migratecmd.MustRegister(app, app.RootCmd, migrateConfig)
// GitHub selfupdate
ghupdate.MustRegister(app, app.RootCmd, ghupdate.Config{
Owner: "presentator",
Repo: "presentator",
ArchiveExecutable: "presentator",
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}
// converts ["title1|url1", "title2|url2"] into {title1:url1, title2:url2}
func arrayLinksToMap(links []string) map[string]string {
result := map[string]string{}
for _, l := range links {
parts := strings.SplitN(l, "|", 2)
if len(parts) != 2 {
continue
}
result[parts[0]] = parts[1]
}
return result
}
// isGoRun loosely checks if "go run" or "go build" was used for compiling the executable.
func isGoRun() bool {
if strings.HasPrefix(os.Args[0], os.TempDir()) {
return true // probably ran with go run
}
return false // probably ran with go build
}