lets-go:2.5
This commit is contained in:
parent
0520d42ea4
commit
cb0a335636
44
snippetbox/main.go
Normal file
44
snippetbox/main.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// home ...
|
||||||
|
func home(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/" {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write([]byte("Hello from snippetbox.chaosfem.tw"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// snippetView ...
|
||||||
|
func snippetView(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Write([]byte("It's a snippet!"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// snippetCreate ...
|
||||||
|
func snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Allow", "POST")
|
||||||
|
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write([]byte("Creating a snippet!"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// main it's the snippetbox webapp
|
||||||
|
func main() {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", home)
|
||||||
|
mux.HandleFunc("/snippet/view", snippetView)
|
||||||
|
mux.HandleFunc("/snippet/create", snippetCreate)
|
||||||
|
|
||||||
|
log.Print("starting a server on :4000")
|
||||||
|
err := http.ListenAndServe(":4000", mux)
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user