worked on handler and templates

This commit is contained in:
Darko Luketic 2017-02-11 23:13:16 +01:00
parent 00a88254b0
commit ebe1360dc0
8 changed files with 72 additions and 29 deletions

11
main.go
View File

@ -8,9 +8,9 @@ import (
"github.com/dalu/wiki/wiki" "github.com/dalu/wiki/wiki"
"github.com/dalu/wiki/wiki/storage" "github.com/dalu/wiki/wiki/storage"
"github.com/dalu/wiki/wiki/storage/mongo" "github.com/dalu/wiki/wiki/storage/mongo"
"github.com/dalu/wiki/wiki/template"
"gopkg.in/mgo.v2" "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
"github.com/dalu/wiki/wiki/template"
) )
func main() { func main() {
@ -33,7 +33,14 @@ func main() {
wikiHandler := wiki.NewWikiHandler(mongostore, renderer, log) wikiHandler := wiki.NewWikiHandler(mongostore, renderer, log)
mux.Handle(wiki.DefaultMountPath, http.StripPrefix(wiki.DefaultMountPath, wikiHandler)) 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 { func initializeWiki(s storage.Interface) error {

View File

@ -1,12 +1,13 @@
package wiki package wiki
import ( import (
//"encoding/json"
"net/http" "net/http"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/dalu/wiki/wiki/storage" "github.com/dalu/wiki/wiki/storage"
"github.com/dalu/wiki/wiki/template" "github.com/dalu/wiki/wiki/template"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
) )
const DefaultMountPath = "/wiki/" const DefaultMountPath = "/wiki/"
@ -47,12 +48,11 @@ func (h *wikiHandler) handleGET(w http.ResponseWriter, r *http.Request) {
switch action { switch action {
case "": case "":
h.l.Debug("show hit")
h.show(w, r) h.show(w, r)
case "edit": case "edit":
h.l.Debug("edit hit") h.l.Debug("edit hit")
case "new": case "create":
h.l.Debug("new hit") h.create(w, r)
case "revision": case "revision":
h.l.Debug("revision hit") h.l.Debug("revision hit")
case "revisions": 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) { func (h *wikiHandler) show(w http.ResponseWriter, r *http.Request) {
h.l.Debug("show hit")
term, e := h.s.GetTermBySlug(r.URL.Path) term, e := h.s.GetTermBySlug(r.URL.Path)
if e != nil { if e != nil {
h.l.Error("wikihandler.show.GetTermBySlug:", e) h.notFound(w, r)
return return
} }
if term.Redirect != "" { if term.Redirect != "" {
@ -75,7 +76,8 @@ func (h *wikiHandler) show(w http.ResponseWriter, r *http.Request) {
return return
} }
if term.CurrentRevisionID == "" { 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 return
} }
revision, e := h.s.GetRevisionByID(term.CurrentRevisionID) revision, e := h.s.GetRevisionByID(term.CurrentRevisionID)
@ -84,19 +86,31 @@ func (h *wikiHandler) show(w http.ResponseWriter, r *http.Request) {
return return
} }
revision.HTML = string(bluemonday.UGCPolicy().SanitizeBytes(blackfriday.MarkdownCommon([]byte(revision.Text))))
ctx := make(map[string]interface{}) ctx := make(map[string]interface{})
ctx["term"] = term ctx["term"] = term
ctx["revision"] = revision 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) 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")
}

View File

@ -12,7 +12,7 @@ type Term struct {
Language string `bson:"lang"` Language string `bson:"lang"`
Name string `bson:"name"` Name string `bson:"name"`
CurrentRevisionID string `bson:"revision_id,omitempty"` CurrentRevisionID string `bson:"revision_id,omitempty"`
Redirect string `bson:"redirect,omitempty"` Redirect string `bson:"redirect,omitempty"`
} }
type Revision struct { type Revision struct {
@ -21,4 +21,5 @@ type Revision struct {
AuthorIP string `bson:"author_ip"` AuthorIP string `bson:"author_ip"`
PublishDate time.Time `bson:"date"` PublishDate time.Time `bson:"date"`
Text string `bson:"text"` Text string `bson:"text"`
HTML string `bson:"html,omitempty"`
} }

View File

@ -1,12 +1,16 @@
package template package template
import ( import (
"errors"
"io" "io"
"github.com/GeertJohan/go.rice" "github.com/GeertJohan/go.rice"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
_ "github.com/flosch/pongo2-addons"
) )
const DefaultExtension = ".html.twig"
type loader struct { type loader struct {
ricebox *rice.Box ricebox *rice.Box
} }
@ -35,16 +39,14 @@ type Renderer struct {
} }
func (r *Renderer) Render(w io.Writer, name string, data interface{}) error { 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 { if e != nil {
return e 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
}

View File

@ -1 +1,3 @@
{% extends "layout.html.twig" %} {% extends "layout.html.twig" %}
{% block middle %}
{% endblock %}

View File

@ -0,0 +1,10 @@
{% extends "layout.html.twig" %}
{% block title %}{{ term }}{% endblock %}
{% block middle %}
From wiki, the free encyclopedia<br>
<h1>{{ term }}</h1>
<p>
wiki does not have an article with the exact name.<br>
Would you like to <a href="/wiki/{{ path }}?action=create">create</a> it?
</p>
{% endblock %}

View File

@ -1,5 +1,6 @@
{% extends "layout.html.twig" %} {% extends "layout.html.twig" %}
{% block title %}{{ term.Name }}{% endblock %}
{% block middle %} {% block middle %}
<h1>{{ term.Name }}</h1> From wiki, the free encyclopedia<br>
{{ revision.Text }} {{ revision.HTML | markdown}}
{% endblock %} {% endblock %}

View File

@ -1,5 +1,11 @@
package wiki package wiki
func generateSlug() {} import "strings"
func slugify() {}
func unslugify(in string) string {
return strings.Replace(in, "_", " ", -1)
}
func diff(text1, text2 string) {} func diff(text1, text2 string) {}