all repos — nirvash @ b530a492ba83df0d63390991494bd8eb09a5e00c

modular CMS using the quartzgun library

nirvash.go (raw)

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
247
248
249
250
251
252
253
254
255
256
257
package main

import (
	"html/template"
	"net/http"
	core "nilfm.cc/git/nirvash/archetype"
	. "nilfm.cc/git/nirvash/lfo"
	"nilfm.cc/git/quartzgun/indentalUserDB"
	. "nilfm.cc/git/quartzgun/middleware"
	"nilfm.cc/git/quartzgun/renderer"
	"nilfm.cc/git/quartzgun/router"
	"os"
	"path/filepath"
)

func main() {
	cfg := core.ReadConfig()
	udb := indentalUserDB.CreateIndentalUserDB(
		filepath.Join(
			core.GetConfigLocation(),
			"user.db"))
	if core.ProcessCmd(os.Args, udb, cfg) {
		os.Exit(0)
	}
	if cfg.IsNull() {
		cfg.RunWizard()
	}

	cfg.Adapter.Init(cfg)

	fileManager := &core.SimpleFileManager{}
	fileManager.Init(cfg)

	pathConcat := filepath.Join
	templateRoot := pathConcat(cfg.AssetRoot, "templates")

	rtr := &router.Router{
		StaticPaths: map[string]string{
			"/static/": filepath.Join(cfg.AssetRoot, "static"),
			"/files/":  cfg.StaticRoot,
		},
		Fallback: *template.Must(template.ParseFiles(
			pathConcat(templateRoot, "error.html"),
			pathConcat(templateRoot, "header.html"),
			pathConcat(templateRoot, "footer.html"))),
	}

	rtr.Get("/login", renderer.Template(
		pathConcat(templateRoot, "login.html")))

	rtr.Post("/login", Authorize("/", udb, "/login?tryagain=1"))

	rtr.Get("/logout", Bunt("/", udb, "/login?tryagain=1"))

	rtr.Get(
		"/",
		Protected(
			WithAdapter(
				renderer.Template(
					pathConcat(templateRoot, "cms_list.html"),
					pathConcat(templateRoot, "header.html"),
					pathConcat(templateRoot, "footer.html")),
				cfg.Adapter),
			http.MethodGet,
			udb,
			"/login"))

	rtr.Get(
		`/edit/(?P<Slug>\S+)`,
		Fortify(
			Protected(
				WithAdapter(
					renderer.Template(
						pathConcat(templateRoot, "cms_edit.html"),
						pathConcat(templateRoot, "header.html"),
						pathConcat(templateRoot, "footer.html")),
					cfg.Adapter),
				http.MethodGet,
				udb,
				"/login")))

	rtr.Post(
		`/save/(?P<Slug>\S+)`,
		Defend(
			Protected(
				WithAdapter(
					EnsurePageData(
						renderer.Template(
							pathConcat(templateRoot, "cms_save.html"),
							pathConcat(templateRoot, "header.html"),
							pathConcat(templateRoot, "footer.html")),
						cfg.Adapter),
					cfg.Adapter),
				http.MethodGet,
				udb,
				"/login"),
			udb,
			"/"))

	rtr.Get(
		`/new`,
		Fortify(
			Protected(
				WithAdapter(
					renderer.Template(
						pathConcat(templateRoot, "cms_new.html"),
						pathConcat(templateRoot, "header.html"),
						pathConcat(templateRoot, "footer.html")),
					cfg.Adapter),
				http.MethodGet,
				udb,
				"/login")))

	rtr.Post(
		`/create`,
		Defend(
			Protected(
				WithAdapter(
					EnsurePageData(
						renderer.Template(
							pathConcat(templateRoot, "cms_create.html"),
							pathConcat(templateRoot, "header.html"),
							pathConcat(templateRoot, "footer.html")),
						cfg.Adapter),
					cfg.Adapter),
				http.MethodGet,
				udb,
				"/login"),
			udb,
			"/"))

	rtr.Get(
		`/build`,
		Fortify(
			Protected(
				WithAdapter(
					renderer.Template(
						pathConcat(templateRoot, "build.html"),
						pathConcat(templateRoot, "header.html"),
						pathConcat(templateRoot, "footer.html")),
					cfg.Adapter),
				http.MethodGet,
				udb,
				"/login")))

	rtr.Post(
		`/build-run`,
		Defend(
			Protected(
				SanitizeFormMap(
					WithAdapter(
						renderer.Template(
							pathConcat(templateRoot, "build_run.html"),
							pathConcat(templateRoot, "header.html"),
							pathConcat(templateRoot, "footer.html")),
						cfg.Adapter)),
				http.MethodGet,
				udb,
				"/login"),
			udb,
			"/"))

	rtr.Post(
		`/delete/(?P<Slug>\S+)`,
		Defend(
			Protected(
				WithAdapter(
					renderer.Template(
						pathConcat(templateRoot, "delete.html"),
						pathConcat(templateRoot, "header.html"),
						pathConcat(templateRoot, "footer.html")),
					cfg.Adapter),
				http.MethodGet,
				udb,
				"/login"),
			udb,
			"/"))

	rtr.Get(
		`/config`,
		Fortify(
			Protected(
				WithAdapter(
					renderer.Template(
						pathConcat(templateRoot, "config.html"),
						pathConcat(templateRoot, "header.html"),
						pathConcat(templateRoot, "footer.html")),
					cfg.Adapter),
				http.MethodGet,
				udb,
				"/login")))

	rtr.Post(
		`/config-set`,
		Defend(
			Protected(
				SanitizeFormMap(
					FormMapToAdapterConfig(
						WithAdapter(
							renderer.Template(
								pathConcat(templateRoot, "config_set.html"),
								pathConcat(templateRoot, "header.html"),
								pathConcat(templateRoot, "footer.html")),
							cfg.Adapter),
						cfg.Adapter)),
				http.MethodGet,
				udb,
				"/login"),
			udb,
			"/"))

	rtr.Get(
		`/static-mgr`,
		http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			http.Redirect(w, req, "/static-mgr/", http.StatusSeeOther)
		}))

	rtr.Get(
		`/static-mgr/(?P<Slug>.*)`,
		Fortify(
			Protected(
				WithFileManager(
					renderer.Template(
						pathConcat(templateRoot, "file_list.html"),
						pathConcat(templateRoot, "header.html"),
						pathConcat(templateRoot, "footer.html")),
					fileManager),
				http.MethodGet,
				udb,
				"/login")))

	rtr.Get(
		`/file-actions/(?P<Slug>.*)`,
		Fortify(
			Protected(
				WithFileManager(
					WithFileData(
						renderer.Template(
							pathConcat(templateRoot, "file_actions.html"),
							pathConcat(templateRoot, "header.html"),
							pathConcat(templateRoot, "footer.html")),
						fileManager),
					fileManager),
				http.MethodGet,
				udb,
				"/login")))
	// file upload GET contains form for file upload
	// file upload POST performs the action of creating/overwriting
	// add directory GET contains the form for directory creation
	// add directory POST performs the action of creating directory
	// delete GET contains the form for confirming deletion
	// delete POST performs the action of deleting
	// move GET (not required?)
	// move-choose POST uses a form to navigate through the file tree
	// move-do POST moves the file when finalized in move-choose
	http.ListenAndServe(":8080", rtr)
}