EurekaAdapter: flesh out CreatePage, SavePage, DeletePage and added TODO comments and the Config map; gofmt

This commit is contained in:
Iris Lightshard 2022-06-05 12:09:00 -06:00
parent 1bc8eac481
commit b9d971140a
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
5 changed files with 62 additions and 35 deletions

View file

@ -3,8 +3,8 @@ package archetype
type Adapter interface { type Adapter interface {
Init(cfg *Config) Init(cfg *Config)
Name() string Name() string
EditableSlugs() bool EditableSlugs() bool
BuildOptions() ([]string) BuildOptions() []string
GetConfig(key string) (interface{}, error) GetConfig(key string) (interface{}, error)
SetConfig(key string, value interface{}) error SetConfig(key string, value interface{}) error
ListPages() map[string]string ListPages() map[string]string

View file

@ -9,7 +9,8 @@ import (
) )
type EurekaAdapter struct { type EurekaAdapter struct {
Root string Root string
Config map[string]interface{}
} }
func (self *EurekaAdapter) Init(cfg *Config) { func (self *EurekaAdapter) Init(cfg *Config) {
@ -21,6 +22,8 @@ func (self *EurekaAdapter) Init(cfg *Config) {
} }
self.Root = cfg.Root self.Root = cfg.Root
// TODO: read config.h and build self.Config
} }
func (self *EurekaAdapter) Name() string { func (self *EurekaAdapter) Name() string {
@ -28,11 +31,11 @@ func (self *EurekaAdapter) Name() string {
} }
func (self *EurekaAdapter) EditableSlugs() bool { func (self *EurekaAdapter) EditableSlugs() bool {
return false return false
} }
func (self *EurekaAdapter) BuildOptions() []string { func (self *EurekaAdapter) BuildOptions() []string {
return []string{"twtxt"} return []string{"twtxt"}
} }
func (self *EurekaAdapter) GetConfig(key string) (interface{}, error) { func (self *EurekaAdapter) GetConfig(key string) (interface{}, error) {
@ -81,32 +84,56 @@ func (self *EurekaAdapter) GetPage(filename string) (Page, error) {
return Page{ return Page{
Title: title, Title: title,
Slug: filename, Slug: filename,
Content: content, Content: content,
Edited: fileInfo.ModTime(), Edited: fileInfo.ModTime(),
}, nil }, nil
} }
func (self *EurekaAdapter) FormatPage(raw string) string { func (self *EurekaAdapter) FormatPage(raw string) string {
// TODO: implement Eureka formatter to show preview
return raw return raw
} }
func (self *EurekaAdapter) FormattingHelp() string { func (self *EurekaAdapter) FormattingHelp() string {
// TODO: show Eureka formatting guide
return "help!" return "help!"
} }
func (self *EurekaAdapter) CreatePage(slug, title, content string) error { func (self *EurekaAdapter) CreatePage(slug, title, content string) error {
// eureka makes titles from slugs, so we don't use title here
f, err := os.Create(filepath.Join(self.Root, "inc", newSlug))
if err != nil {
return err
}
defer f.Close()
f.WriteString(content)
return nil return nil
} }
func (self *EurekaAdapter) SavePage(oldSlug, newSlug, title, content string) error { func (self *EurekaAdapter) SavePage(oldSlug, newSlug, title, content string) error {
// eureka makes titles from slugs, so we don't use title here
f, err := os.Create(filepath.Join(self.Root, "inc", newSlug))
if err != nil {
return err
}
defer f.Close()
if oldSlug != newSlug {
// TODO: delete old html as well
os.Remove(filepath.Join(self.Root, "inc", oldSlug))
}
f.WriteString(content)
return nil return nil
} }
func (self *EurekaAdapter) DeletePage(slug string) error { func (self *EurekaAdapter) DeletePage(slug string) error {
return nil // TODO: delete old html as well
return os.Remove(filepath.Join(self.Root, "inc", slug))
} }
func (self *EurekaAdapter) Build(buildOptions map[string]string) (bool, string) { func (self *EurekaAdapter) Build(buildOptions map[string]string) (bool, string) {
// TODO: shell out to build.sh with buildOptions, record exit status and output
return true, "Build successful" return true, "Build successful"
} }

View file

@ -6,7 +6,7 @@ import (
type Page struct { type Page struct {
Title string Title string
Slug string Slug string
Content string Content string
Edited time.Time Edited time.Time
} }

View file

@ -2,9 +2,9 @@ package lfo
import ( import (
"context" "context"
"strings"
"net/http" "net/http"
core "nilfm.cc/git/nirvash/archetype" core "nilfm.cc/git/nirvash/archetype"
"strings"
) )
func WithAdapter(next http.Handler, adapter core.Adapter) http.Handler { func WithAdapter(next http.Handler, adapter core.Adapter) http.Handler {
@ -17,20 +17,20 @@ func WithAdapter(next http.Handler, adapter core.Adapter) http.Handler {
} }
func EnsurePageData(next http.Handler) http.Handler { func EnsurePageData(next http.Handler) http.Handler {
handlerFunc := func(w http.ResponseWriter, req *http.Request) { handlerFunc := func(w http.ResponseWriter, req *http.Request) {
pageTitle := req.FormValue("title") pageTitle := req.FormValue("title")
pageContent := req.FormValue("content") pageContent := req.FormValue("content")
if pageTitle == "" || pageContent == "" { if pageTitle == "" || pageContent == "" {
newUri := "/edit/" newUri := "/edit/"
slug := strings.Join(strings.Split(req.URL.Path, "/")[2:], "/") slug := strings.Join(strings.Split(req.URL.Path, "/")[2:], "/")
newUri += slug newUri += slug
req.Method = http.MethodGet req.Method = http.MethodGet
http.Redirect(w, req, newUri, http.StatusSeeOther) http.Redirect(w, req, newUri, http.StatusSeeOther)
} else { } else {
next.ServeHTTP(w, req) next.ServeHTTP(w, req)
} }
} }
return http.HandlerFunc(handlerFunc) return http.HandlerFunc(handlerFunc)
} }

View file

@ -33,7 +33,7 @@ func main() {
}, },
} }
rtr.Get("/login",renderer.Template( rtr.Get("/login", renderer.Template(
"templates/login.html")) "templates/login.html"))
rtr.Post("/login", middleware.Authorize("/", udb, "/login?tryagain=1")) rtr.Post("/login", middleware.Authorize("/", udb, "/login?tryagain=1"))
@ -52,13 +52,13 @@ func main() {
"templates/header.html", "templates/header.html",
"templates/footer.html"), cfg.Adapter), http.MethodGet, udb, "/login"))) "templates/footer.html"), cfg.Adapter), http.MethodGet, udb, "/login")))
rtr.Post(`/save/(?P<Slug>\S+)`, middleware.Defend(middleware.Protected( rtr.Post(`/save/(?P<Slug>\S+)`, middleware.Defend(middleware.Protected(
shell.WithAdapter( shell.WithAdapter(
shell.EnsurePageData( shell.EnsurePageData(
renderer.Template( renderer.Template(
"templates/cms_save.html", "templates/cms_save.html",
"templates/header.html", "templates/header.html",
"templates/footer.html")), cfg.Adapter), http.MethodGet, udb, "/login"), udb, "/")) "templates/footer.html")), cfg.Adapter), http.MethodGet, udb, "/login"), udb, "/"))
http.ListenAndServe(":8080", rtr) http.ListenAndServe(":8080", rtr)
} }