Commit dec3ed61 authored by Artemis's avatar Artemis

Moved templates into a single Template object, split html templates

parent cdda4e50
......@@ -4,13 +4,14 @@ import (
"fmt"
"github.com/go-redis/redis"
"github.com/husobee/vestigo"
"html/template"
"net/http"
"time"
)
type Handlers struct {
Backend *redis.Client
Templates *Templates
Templates *template.Template
}
type Content struct {
......@@ -32,10 +33,7 @@ func (h Handlers) GetRouter() *vestigo.Router {
}
func (h Handlers) HandleHome(w http.ResponseWriter, req *http.Request) {
err := h.Templates.homepage.Execute(w, nil)
if err != nil {
fmt.Println(err)
}
_ = h.Templates.ExecuteTemplate(w, "homepage", nil)
}
func (h Handlers) HandleNewPaste(w http.ResponseWriter, req *http.Request) {
......@@ -44,14 +42,14 @@ func (h Handlers) HandleNewPaste(w http.ResponseWriter, req *http.Request) {
if err != nil {
w.WriteHeader(400)
fmt.Println(err)
_ = h.Templates.homepage.Execute(w, &Content{Error: "Form submit error"})
_ = h.Templates.ExecuteTemplate(w, "homepage", &Content{Error: "Form submit error"})
return
}
if len(req.Form["paste"]) == 0 {
// Required value
w.WriteHeader(400)
_ = h.Templates.homepage.Execute(w, &Content{Error: "You must provide a value"})
_ = h.Templates.ExecuteTemplate(w, "homepage", &Content{Error: "You must provide a value"})
return
}
......@@ -59,7 +57,7 @@ func (h Handlers) HandleNewPaste(w http.ResponseWriter, req *http.Request) {
if pasteValue == "" {
// Required value
w.WriteHeader(400)
_ = h.Templates.homepage.Execute(w, &Content{Error: "You must provide a value", Value: pasteValue})
_ = h.Templates.ExecuteTemplate(w, "homepage", &Content{Error: "You must provide a value", Value: pasteValue})
return
}
......@@ -75,7 +73,7 @@ func (h Handlers) HandleNewPaste(w http.ResponseWriter, req *http.Request) {
if err := res.Err(); err != nil {
w.WriteHeader(500)
fmt.Println(err)
_ = h.Templates.homepage.Execute(w, &Content{Error: "Failed to store the value", Value: pasteValue})
_ = h.Templates.ExecuteTemplate(w, "homepage", &Content{Error: "Failed to store the value", Value: pasteValue})
return
}
......@@ -98,7 +96,7 @@ func (h Handlers) HandleShowPaste(w http.ResponseWriter, req *http.Request) {
return
}
err := h.Templates.show.Execute(w, &Content{
err := h.Templates.ExecuteTemplate(w, "show", &Content{
Value: res.Val(),
Raw: "/r/" + key,
})
......@@ -127,5 +125,5 @@ func (h Handlers) HandleShowRaw(w http.ResponseWriter, req *http.Request) {
}
func (h Handlers) HandlePrivacyPolicy(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "privacy.html")
_ = h.Templates.ExecuteTemplate(w, "privacy", nil)
}
......@@ -2,14 +2,6 @@ package main
import "html/template"
type Templates struct {
homepage *template.Template
show *template.Template
}
func InitTemplates() *Templates {
return &Templates{
homepage: template.Must(template.ParseFiles("templates/homepage.html")),
show: template.Must(template.ParseFiles("templates/show.html")),
}
func InitTemplates() *template.Template {
return template.Must(template.ParseGlob("templates/*"))
}
{{define "footer"}}
<hr>
<footer>
<a href="https://gitlab.com/Artemix/paste" target="_blank">Source code</a>
| <a href="/templates/privacy.html" target="_blank">Privacy Policy</a>
</footer>
{{end}}
{{define "header"}}
<head>
<meta charset="UTF-8">
<title>Paste</title>
<link rel="stylesheet" href="https://res.axdn.fr/paste/style.min.css" />
</head>
{{end}}
{{define "homepage"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Paste</title>
<link rel="stylesheet" href="https://res.axdn.fr/paste/style.min.css" />
</head>
{{template "header"}}
<body>
<h1>Paste</h1>
......@@ -28,11 +25,7 @@
<p>Pastes are stored for 1 hour or until next server restart, whichever comes first.</p>
</section>
<hr>
<footer>
<a href="https://gitlab.com/Artemix/paste" target="_blank">Source code</a>
| <a href="/privacy.html" target="_blank">Privacy Policy</a>
</footer>
{{template "footer"}}
</body>
</html>
{{end}}
{{define "privacy"}}
<!DOCTYPE html>
<html lang="en">
<head>
......@@ -15,3 +16,4 @@
<p>In that sense, we don't persist anything, and once the expiry time is passed, everything is forgotten.</p>
</body>
</html>
{{end}}
{{define "show"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Paste</title>
<link rel="stylesheet" href="https://res.axdn.fr/paste/style.min.css" />
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/monokai-sublime.min.css">
</head>
{{template "header"}}
<body>
<h1>Paste</h1>
......@@ -20,13 +15,9 @@
<pre><code>{{.Value}}</code></pre>
</section>
<hr>
<footer>
<a href="https://gitlab.com/Artemix/paste" target="_blank">Source code</a>
| <a href="/privacy.html" target="_blank">Privacy Policy</a>
</footer>
{{template "footer"}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</body>
</html>
{{end}}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment