136 lines
2.9 KiB
Go
136 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/sha512"
|
|
"encoding/hex"
|
|
"html/template"
|
|
"igar/storage"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo"
|
|
"github.com/labstack/echo/middleware"
|
|
//"github.com/labstack/echo/middleware"
|
|
)
|
|
|
|
type (
|
|
TemplateRenderer struct {
|
|
template *template.Template
|
|
}
|
|
)
|
|
|
|
var (
|
|
list *storage.EntryMap = &storage.EntryMap{
|
|
Filename: "/var/lib/igar/list.json",
|
|
}
|
|
)
|
|
|
|
// FUNCTIONS
|
|
|
|
func genName() string {
|
|
return strings.Replace(uuid.New().String(), "-", "", -1)
|
|
}
|
|
|
|
// TemplateRenderer
|
|
|
|
func (renderer *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
return renderer.template.ExecuteTemplate(w, name, data)
|
|
}
|
|
|
|
func IndexController(c echo.Context) (err error) {
|
|
entries, err := list.GetEntries()
|
|
if err == nil {
|
|
err = c.Render(http.StatusOK, "index.html", entries)
|
|
}
|
|
return
|
|
}
|
|
|
|
func ViewController(c echo.Context) (err error) {
|
|
uuid := c.QueryParam("uuid")
|
|
entry, err := list.GetEntry(uuid)
|
|
if err == nil {
|
|
err = c.Render(http.StatusOK, "view.html", entry)
|
|
}
|
|
if err != nil {
|
|
c.Logger().Error(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func AdminController(c echo.Context) (err error) {
|
|
entries, err := list.GetEntries()
|
|
if err == nil {
|
|
err = c.Render(http.StatusOK, "admin.html", entries)
|
|
}
|
|
return
|
|
}
|
|
|
|
func PostController(c echo.Context) (err error) {
|
|
desc := c.FormValue("desc")
|
|
date := c.FormValue("date")
|
|
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
entry := storage.NewEntry(desc, date)
|
|
entry.GeneratePostID()
|
|
entry.AddMedia(file)
|
|
|
|
list.AddEntry(entry)
|
|
|
|
return c.String(http.StatusCreated, "")
|
|
}
|
|
|
|
func DeleteController(c echo.Context) (err error) {
|
|
uuid := c.QueryParam("uuid")
|
|
entry := list.RemoveEntry(uuid)
|
|
entry.RemoveFiles()
|
|
if entry == nil {
|
|
return c.String(http.StatusNotFound, "")
|
|
}
|
|
return c.String(http.StatusOK, "")
|
|
}
|
|
|
|
func auth(username, password string, c echo.Context) (bool, error) {
|
|
adminUsername := "danolo"
|
|
adminPassword := "bd4cad796950f50352225de3c773d8f3c39622bc17f34ad661eabe615cdf6d32751c5751e0648dc17d890f40330018334a2ae899878f200f6dc80121ddb70cc9"
|
|
hash := sha512.Sum512([]byte(password))
|
|
hashString := hex.EncodeToString(hash[:])
|
|
if username == adminUsername && hashString == adminPassword {
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func main() {
|
|
e := echo.New()
|
|
|
|
e.Renderer = &TemplateRenderer{
|
|
template: template.Must(template.ParseGlob("templates/*.html")),
|
|
}
|
|
|
|
e.GET("/", IndexController)
|
|
e.GET("/view", ViewController)
|
|
|
|
//e.GET("/rss", server.RSSController)
|
|
//e.GET("/json", server.JSONController)
|
|
//e.GET("/atom", server.AtomController)
|
|
|
|
e.Static("/static", "static")
|
|
e.Static("/img", "/var/lib/igar")
|
|
|
|
admin := e.Group("/admin", middleware.BasicAuth(auth))
|
|
|
|
admin.GET("/", AdminController)
|
|
admin.POST("/post", PostController)
|
|
admin.DELETE("/post", DeleteController)
|
|
|
|
admin.Static("/static", "admin/static")
|
|
|
|
e.Logger.Fatal(e.Start(":40404"))
|
|
}
|