-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
38 lines (31 loc) · 820 Bytes
/
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
package main
import (
"fmt"
"log"
"net/http"
)
func home(w http.ResponseWriter, req *http.Request) {
message := "Default route"
w.Write([]byte(message))
}
func snippetCreate(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
w.WriteHeader(405)
w.Write([]byte(fmt.Sprintf("Method %s not accepted", req.Method)))
}
}
func snippetShow(w http.ResponseWriter, req *http.Request) {
message := "Psst, kid, want some snippet?"
w.Write([]byte(message))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", home)
mux.HandleFunc("/snippet/create", snippetCreate)
mux.HandleFunc("/snippet", snippetShow)
serverPort := ":4000"
log.Println("Server started...")
defer log.Println("Server shutting down....")
err := http.ListenAndServe(serverPort, mux)
log.Fatal(err)
}