EurekaAdapter: flesh out CreatePage, SavePage, DeletePage and added TODO comments and the Config map; gofmt
This commit is contained in:
parent
1bc8eac481
commit
b9d971140a
5 changed files with 62 additions and 35 deletions
|
@ -3,8 +3,8 @@ package archetype
|
|||
type Adapter interface {
|
||||
Init(cfg *Config)
|
||||
Name() string
|
||||
EditableSlugs() bool
|
||||
BuildOptions() ([]string)
|
||||
EditableSlugs() bool
|
||||
BuildOptions() []string
|
||||
GetConfig(key string) (interface{}, error)
|
||||
SetConfig(key string, value interface{}) error
|
||||
ListPages() map[string]string
|
||||
|
|
|
@ -9,7 +9,8 @@ import (
|
|||
)
|
||||
|
||||
type EurekaAdapter struct {
|
||||
Root string
|
||||
Root string
|
||||
Config map[string]interface{}
|
||||
}
|
||||
|
||||
func (self *EurekaAdapter) Init(cfg *Config) {
|
||||
|
@ -21,6 +22,8 @@ func (self *EurekaAdapter) Init(cfg *Config) {
|
|||
}
|
||||
|
||||
self.Root = cfg.Root
|
||||
|
||||
// TODO: read config.h and build self.Config
|
||||
}
|
||||
|
||||
func (self *EurekaAdapter) Name() string {
|
||||
|
@ -28,11 +31,11 @@ func (self *EurekaAdapter) Name() string {
|
|||
}
|
||||
|
||||
func (self *EurekaAdapter) EditableSlugs() bool {
|
||||
return false
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *EurekaAdapter) BuildOptions() []string {
|
||||
return []string{"twtxt"}
|
||||
return []string{"twtxt"}
|
||||
}
|
||||
|
||||
func (self *EurekaAdapter) GetConfig(key string) (interface{}, error) {
|
||||
|
@ -81,32 +84,56 @@ func (self *EurekaAdapter) GetPage(filename string) (Page, error) {
|
|||
|
||||
return Page{
|
||||
Title: title,
|
||||
Slug: filename,
|
||||
Slug: filename,
|
||||
Content: content,
|
||||
Edited: fileInfo.ModTime(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (self *EurekaAdapter) FormatPage(raw string) string {
|
||||
// TODO: implement Eureka formatter to show preview
|
||||
return raw
|
||||
}
|
||||
|
||||
func (self *EurekaAdapter) FormattingHelp() string {
|
||||
// TODO: show Eureka formatting guide
|
||||
return "help!"
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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) {
|
||||
// TODO: shell out to build.sh with buildOptions, record exit status and output
|
||||
return true, "Build successful"
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
|
||||
type Page struct {
|
||||
Title string
|
||||
Slug string
|
||||
Slug string
|
||||
Content string
|
||||
Edited time.Time
|
||||
}
|
||||
|
|
|
@ -2,9 +2,9 @@ package lfo
|
|||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"net/http"
|
||||
core "nilfm.cc/git/nirvash/archetype"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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 {
|
||||
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
||||
pageTitle := req.FormValue("title")
|
||||
pageContent := req.FormValue("content")
|
||||
|
||||
if pageTitle == "" || pageContent == "" {
|
||||
newUri := "/edit/"
|
||||
slug := strings.Join(strings.Split(req.URL.Path, "/")[2:], "/")
|
||||
newUri += slug
|
||||
req.Method = http.MethodGet
|
||||
http.Redirect(w, req, newUri, http.StatusSeeOther)
|
||||
} else {
|
||||
next.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
return http.HandlerFunc(handlerFunc)
|
||||
}
|
||||
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
||||
pageTitle := req.FormValue("title")
|
||||
pageContent := req.FormValue("content")
|
||||
|
||||
if pageTitle == "" || pageContent == "" {
|
||||
newUri := "/edit/"
|
||||
slug := strings.Join(strings.Split(req.URL.Path, "/")[2:], "/")
|
||||
newUri += slug
|
||||
req.Method = http.MethodGet
|
||||
http.Redirect(w, req, newUri, http.StatusSeeOther)
|
||||
} else {
|
||||
next.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
return http.HandlerFunc(handlerFunc)
|
||||
}
|
||||
|
|
18
nirvash.go
18
nirvash.go
|
@ -33,7 +33,7 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
rtr.Get("/login",renderer.Template(
|
||||
rtr.Get("/login", renderer.Template(
|
||||
"templates/login.html"))
|
||||
|
||||
rtr.Post("/login", middleware.Authorize("/", udb, "/login?tryagain=1"))
|
||||
|
@ -51,14 +51,14 @@ func main() {
|
|||
"templates/cms_edit.html",
|
||||
"templates/header.html",
|
||||
"templates/footer.html"), cfg.Adapter), http.MethodGet, udb, "/login")))
|
||||
|
||||
rtr.Post(`/save/(?P<Slug>\S+)`, middleware.Defend(middleware.Protected(
|
||||
shell.WithAdapter(
|
||||
shell.EnsurePageData(
|
||||
renderer.Template(
|
||||
"templates/cms_save.html",
|
||||
"templates/header.html",
|
||||
"templates/footer.html")), cfg.Adapter), http.MethodGet, udb, "/login"), udb, "/"))
|
||||
|
||||
rtr.Post(`/save/(?P<Slug>\S+)`, middleware.Defend(middleware.Protected(
|
||||
shell.WithAdapter(
|
||||
shell.EnsurePageData(
|
||||
renderer.Template(
|
||||
"templates/cms_save.html",
|
||||
"templates/header.html",
|
||||
"templates/footer.html")), cfg.Adapter), http.MethodGet, udb, "/login"), udb, "/"))
|
||||
|
||||
http.ListenAndServe(":8080", rtr)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue