-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.go
49 lines (39 loc) · 991 Bytes
/
messages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package controllers
import (
"github.com/perthgophers/puddle/models"
"html/template"
"io/ioutil"
"log"
"net/http"
"path"
)
// loadFile locates the log and returns it
func loadFile() (*models.Page, error) {
filename := path.Join("logs", "log.txt")
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &models.Page{Body: template.HTML(body)}, nil
}
// renderTemplate locates and renders the html file
func renderTemplate(w http.ResponseWriter, p *models.Page) {
lp := path.Join("views", "log.html")
tmpl, err := template.ParseFiles(lp)
if err != nil {
log.Println(err)
}
tmpl.ExecuteTemplate(w, "log.html", p)
}
// ServeTastic executes the loadFile and renderTemplate functions
func ServeTastic(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "405: Method Not Allowed", http.StatusMethodNotAllowed)
} else {
p, err := loadFile()
if err != nil {
log.Println(err)
}
renderTemplate(w, p)
}
}