-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
60 lines (54 loc) · 1.67 KB
/
handlers.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
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"html/template"
"net/http"
"strconv"
)
func (app *application) home(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
// Initialize a slice containing the paths to the two files. It's important
// to note that the file containing our base template must be the *first*
// file in the slice.
files := []string{
"./ui/html/base.tmpl.html",
"./ui/html/partials/nav.tmpl.html",
"./ui/html/pages/home.tmpl.html",
}
// Use the template.ParseFiles() function to read the template file into a
// template set. If there's an error, we log the detailed error message and use
// the http.Error() function to send a generic 500 Internal Server Error
// response to the user.
ts, err := template.ParseFiles(files...)
if err != nil {
app.errorLog.Println(err.Error())
http.Error(w, "Internal server Error ;(", 500)
return
}
// Use the ExecuteTemplate() method to write the content of the "base"
// template as the response body.
err = ts.ExecuteTemplate(w, "base", nil)
if err != nil {
app.errorLog.Println(err.Error())
http.Error(w, "Internal server error", 500)
}
}
func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil || id < 1 {
http.NotFound(w, r)
return
}
fmt.Fprintf(w, "Display a specific snippet with ID %d...", id)
}
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
w.Write([]byte("Create a new snippet..."))
}