-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
56 lines (53 loc) · 1.4 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
// handlers.go defines functions that receive http requests and write
// http responses.
package main
import (
"fmt"
"net/http"
)
// getHandler handles requests to retrive existing pastes.
func getHandler(response http.ResponseWriter, request *http.Request) {
key := request.URL.Path[len(pasteRoot)-1:]
page, keyErr := getPageForKey(key)
if keyErr != nil {
ERROR.Println(keyErr)
http.NotFound(response, request)
} else {
_, printErr := fmt.Fprint(response, page)
if printErr != nil {
ERROR.Println(printErr)
}
}
}
// putHandler handles requests made to the root path. It the request
// is a GET then we return a page containing a form. If it is a POST
// then we save the new paste.
func putHandler(response http.ResponseWriter, request *http.Request) {
switch request.Method {
case "GET":
newPastePage := getNewPastePage()
_, printErr := fmt.Fprint(response, newPastePage)
if printErr != nil {
ERROR.Println(printErr)
}
case "POST":
parseErr := request.ParseForm()
if parseErr != nil {
internalServerError(response, parseErr)
} else {
text := request.FormValue("paste")
newKey, saveErr := savePaste(text)
if saveErr != nil {
internalServerError(response, saveErr)
} else {
http.Redirect(
response, request,
pasteRoot+newKey, http.StatusFound)
}
}
default:
http.Error(
response, "Error: Method Not Allowed (405)",
http.StatusMethodNotAllowed)
}
}