mirror of
https://hacklab.nilfm.cc/nirvash
synced 2024-10-22 14:01:49 +00:00
306 lines
6.8 KiB
Go
306 lines
6.8 KiB
Go
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")))
|
|
|
|
rtr.Post(
|
|
`/file-delete/(?P<Slug>.*)`,
|
|
Defend(
|
|
Protected(
|
|
WithFileManager(
|
|
renderer.Template(
|
|
pathConcat(templateRoot, "file_delete.html"),
|
|
pathConcat(templateRoot, "header.html"),
|
|
pathConcat(templateRoot, "footer.html")),
|
|
fileManager),
|
|
http.MethodGet,
|
|
udb,
|
|
"/login"),
|
|
udb,
|
|
"/"))
|
|
|
|
rtr.Get(
|
|
`/upload/(?P<Slug>.*)`,
|
|
Fortify(
|
|
Protected(
|
|
WithFileManager(
|
|
renderer.Template(
|
|
pathConcat(templateRoot, "file_upload.html"),
|
|
pathConcat(templateRoot, "header.html"),
|
|
pathConcat(templateRoot, "footer.html")),
|
|
fileManager),
|
|
http.MethodGet,
|
|
udb,
|
|
"/login")))
|
|
|
|
rtr.Post(
|
|
`/upload-process/(?P<Slug>.*)`,
|
|
PrepareForUpload(
|
|
Defend(
|
|
Protected(
|
|
WithFileManager(
|
|
renderer.Template(
|
|
pathConcat(templateRoot, "file_upload_process.html"),
|
|
pathConcat(templateRoot, "header.html"),
|
|
pathConcat(templateRoot, "footer.html")),
|
|
fileManager),
|
|
http.MethodGet,
|
|
udb,
|
|
"/login"),
|
|
udb,
|
|
"/")))
|
|
|
|
// TODO:
|
|
// file upload GET contains form for file upload in current directory
|
|
// 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
|
|
// move-choose POST uses a form to navigate through the file tree
|
|
// - to use links to navigate, destination location uses the slug,
|
|
// - and file to move uses the POST parameters
|
|
// move-do POST moves the file when finalized in move-choose
|
|
|
|
http.ListenAndServe(":8080", rtr)
|
|
}
|