2022-03-15 21:50:11 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-03-19 00:20:31 +01:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
|
2022-03-15 21:50:11 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/oscarmlage/wikingo/model"
|
|
|
|
)
|
|
|
|
|
2022-03-19 00:20:31 +01:00
|
|
|
// Define the template registry struct
|
|
|
|
type TemplateRegistry struct {
|
|
|
|
templates map[string]*template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement e.Renderer interface
|
|
|
|
func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
|
|
tmpl, ok := t.templates[name]
|
|
|
|
if !ok {
|
|
|
|
err := errors.New("Template not found -> " + name)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tmpl.ExecuteTemplate(w, "base.html", data)
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:15:55 +01:00
|
|
|
var (
|
2022-03-18 23:36:26 +01:00
|
|
|
store model.Store
|
2022-03-18 13:15:55 +01:00
|
|
|
)
|
2022-03-15 21:50:11 +01:00
|
|
|
|
|
|
|
func Serve() {
|
2022-03-18 23:36:26 +01:00
|
|
|
|
2022-03-18 13:15:55 +01:00
|
|
|
// Store instance
|
2022-03-18 23:36:26 +01:00
|
|
|
// Depending on config we should open one store or other
|
|
|
|
// (Gorm, File, Git...)
|
|
|
|
store = new(model.StoreGorm)
|
2022-03-15 21:50:11 +01:00
|
|
|
err := store.Open()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2022-03-28 21:11:10 +02:00
|
|
|
Debug.Printf("test.db open\n")
|
2022-03-18 13:15:55 +01:00
|
|
|
|
|
|
|
// Echo instance
|
2022-03-15 21:50:11 +01:00
|
|
|
e := echo.New()
|
2022-03-18 13:15:55 +01:00
|
|
|
|
2022-03-19 00:20:31 +01:00
|
|
|
// Instantiate a template registry with an array of template set
|
|
|
|
// Ref: https://gist.github.com/rand99/808e6e9702c00ce64803d94abff65678
|
|
|
|
templates := make(map[string]*template.Template)
|
2022-03-22 22:06:41 +01:00
|
|
|
|
|
|
|
templates["page.html"] = template.Must(template.New("page.html").Funcs(template.FuncMap{
|
|
|
|
"safeHTML": func(s string) template.HTML {
|
|
|
|
return template.HTML(s)
|
|
|
|
},
|
2022-03-28 21:50:10 +02:00
|
|
|
}).ParseFiles("templates/page.html", "templates/base.html"))
|
|
|
|
templates["list.html"] = template.Must(template.ParseFiles("templates/list.html", "templates/base.html"))
|
|
|
|
templates["edit.html"] = template.Must(template.ParseFiles("templates/edit.html", "templates/base.html"))
|
|
|
|
templates["about.html"] = template.Must(template.ParseFiles("templates/about.html", "templates/base.html"))
|
|
|
|
templates["notfound.html"] = template.Must(template.ParseFiles("templates/notfound.html", "templates/base.html"))
|
|
|
|
templates["404.html"] = template.Must(template.ParseFiles("templates/404.html", "templates/base.html"))
|
2022-03-19 00:20:31 +01:00
|
|
|
e.Renderer = &TemplateRegistry{
|
|
|
|
templates: templates,
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:15:55 +01:00
|
|
|
// Routes
|
2022-03-15 21:50:11 +01:00
|
|
|
e.GET("/", WikiHome)
|
2022-03-22 16:36:18 +01:00
|
|
|
e.GET("/:page", WikiPage)
|
2022-03-28 17:14:01 +02:00
|
|
|
e.GET("/:page/:version", WikiPage)
|
2022-03-22 16:36:18 +01:00
|
|
|
e.GET("/:page/edit", WikiPageEdit)
|
2022-03-28 17:14:01 +02:00
|
|
|
e.GET("/:page/:version/edit", WikiPageEdit)
|
2022-03-22 16:36:18 +01:00
|
|
|
e.POST("/:page/edit", WikiPagePostEdit)
|
2022-03-28 17:14:01 +02:00
|
|
|
e.POST("/:page/:version/edit", WikiPagePostEdit)
|
2022-03-22 16:36:18 +01:00
|
|
|
e.GET("/list", WikiList)
|
2022-03-15 21:50:11 +01:00
|
|
|
e.GET("/about", WikiAbout)
|
|
|
|
e.GET("/about/:id", WikiAbout)
|
|
|
|
|
2022-03-18 13:15:55 +01:00
|
|
|
// Logger
|
2022-03-15 21:50:11 +01:00
|
|
|
e.Logger.Fatal(e.Start(":2323"))
|
|
|
|
}
|