nirvash/nirvash.go

416 lines
8.9 KiB
Go
Raw Permalink Normal View History

package main
import (
core "hacklab.nilfm.cc/nirvash/archetype"
. "hacklab.nilfm.cc/nirvash/lfo"
"hacklab.nilfm.cc/quartzgun/indentalUserDB"
. "hacklab.nilfm.cc/quartzgun/middleware"
"hacklab.nilfm.cc/quartzgun/renderer"
"hacklab.nilfm.cc/quartzgun/router"
2023-10-21 04:46:37 +00:00
"html/template"
"net/http"
2022-05-29 06:26:36 +00:00
"os"
"path/filepath"
)
func main() {
cfg := core.ReadConfig()
2022-05-29 06:26:36 +00:00
udb := indentalUserDB.CreateIndentalUserDB(
filepath.Join(
core.GetConfigLocation(),
2022-05-29 06:26:36 +00:00
"user.db"))
if core.ProcessCmd(os.Args, udb, cfg) {
2022-05-29 06:26:36 +00:00
os.Exit(0)
}
if cfg.IsNull() {
cfg.RunWizard()
2022-05-29 06:26:36 +00:00
}
cfg.Adapter.Init(cfg)
fileManager := &core.SimpleFileManager{}
fileManager.Init(cfg)
2022-06-08 05:44:54 +00:00
pathConcat := filepath.Join
templateRoot := pathConcat(cfg.AssetRoot, "templates")
2022-06-08 05:44:54 +00:00
2022-05-29 06:26:36 +00:00
rtr := &router.Router{
StaticPaths: map[string]string{
"/static/": filepath.Join(cfg.AssetRoot, "static"),
"/files/": cfg.StaticRoot,
2022-05-29 06:26:36 +00:00
},
Fallback: *template.Must(template.ParseFiles(
pathConcat(templateRoot, "error.html"),
pathConcat(templateRoot, "header_err.html"),
pathConcat(templateRoot, "footer.html"))),
2022-05-29 06:26:36 +00:00
}
rtr.Get("/login", renderer.Template(
2022-06-08 05:44:54 +00:00
pathConcat(templateRoot, "login.html")))
2022-05-29 06:26:36 +00:00
rtr.Post("/login", Authorize("/", udb, "/login?tryagain=1", 84))
2022-05-29 06:26:36 +00:00
rtr.Get("/logout", Bunt("/", udb, "/login?tryagain=1"))
2022-06-03 04:56:38 +00:00
rtr.Get(
"/",
Protected(
WithAdapter(
renderer.Template(
2022-06-08 05:44:54 +00:00
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(
2022-06-08 05:44:54 +00:00
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(
2022-06-08 05:44:54 +00:00
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(
2022-06-08 05:44:54 +00:00
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(
2022-06-08 05:44:54 +00:00
pathConcat(templateRoot, "cms_create.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
cfg.Adapter),
cfg.Adapter),
http.MethodGet,
udb,
"/login"),
udb,
"/"))
2022-05-29 06:26:36 +00:00
2022-06-07 06:59:41 +00:00
rtr.Get(
`/build`,
Fortify(
Protected(
WithAdapter(
2022-06-07 06:59:41 +00:00
renderer.Template(
2022-06-08 05:44:54 +00:00
pathConcat(templateRoot, "build.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
2022-06-07 06:59:41 +00:00
cfg.Adapter),
http.MethodGet,
udb,
"/login")))
rtr.Post(
`/build-run`,
Defend(
Protected(
SanitizeFormMap(
FormMapToBuildOptions(
WithAdapter(
renderer.Template(
pathConcat(templateRoot, "build_run.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
cfg.Adapter))),
2022-06-07 06:59:41 +00:00
http.MethodGet,
udb,
"/login"),
udb,
"/"))
2023-10-21 04:46:37 +00:00
rtr.Get(
`/deploy`,
Fortify(
Protected(
WithAdapter(
renderer.Template(
pathConcat(templateRoot, "deploy.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
cfg.Adapter),
http.MethodGet,
udb,
"/login")))
rtr.Post(
`/deploy`,
Defend(
Protected(
WithAdapter(
renderer.Template(
pathConcat(templateRoot, "deployed.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
cfg.Adapter),
http.MethodGet,
udb,
"/login"),
udb,
"/"))
rtr.Post(
`/revert`,
Defend(
Protected(
WithAdapter(
renderer.Template(
pathConcat(templateRoot, "reverted.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
cfg.Adapter),
http.MethodGet,
udb,
"/login"),
udb,
"/"))
2022-06-07 06:59:41 +00:00
rtr.Post(
`/delete/(?P<Slug>\S+)`,
Defend(
Protected(
WithAdapter(
2022-06-07 06:59:41 +00:00
renderer.Template(
2022-06-08 05:44:54 +00:00
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(
2022-06-08 05:44:54 +00:00
renderer.Template(
pathConcat(templateRoot, "config.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
2022-06-07 06:59:41 +00:00
cfg.Adapter),
http.MethodGet,
udb,
2022-06-08 05:44:54 +00:00
"/login")))
rtr.Post(
`/config-set`,
Defend(
Protected(
SanitizeFormMap(
FormMapToAdapterConfig(
WithAdapter(
2022-06-08 05:44:54 +00:00
renderer.Template(
pathConcat(templateRoot, "config_set.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
cfg.Adapter))),
2022-06-08 05:44:54 +00:00
http.MethodGet,
udb,
2022-06-07 06:59:41 +00:00
"/login"),
udb,
"/"))
2022-06-08 05:44:54 +00:00
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")))
2022-06-13 06:32:14 +00:00
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,
"/"))
2022-06-13 06:32:14 +00:00
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))
2022-06-13 06:32:14 +00:00
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,
"/"))
2022-06-13 06:32:14 +00:00
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"))
2024-10-27 19:30:20 +00:00
if cfg.ListenAddress == "" {
cfg.ListenAddress = "127.0.0.1:8080"
}
http.ListenAndServe(cfg.ListenAddress, rtr)
2022-05-29 06:26:36 +00:00
}