wikingo/server/app.go

115 lines
2.7 KiB
Go
Raw Normal View History

package server
import (
"fmt"
2022-03-23 00:25:58 +01:00
"log"
"net/http"
"strings"
2022-03-28 17:14:01 +02:00
"github.com/oscarmlage/wikingo/model"
"github.com/labstack/echo/v4"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
)
func WikiHome(c echo.Context) error {
2022-03-23 00:24:40 +01:00
return c.Redirect(http.StatusMovedPermanently, "/WikiHome")
2022-03-22 16:36:18 +01:00
}
func WikiPage(c echo.Context) error {
2022-03-28 17:14:01 +02:00
var page model.Page
2022-03-22 16:36:18 +01:00
var err error
2022-03-28 17:14:01 +02:00
page_id := c.Param("page")
version := c.Param("version")
if version != "" {
fmt.Printf("version: %s", version)
page, err = store.GetPageVersion(page_id, version)
if err != nil {
// If record not found, show 404
if err.Error() == "record not found" {
return c.Render(http.StatusOK, "notfound.html", map[string]interface{}{
"name": page_id,
})
}
}
} else {
var err error
page, err = store.GetPage(page_id)
if err != nil {
// If record not found, show the create
if err.Error() == "record not found" {
return c.Render(http.StatusOK, "notfound.html", map[string]interface{}{
"name": page_id,
})
}
2022-03-22 16:36:18 +01:00
}
}
fmt.Printf("res: %T\n", page)
2022-03-23 00:25:18 +01:00
body := strings.ReplaceAll(page.Body, "\r\n", "\n")
unsafe := blackfriday.Run([]byte(body))
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
2022-03-22 16:36:18 +01:00
return c.Render(http.StatusOK, "page.html", map[string]interface{}{
"page": page,
"html": string(html[:]),
2022-03-22 16:36:18 +01:00
})
}
func WikiPageEdit(c echo.Context) error {
2022-03-28 17:14:01 +02:00
var page model.Page
2022-03-22 16:36:18 +01:00
var err error
2022-03-28 17:14:01 +02:00
page_id := c.Param("page")
version := c.Param("version")
if version != "" {
fmt.Printf("version; %s\n", version)
page, err = store.GetPageVersion(page_id, version)
} else {
fmt.Println("No version")
page, err = store.GetPage(page_id)
}
2022-03-22 16:36:18 +01:00
if err != nil {
// If record not found, create and show the form
if err.Error() == "record not found" {
err := store.AddPage(page_id, "# "+page_id)
if err != nil {
log.Panicln(err.Error())
}
page, err = store.GetPage(page_id)
if err != nil {
log.Panicln(err)
}
}
}
fmt.Printf("res: %T\n", page)
return c.Render(http.StatusOK, "edit.html", map[string]interface{}{
"page": page,
})
}
func WikiPagePostEdit(c echo.Context) error {
page_id := c.Param("page")
var err error
page, err := store.GetPage(page_id)
if err != nil {
log.Panic(err.Error())
}
page.Body = c.FormValue("html")
store.CreatePage(page)
return c.Redirect(http.StatusMovedPermanently, "/"+page.Name)
}
func WikiList(c echo.Context) error {
pages, err := store.GetAllPages()
if err != nil {
log.Panicln(err)
}
2022-03-22 16:36:18 +01:00
fmt.Printf("res: %T\n", pages)
return c.Render(http.StatusOK, "list.html", map[string]interface{}{
"pages": pages,
2022-03-19 00:20:31 +01:00
})
}
func WikiAbout(c echo.Context) error {
2022-03-22 16:36:18 +01:00
return c.Render(http.StatusOK, "about.html", map[string]interface{}{})
}