45 lines
918 B
Go
45 lines
918 B
Go
|
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)
|
||
|
}
|