worked on handler and templates
This commit is contained in:
parent
00a88254b0
commit
ebe1360dc0
11
main.go
11
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 {
|
||||
|
@ -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")
|
||||
|
||||
}
|
||||
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -1 +1,3 @@
|
||||
{% extends "layout.html.twig" %}
|
||||
{% block middle %}
|
||||
{% endblock %}
|
10
wiki/template/views/wiki/not-found.html.twig
Normal file
10
wiki/template/views/wiki/not-found.html.twig
Normal 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 %}
|
@ -1,5 +1,6 @@
|
||||
{% extends "layout.html.twig" %}
|
||||
{% block title %}{{ term.Name }}{% endblock %}
|
||||
{% block middle %}
|
||||
<h1>{{ term.Name }}</h1>
|
||||
{{ revision.Text }}
|
||||
From wiki, the free encyclopedia<br>
|
||||
{{ revision.HTML | markdown}}
|
||||
{% endblock %}
|
@ -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) {}
|
Loading…
Reference in New Issue
Block a user