-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalInfo.go
76 lines (71 loc) · 2.77 KB
/
globalInfo.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"log"
"net/http"
"fmt"
"strconv"
)
// Define a home handler function which writes a byte slice containing
// "Hello from Snippetbox" as the response body.
func home(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
// 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
// the http.Error() function to send a generic 500 Internal Server Error
// response to the user.
ts, err := template.ParseFiles("./ui/html/home.page.tmpl")
if err != nil {
log.Println(err.Error())http.Error(w, "Internal Server Error", 500)
return
}
// We then use the Execute() method on the template set to write the templa
// content as the response body. The last parameter to Execute() represents
// dynamic data that we want to pass in, which for now we'll leave as nil.
err = ts.Execute(w, nil)
if err != nil {
log.Println(err.Error())
http.Error(w, "Internal Server Error", 500)
}
}
// Add a showSnippet handler function.
func showSnippet(w http.ResponseWriter, r *http.Request) {
// Extract the value of the id parameter from the query string and try to
// convert it to an integer using the strconv.Atoi() function. If it can't
// be converted to an integer, or the value is less than 1, we return a 404
// not found response.
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil || id < 1 {
http.NotFound(w, r)
return
}
// Use the fmt.Fprintf() function to interpolate the id value with our respo
// and write it to the http.ResponseWriter.
fmt.Fprintf(w, "Display a specific snippet with ID %d...", id)
}
// Add a createSnippet handler function.
func createSnippet(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.Header().Set("Allow", "POST")
http.Error(w, "Method Not Allowed", 405) // http.Error() takesa given message and status code calls the w.WriteHeader(405) AND w.Write([]byte("Method Not Allowed")) methods behind-the-scenes for us.
return
}
w.Write([]byte("Create a new snippet..."))
}
func main() {
// Register the two new handler functions and corresponding URL patterns wi
// the servemux, in exactly the same way that we did before.
mux := http.NewServeMux()
mux.HandleFunc("/", home)
mux.HandleFunc("/snippet", showSnippet)
mux.HandleFunc("/snippet/create", createSnippet)
// Use the http.ListenAndServe() function to start a new web server. We pas
// two parameters: the TCP network address to listen on (in this case ":4000
// and the servemux we just created. If http.ListenAndServe() returns an er
// we use the log.Fatal() function to log the error message and exit.
log.Println("Starting server on :4000")
err := http.ListenAndServe(":4000", mux)
log.Fatal(err)
}