From ebe1360dc0de2dca03b4f4b27154e3fc75d41eb0 Mon Sep 17 00:00:00 2001 From: Darko Luketic Date: Sat, 11 Feb 2017 23:13:16 +0100 Subject: [PATCH] worked on handler and templates --- main.go | 11 ++++- wiki/handler.go | 44 ++++++++++++------- wiki/storage/model.go | 3 +- wiki/template/renderer.go | 18 ++++---- .../wiki/{new.html.twig => create.html.twig} | 2 + wiki/template/views/wiki/not-found.html.twig | 10 +++++ wiki/template/views/wiki/show.html.twig | 5 ++- wiki/wiki.go | 8 +++- 8 files changed, 72 insertions(+), 29 deletions(-) rename wiki/template/views/wiki/{new.html.twig => create.html.twig} (50%) create mode 100644 wiki/template/views/wiki/not-found.html.twig diff --git a/main.go b/main.go index 0c689c7..edcae6a 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,9 @@ import ( "github.com/dalu/wiki/wiki" "github.com/dalu/wiki/wiki/storage" "github.com/dalu/wiki/wiki/storage/mongo" + "github.com/dalu/wiki/wiki/template" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" - "github.com/dalu/wiki/wiki/template" ) func main() { @@ -33,7 +33,14 @@ func main() { wikiHandler := wiki.NewWikiHandler(mongostore, renderer, log) mux.Handle(wiki.DefaultMountPath, http.StripPrefix(wiki.DefaultMountPath, wikiHandler)) - log.Fatal(http.ListenAndServe(":8080", mux)) + server := &http.Server{ + Handler: mux, + Addr: ":8080", + WriteTimeout: 15 * time.Second, + ReadTimeout: 15 * time.Second, + } + + log.Fatal(server.ListenAndServe()) } func initializeWiki(s storage.Interface) error { diff --git a/wiki/handler.go b/wiki/handler.go index f0b98b1..4a683b6 100644 --- a/wiki/handler.go +++ b/wiki/handler.go @@ -1,12 +1,13 @@ package wiki import ( - //"encoding/json" "net/http" "github.com/Sirupsen/logrus" "github.com/dalu/wiki/wiki/storage" "github.com/dalu/wiki/wiki/template" + "github.com/microcosm-cc/bluemonday" + "github.com/russross/blackfriday" ) const DefaultMountPath = "/wiki/" @@ -47,12 +48,11 @@ func (h *wikiHandler) handleGET(w http.ResponseWriter, r *http.Request) { switch action { case "": - h.l.Debug("show hit") h.show(w, r) case "edit": h.l.Debug("edit hit") - case "new": - h.l.Debug("new hit") + case "create": + h.create(w, r) case "revision": h.l.Debug("revision hit") case "revisions": @@ -65,9 +65,10 @@ func (h *wikiHandler) handlePOST(w http.ResponseWriter, r *http.Request) {} // ----- func (h *wikiHandler) show(w http.ResponseWriter, r *http.Request) { + h.l.Debug("show hit") term, e := h.s.GetTermBySlug(r.URL.Path) if e != nil { - h.l.Error("wikihandler.show.GetTermBySlug:", e) + h.notFound(w, r) return } if term.Redirect != "" { @@ -75,7 +76,8 @@ func (h *wikiHandler) show(w http.ResponseWriter, r *http.Request) { return } if term.CurrentRevisionID == "" { - http.Redirect(w, r, DefaultMountPath+r.URL.Path+"?action=new", http.StatusTemporaryRedirect) + h.notFound(w, r) + //http.Redirect(w, r, DefaultMountPath+r.URL.Path+"?action=new", http.StatusTemporaryRedirect) return } revision, e := h.s.GetRevisionByID(term.CurrentRevisionID) @@ -84,19 +86,31 @@ func (h *wikiHandler) show(w http.ResponseWriter, r *http.Request) { return } + revision.HTML = string(bluemonday.UGCPolicy().SanitizeBytes(blackfriday.MarkdownCommon([]byte(revision.Text)))) + ctx := make(map[string]interface{}) ctx["term"] = term ctx["revision"] = revision - if e := h.r.Render(w, "wiki/show.html.twig", ctx); e != nil { + if e := h.r.Render(w, "wiki/show", ctx); e != nil { h.l.Error("wikiHandler.show.Renderer.write:", e) } - - // temporary - /* - if e := json.NewEncoder(w).Encode(revision); e != nil { - h.l.Error("wikihandler.show.NewEncoder:", e) - http.Error(w, e.Error(), http.StatusInternalServerError) - } - */ } + +func (h *wikiHandler) notFound(w http.ResponseWriter, r *http.Request) { + h.l.Debug("not found hit") + w.WriteHeader(http.StatusNotFound) + + ctx := make(map[string]interface{}) + ctx["term"] = unslugify(r.URL.Path) + ctx["path"] = r.URL.Path + if e := h.r.Render(w, "wiki/not-found", ctx); e != nil { + h.l.Error("wikiHandler.notFound.Renderer.write:", e) + } +} + +func (h *wikiHandler) create(w http.ResponseWriter, r *http.Request) { + h.l.Debug("create hit") + +} + diff --git a/wiki/storage/model.go b/wiki/storage/model.go index 9de9c7d..5b29b5b 100644 --- a/wiki/storage/model.go +++ b/wiki/storage/model.go @@ -12,7 +12,7 @@ type Term struct { Language string `bson:"lang"` Name string `bson:"name"` CurrentRevisionID string `bson:"revision_id,omitempty"` - Redirect string `bson:"redirect,omitempty"` + Redirect string `bson:"redirect,omitempty"` } type Revision struct { @@ -21,4 +21,5 @@ type Revision struct { AuthorIP string `bson:"author_ip"` PublishDate time.Time `bson:"date"` Text string `bson:"text"` + HTML string `bson:"html,omitempty"` } diff --git a/wiki/template/renderer.go b/wiki/template/renderer.go index 991d78d..f73fd53 100644 --- a/wiki/template/renderer.go +++ b/wiki/template/renderer.go @@ -1,12 +1,16 @@ package template import ( + "errors" "io" "github.com/GeertJohan/go.rice" "github.com/flosch/pongo2" + _ "github.com/flosch/pongo2-addons" ) +const DefaultExtension = ".html.twig" + type loader struct { ricebox *rice.Box } @@ -35,16 +39,14 @@ type Renderer struct { } func (r *Renderer) Render(w io.Writer, name string, data interface{}) error { - t ,e := r.set.FromCache(name) + t ,e := r.set.FromCache(name+DefaultExtension) if e != nil { return e } - return t.ExecuteWriter(pongoContext(data), w) + ctx, ok := data.(map[string]interface{}) + if !ok { + return errors.New("data is compatible with map[string]interface{}") + } + return t.ExecuteWriter(ctx, w) } -func pongoContext(data interface{}) pongo2.Context { - if ctx, ok := data.(map[string]interface{}); ok { - return ctx - } - return nil -} \ No newline at end of file diff --git a/wiki/template/views/wiki/new.html.twig b/wiki/template/views/wiki/create.html.twig similarity index 50% rename from wiki/template/views/wiki/new.html.twig rename to wiki/template/views/wiki/create.html.twig index 6a5d555..fa4d567 100644 --- a/wiki/template/views/wiki/new.html.twig +++ b/wiki/template/views/wiki/create.html.twig @@ -1 +1,3 @@ {% extends "layout.html.twig" %} +{% block middle %} +{% endblock %} \ No newline at end of file diff --git a/wiki/template/views/wiki/not-found.html.twig b/wiki/template/views/wiki/not-found.html.twig new file mode 100644 index 0000000..a598ab9 --- /dev/null +++ b/wiki/template/views/wiki/not-found.html.twig @@ -0,0 +1,10 @@ +{% extends "layout.html.twig" %} +{% block title %}{{ term }}{% endblock %} +{% block middle %} + From wiki, the free encyclopedia
+

{{ term }}

+

+ wiki does not have an article with the exact name.
+ Would you like to create it? +

+{% endblock %} \ No newline at end of file diff --git a/wiki/template/views/wiki/show.html.twig b/wiki/template/views/wiki/show.html.twig index e638f8f..ba20f8f 100644 --- a/wiki/template/views/wiki/show.html.twig +++ b/wiki/template/views/wiki/show.html.twig @@ -1,5 +1,6 @@ {% extends "layout.html.twig" %} +{% block title %}{{ term.Name }}{% endblock %} {% block middle %} -

{{ term.Name }}

- {{ revision.Text }} + From wiki, the free encyclopedia
+ {{ revision.HTML | markdown}} {% endblock %} \ No newline at end of file diff --git a/wiki/wiki.go b/wiki/wiki.go index 2d523f0..6fd55ef 100644 --- a/wiki/wiki.go +++ b/wiki/wiki.go @@ -1,5 +1,11 @@ package wiki -func generateSlug() {} +import "strings" + +func slugify() {} + +func unslugify(in string) string { + return strings.Replace(in, "_", " ", -1) +} func diff(text1, text2 string) {} \ No newline at end of file