renderer added, storage + renderer interfaces renamed to Interface

This commit is contained in:
Darko Luketic 2017-02-11 20:36:01 +01:00
parent d63dba41a8
commit e92d895690
13 changed files with 130 additions and 10 deletions

View File

@ -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 {

0
views/base.html.twig Normal file
View File

13
views/layout.html.twig Normal file
View File

@ -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 %}

View File

@ -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)
}
*/
}

View File

@ -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

View File

@ -0,0 +1,7 @@
package template
import "io"
type Interface interface {
Render(w io.Writer, name string, data interface{}) error
}

50
wiki/template/renderer.go Normal file
View File

@ -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
}

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="{% block lang %}{% endblock %}">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
{% block css %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
{% block js %}{% endblock %}
</body>
</html>

View File

@ -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 %}

View File

@ -0,0 +1 @@
{% extends "layout.html.twig" %}

View File

@ -0,0 +1 @@
{% extends "layout.html.twig" %}

View File

@ -0,0 +1,5 @@
{% extends "layout.html.twig" %}
{% block middle %}
<h1>{{ term.Name }}</h1>
{{ revision.Text }}
{% endblock %}

1
wiki/templates/loader.go Normal file
View File

@ -0,0 +1 @@
package templates