nirvash/nirvash.go

367 lines
7.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_err.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(
`/file-mgr`,
http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "/file-mgr/", http.StatusSeeOther)
}))
rtr.Get(
`/file-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(
renderer.Template(
pathConcat(templateRoot, "file_actions.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
fileManager),
http.MethodGet,
udb,
"/login")))
rtr.Get(
`/file-move/(?P<Slug>.*)`,
Fortify(
Protected(
WithFileManager(
renderer.Template(
pathConcat(templateRoot, "file_move.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
fileManager),
http.MethodGet,
udb,
"/login")))
rtr.Post(
`/file-move-process/(?P<Slug>.*)`,
Defend(
Protected(
WithFileManager(
renderer.Template(
pathConcat(templateRoot, "file_move_process.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
fileManager),
http.MethodGet,
udb,
"/login"),
udb,
"/"))
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,
"/"),
fileManager))
rtr.Get(
`/mkdir/(?P<Slug>.*)`,
Fortify(
Protected(
WithFileManager(
renderer.Template(
pathConcat(templateRoot, "file_mkdir.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
fileManager),
http.MethodGet,
udb,
"/login")))
rtr.Post(
`/mkdir-process/(?P<Slug>.*)`,
Defend(
Protected(
WithFileManager(
renderer.Template(
pathConcat(templateRoot, "file_mkdir_process.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
fileManager),
http.MethodGet,
udb,
"/login"),
udb,
"/"))
rtr.Get(
`/fmt-help`,
Protected(
WithAdapter(
renderer.Template(
pathConcat(templateRoot, "format_help.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
cfg.Adapter),
http.MethodGet,
udb,
"/login"))
http.ListenAndServe(":8080", rtr)
}