-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (58 loc) · 2.59 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"strconv"
)
// (1) Define a home handler function which write a byte slice containing
// "Hello from Snippetbox" as the response body.
func home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello from Snippetbox"))
}
// Add a snippetView handler function.
func snippetView(w http.ResponseWriter, r *http.Request) {
// Extract the value of the wildcard from the request using r.PathValue()
// 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 page not found response.
id, err := strconv.Atoi(r.PathValue("id"))
if err != nil || id < 1 {
http.NotFound(w, r)
return
}
msg := fmt.Sprintf("Display a specific snippet with ID %d...", id)
w.Write([]byte(msg))
// phased out after refactoring in line above ^ -- w.Write([]byte("Display a specific snippet..."))
}
// Add a snippetCreate handler function.
func snippetCreate(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Display a form for creating a new snippet..."))
}
func snippetCreatePost(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Save a new snippet..."))
}
func main() {
// (2) Use the http.NewServeMux() function to initialize a new servemux, then
// (3) register the home function as the handler for "/" URL pattern.
// ** by avoiding http.DefaultServeMux as global variable and using our own locally-scoped
// servemux, we are writing more clear, maintainable and secure code as
// third-party packages will be unable to register routes; a best practice
mux := http.NewServeMux()
mux.HandleFunc("GET /{$}", home)
// Register the two new handler functions and corressponding route patterns with
// the servemux, in exactly the same way we did before.
mux.HandleFunc("GET /snippet/view/{id}", snippetView) // Add the {id} wildcard segment
mux.HandleFunc("GET /snippet/create", snippetCreate)
// Create the new route, which is restricted to POST requests only.
mux.HandleFunc("POST /snippet/create", snippetCreatePost)
// Print a log message to say that the server is starting.
log.Print("starting server on :4000")
// Use the http.ListenAndServe() function to start a new web server. We passs in
// two parameters: The TCP network address to listen on (in this case ":4000")
// and the servemux we just created. If http.ListenAndServe() returns an error
// we use the log.Fatal() function to log the error message and exit. Note
// that any error returned by http.ListenAndServe() as always non-nil.
err := http.ListenAndServe(":4000", mux)
log.Fatal(err)
}