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
|
package main
import (
"fmt"
"log"
"net/http"
)
func handleGet( w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "It's the root ! [%s]{{%s}}", r.URL.Path[1:], r.Method)
}
func handlePost( w http.ResponseWriter, r *http.Request) {
}
func handleDelete( w http.ResponseWriter, r *http.Request) {
}
func handlePut( w http.ResponseWriter, r *http.Request) {
}
func handleMethod( w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
handleGet(w, r)
case http.MethodPost:
handlePost(w, r)
case http.MethodPut:
handlePut(w, r)
case http.MethodDelete:
// Remove the record.
handleDelete(w, r)
default:
w.WriteHeader(http.StatusTeapot)
fmt.Fprintln(w, "Sry, i'm a teapot >///<")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "Here's some tea. 🍵")
}
}
func main() {
fmt.Println("Launch server")
http.HandleFunc("/", handleMethod)
log.Fatal(http.ListenAndServe(":8080", nil))
fmt.Println("Bye bye")
}
|