-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (40 loc) · 1.19 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
package main
import (
"fmt"
"log"
"net/http"
"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) {
w.Write([]byte("Hello from Snippetbox"))
}
// Add a snippetView hanlder function.
func snippetView(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
idInt, err := strconv.Atoi(id)
if err != nil || idInt < 1 {
http.NotFound(w, r)
return
}
fmt.Fprintf(w, "Display a specific snippet with ID %d...", idInt)
}
func snippetCreate(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Create a new snippet..."))
}
func snippetCreatePost(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
w.Write([]byte("Save a new snippet..."))
}
func main() {
mux := http.NewServeMux()
// Restrict this route to exact matches on / only.
mux.HandleFunc("GET /{$}", home)
mux.HandleFunc("GET /snippet/view/{id}", snippetView)
mux.HandleFunc("GET /snippet/create", snippetCreate)
mux.HandleFunc("POST /snippet/create", snippetCreatePost)
log.Print("starting server on :4000")
err := http.ListenAndServe(":4000", mux)
log.Fatal(err)
}