nirvash/nirvash.go

422 lines
9.2 KiB
Go

package main
import (
core "forge.lightcrystal.systems/nilix/nirvash/archetype"
. "forge.lightcrystal.systems/nilix/nirvash/lfo"
"forge.lightcrystal.systems/nilix/quartzgun/indentalUserDB"
. "forge.lightcrystal.systems/nilix/quartzgun/middleware"
"forge.lightcrystal.systems/nilix/quartzgun/rateLimiter"
"forge.lightcrystal.systems/nilix/quartzgun/renderer"
"forge.lightcrystal.systems/nilix/quartzgun/router"
"html/template"
"net/http"
"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"))),
}
rl := rateLimiter.IpRateLimiter{
Data: map[string]*rateLimiter.RateLimitData{},
Seconds: 5,
AttemptsAllowed: 5,
}
rtr.Get("/login", renderer.Template(
pathConcat(templateRoot, "login.html")))
rtr.Post("/login", Throttle(Authorize("/", udb, "/login?tryagain=1", 84), rl.RateLimit))
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(
FormMapToBuildOptions(
WithAdapter(
renderer.Template(
pathConcat(templateRoot, "build_run.html"),
pathConcat(templateRoot, "header.html"),
pathConcat(templateRoot, "footer.html")),
cfg.Adapter))),
http.MethodGet,
udb,
"/login"),
udb,
"/"))
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,
"/"))
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))),
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"))
if cfg.ListenAddress == "" {
cfg.ListenAddress = "127.0.0.1:8080"
}
http.ListenAndServe(cfg.ListenAddress, rtr)
}