-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
39 lines (33 loc) · 878 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
39
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
log.Println("restSanity Started")
r := mux.NewRouter()
r.HandleFunc("/", Test_Get).Methods("GET")
r.HandleFunc("/", Test_Post).Methods("POST")
log.Fatal(http.ListenAndServe(":5001", r))
log.Println("restSanity Ended")
}
func Test_Get(w http.ResponseWriter, r *http.Request) {
log.Println("Test_Get")
vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Get: %v\n", vars["test"])
}
func Test_Post(w http.ResponseWriter, r *http.Request) {
log.Println("Test_Post")
var json_data map[string]string
if err := json.NewDecoder(r.Body).Decode(&json_data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
fmt.Printf("Posted: %v\n", json_data)
fmt.Fprintf(w, "Posted: %v\n", json_data)
}