diff --git a/main.go b/main.go index acfbac1..0c689c7 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/dalu/wiki/wiki/storage/mongo" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" + "github.com/dalu/wiki/wiki/template" ) func main() { @@ -25,15 +26,17 @@ func main() { log.Fatal("initializeWiki: ", e.Error()) } + renderer := template.New(true) + mux := http.NewServeMux() - wikiHandler := wiki.NewWikiHandler(mongostore, log) + wikiHandler := wiki.NewWikiHandler(mongostore, renderer, log) mux.Handle(wiki.DefaultMountPath, http.StripPrefix(wiki.DefaultMountPath, wikiHandler)) log.Fatal(http.ListenAndServe(":8080", mux)) } -func initializeWiki(s storage.WikiStorage) error { +func initializeWiki(s storage.Interface) error { _, e := s.GetTermByName("") if e != nil { if e == mgo.ErrNotFound { diff --git a/views/base.html.twig b/views/base.html.twig new file mode 100644 index 0000000..e69de29 diff --git a/views/layout.html.twig b/views/layout.html.twig new file mode 100644 index 0000000..7a050a9 --- /dev/null +++ b/views/layout.html.twig @@ -0,0 +1,13 @@ +{% extends "base.html.twig" %} +{% block css %} +{% endblock %} +{% block body %} + {% block nav %}{% endblock %} + {% block header %}{% endblock %} + {% block left %}{% endblock %} + {% block middle %}{% endblock %} + {% block right %}{% endblock %} + {% block footer %}{% endblock %} +{% endblock %} +{% block js %} +{% endblock %} \ No newline at end of file diff --git a/wiki/handler.go b/wiki/handler.go index 94448b6..f0b98b1 100644 --- a/wiki/handler.go +++ b/wiki/handler.go @@ -1,11 +1,12 @@ package wiki import ( - "encoding/json" + //"encoding/json" "net/http" "github.com/Sirupsen/logrus" "github.com/dalu/wiki/wiki/storage" + "github.com/dalu/wiki/wiki/template" ) const DefaultMountPath = "/wiki/" @@ -16,14 +17,16 @@ const ( ) type wikiHandler struct { - s storage.WikiStorage + s storage.Interface + r template.Interface l *logrus.Logger } -func NewWikiHandler(s storage.WikiStorage, l *logrus.Logger) *wikiHandler { +func NewWikiHandler(s storage.Interface, r template.Interface, l *logrus.Logger) *wikiHandler { l.Level = logrus.DebugLevel return &wikiHandler{ s: s, + r: r, l: l, } } @@ -80,9 +83,20 @@ func (h *wikiHandler) show(w http.ResponseWriter, r *http.Request) { h.l.Error("wikihandler.show.GetRevisionByTermID:", e) return } + + ctx := make(map[string]interface{}) + ctx["term"] = term + ctx["revision"] = revision + + if e := h.r.Render(w, "wiki/show.html.twig", 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) } + */ } diff --git a/wiki/storage/interface.go b/wiki/storage/interface.go index d35da88..0f936bb 100644 --- a/wiki/storage/interface.go +++ b/wiki/storage/interface.go @@ -1,11 +1,11 @@ package storage -type WikiStorage interface { - TermStorage - RevisionStorage +type Interface interface { + TermInterface + RevisionInterface } -type TermStorage interface { +type TermInterface interface { CreateTerm(term *Term) error UpdateTerm(term *Term) error RemoveTerm(term *Term) error @@ -15,7 +15,7 @@ type TermStorage interface { GetTerms() ([]*Term, error) } -type RevisionStorage interface { +type RevisionInterface interface { CreateRevision(revision *Revision) error RemoveRevision(revision *Revision) error RemoveRevisionsByTermID(termID string) error diff --git a/wiki/template/interface.go b/wiki/template/interface.go new file mode 100644 index 0000000..1c47819 --- /dev/null +++ b/wiki/template/interface.go @@ -0,0 +1,7 @@ +package template + +import "io" + +type Interface interface { + Render(w io.Writer, name string, data interface{}) error +} diff --git a/wiki/template/renderer.go b/wiki/template/renderer.go new file mode 100644 index 0000000..991d78d --- /dev/null +++ b/wiki/template/renderer.go @@ -0,0 +1,50 @@ +package template + +import ( + "io" + + "github.com/GeertJohan/go.rice" + "github.com/flosch/pongo2" +) + +type loader struct { + ricebox *rice.Box +} + +func (loader) Abs(base, name string) string { + return name +} + +func (l loader) Get(path string) (io.Reader, error) { + return l.ricebox.Open(path) +} + +func New(debug bool) *Renderer { + l := loader{ + ricebox: rice.MustFindBox("views"), + } + set := pongo2.NewSet("wiki", l) + set.Debug = debug + return &Renderer{ + set: set, + } +} + +type Renderer struct { + set *pongo2.TemplateSet +} + +func (r *Renderer) Render(w io.Writer, name string, data interface{}) error { + t ,e := r.set.FromCache(name) + if e != nil { + return e + } + return t.ExecuteWriter(pongoContext(data), 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/base.html.twig b/wiki/template/views/base.html.twig new file mode 100644 index 0000000..e36339f --- /dev/null +++ b/wiki/template/views/base.html.twig @@ -0,0 +1,12 @@ + + +
+ +