From f862eaf6a94a5139fd2fa434083e02afdd73ef60 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Thu, 6 Dec 2018 13:41:22 -0800 Subject: [PATCH] init for restful explorer service --- services/explorer/rest.go | 88 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 services/explorer/rest.go diff --git a/services/explorer/rest.go b/services/explorer/rest.go new file mode 100644 index 000000000..0a29bc334 --- /dev/null +++ b/services/explorer/rest.go @@ -0,0 +1,88 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + + "github.com/gorilla/mux" +) + +// ExplorerService is the struct for explorer service. +type ExplorerService struct { +} + +// Init is to do init for ExplorerService. +func (es *ExplorerService) Init() { + +} + +// ParentHash *common.Hash `json:"parentHash" gencodec:"required"` +// Coinbase *common.Address `json:"miner" gencodec:"required"` +// Root *common.Hash `json:"stateRoot" gencodec:"required"` + +// Person is fake struct for testing. +type Person struct { + ID string `json:"id,omitempty"` + Firstname string `json:"firstname,omitempty"` + Lastname string `json:"lastname,omitempty"` + Address *Address `json:"address,omitempty"` +} + +// Address is fake struct for testing. +type Address struct { + City string `json:"city,omitempty"` + State string `json:"state,omitempty"` +} + +var people []Person + +// GetPersonEndpoint is the specific person end point. +func GetPersonEndpoint(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + for _, item := range people { + if item.ID == params["id"] { + json.NewEncoder(w).Encode(item) + return + } + } + json.NewEncoder(w).Encode(&Person{}) +} + +// GetPeopleEndpoint is the people end point. +func GetPeopleEndpoint(w http.ResponseWriter, r *http.Request) { + json.NewEncoder(w).Encode(people) +} + +// CreatePersonEndpoint is post people/{id} end point. +func CreatePersonEndpoint(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + var person Person + _ = json.NewDecoder(r.Body).Decode(&person) + person.ID = params["id"] + people = append(people, person) + json.NewEncoder(w).Encode(people) +} + +// DeletePersonEndpoint is delete people/{id} end point. +func DeletePersonEndpoint(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + for index, item := range people { + if item.ID == params["id"] { + people = append(people[:index], people[index+1:]...) + break + } + json.NewEncoder(w).Encode(people) + } +} + +func main() { + router := mux.NewRouter() + people = append(people, Person{ID: "1", Firstname: "John", Lastname: "Doe", Address: &Address{City: "City X", State: "State X"}}) + people = append(people, Person{ID: "2", Firstname: "Koko", Lastname: "Doe", Address: &Address{City: "City Z", State: "State Y"}}) + router.HandleFunc("/people", GetPeopleEndpoint).Methods("GET") + router.HandleFunc("/people/{id}", GetPersonEndpoint).Methods("GET") + router.HandleFunc("/people/{id}", CreatePersonEndpoint).Methods("POST") + router.HandleFunc("/people/{id}", DeletePersonEndpoint).Methods("DELETE") + log.Fatal(http.ListenAndServe(":8000", router)) +}