54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
|
package main
|
||
|
|
||
|
import "fmt"
|
||
|
import "log"
|
||
|
import "net/http"
|
||
|
import "github.com/davecgh/go-spew/spew"
|
||
|
|
||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||
|
// spew.Dump(r)
|
||
|
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
|
||
|
log.Printf("handler: unknown path")
|
||
|
}
|
||
|
|
||
|
func handler2(w http.ResponseWriter, r *http.Request) {
|
||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||
|
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||
|
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
||
|
|
||
|
spew.Dump(r)
|
||
|
fmt.Fprintf(w, "handler2 %s!", r.URL.Path[1:])
|
||
|
|
||
|
log.Printf("handler: method switch statement")
|
||
|
switch r.Method {
|
||
|
case http.MethodGet:
|
||
|
// Serve the resource.
|
||
|
log.Printf("handler2 GET")
|
||
|
case http.MethodPost:
|
||
|
// Create a new record.
|
||
|
log.Printf("handler2 POST")
|
||
|
case http.MethodPut:
|
||
|
// Update an existing record.
|
||
|
log.Printf("handler2 PUT")
|
||
|
case http.MethodDelete:
|
||
|
// Remove the record.
|
||
|
log.Printf("handler2 DELETE")
|
||
|
case http.MethodOptions:
|
||
|
// Remove the record.
|
||
|
log.Printf("handler2 OPTIONS")
|
||
|
default:
|
||
|
// Give an error message.
|
||
|
log.Printf("handler2 DEFAULT Method=", r.Method)
|
||
|
spew.Dump(r.Method)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
log.Println("listen on :9000")
|
||
|
|
||
|
http.HandleFunc("/", handler)
|
||
|
http.HandleFunc("/email", handler2)
|
||
|
|
||
|
http.ListenAndServe(":9000", nil)
|
||
|
}
|