explorer backend service with fake data

pull/153/head
Minh Doan 6 years ago committed by Minh Doan
parent 59ffd3ccac
commit 98e33584b9
  1. 20
      services/explorer/fake_data.go
  2. 2
      services/explorer/fake_data.json
  3. 95
      services/explorer/rest.go
  4. 100
      services/explorer/service.go
  5. 16
      services/explorer/structs.go
  6. 14
      services/explorer/test/main.go
  7. 9
      services/explorer/test2/main.go

@ -7,10 +7,6 @@ import (
"os"
)
var (
data = ReadFakeData()
)
// ReadFakeData ...
func ReadFakeData() Data {
jsonFile, err := os.Open("./fake_data.json")
@ -20,18 +16,28 @@ func ReadFakeData() Data {
os.Exit(1)
}
fmt.Println("Successfully Opened users.json")
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// we initialize our Users array
var data Data
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &data)
err = json.Unmarshal(byteValue, &data)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("block:", data.Block)
fmt.Println("blocks:", data.Blocks)
return data
}

@ -37,7 +37,7 @@
"timestamp": "Dec 10, 2018 1:43:43 AM",
"from": "0x3c65970996d7de5a65d66915a1772481a4a00a61",
"to": "0xe6b3a12cae421d928fa1ca4fb09f2be91afdf650",
"value": "20"
"value": 20
}
]
},

@ -1,95 +0,0 @@
package explorer
import (
"encoding/json"
"log"
"net"
"net/http"
"github.com/gorilla/mux"
)
// Constants for explorer service.
const (
ExplorerServicePort = "5000"
)
// Service is the struct for explorer service.
type Service struct {
people []Person
router *mux.Router
}
// Init is to do init for ExplorerService.
func (s *Service) Init() {
// s.people = append(s.people, Person{ID: "1", Firstname: "John", Lastname: "Doe", Address: &Address{City: "City X", State: "State X"}})
// s.people = append(s.people, Person{ID: "2", Firstname: "Koko", Lastname: "Doe", Address: &Address{City: "City Z", State: "State Y"}})
}
// Run is to run serving explorer.
func (s *Service) Run() {
// Init address.
addr := net.JoinHostPort("", ExplorerServicePort)
// Set up router
s.router = mux.NewRouter()
s.router.HandleFunc("/people", s.GetPeopleEndpoint).Methods("GET")
s.router.HandleFunc("/people/{id}", s.GetPersonEndpoint).Methods("GET")
s.router.HandleFunc("/people/{id}", s.CreatePersonEndpoint).Methods("POST")
s.router.HandleFunc("/people/{id}", s.DeletePersonEndpoint).Methods("DELETE")
// Do serving now.
go log.Fatal(http.ListenAndServe(addr, s.router))
}
// 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"`
// }
// GetPersonEndpoint is the specific person end point.
func (s *Service) GetPersonEndpoint(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
for _, item := range s.people {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
json.NewEncoder(w).Encode(&Person{})
}
// GetPeopleEndpoint is the people end point.
func (s *Service) GetPeopleEndpoint(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(s.people)
}
// CreatePersonEndpoint is post people/{id} end point.
func (s *Service) CreatePersonEndpoint(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
var person Person
_ = json.NewDecoder(r.Body).Decode(&person)
person.ID = params["id"]
s.people = append(s.people, person)
json.NewEncoder(w).Encode(s.people)
}
// DeletePersonEndpoint is delete people/{id} end point.
func (s *Service) DeletePersonEndpoint(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
for index, item := range s.people {
if item.ID == params["id"] {
s.people = append(s.people[:index], s.people[index+1:]...)
break
}
json.NewEncoder(w).Encode(s.people)
}
}

@ -0,0 +1,100 @@
package explorer
import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"github.com/gorilla/mux"
)
// Constants for explorer service.
const (
ExplorerServicePort = "5000"
)
// Service is the struct for explorer service.
type Service struct {
data Data
router *mux.Router
}
// Init is to do init for ExplorerService.
func (s *Service) Init() {
s.data = ReadFakeData()
}
// Run is to run serving explorer.
func (s *Service) Run() {
// Init address.
addr := net.JoinHostPort("", ExplorerServicePort)
// Set up router
s.router = mux.NewRouter()
s.router.HandleFunc("/blocks", s.GetExplorerBlocks).Methods("GET")
s.router.HandleFunc("/block", s.GetExplorerBlock).Methods("GET")
s.router.HandleFunc("/address", s.GetExplorerAddress).Methods("GET")
// s.router.HandleFunc("/people", s.GetPeopleEndpoint).Methods("GET")
// s.router.HandleFunc("/people/{id}", s.GetPersonEndpoint).Methods("GET")
// s.router.HandleFunc("/people/{id}", s.CreatePersonEndpoint).Methods("POST")
// s.router.HandleFunc("/people/{id}", s.DeletePersonEndpoint).Methods("DELETE")
// Do serving now.
fmt.Println("Listening to:", ExplorerServicePort)
go log.Fatal(http.ListenAndServe(addr, s.router))
}
// GetExplorerBlocks ...
func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(s.data.Blocks)
}
// GetExplorerBlock ...
func (s *Service) GetExplorerBlock(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(s.data.Block)
}
// GetExplorerAddress ...
func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(s.data.Address)
}
// // GetPersonEndpoint is the specific person end point.
// func (s *Service) GetPersonEndpoint(w http.ResponseWriter, r *http.Request) {
// params := mux.Vars(r)
// for _, item := range s.people {
// if item.ID == params["id"] {
// json.NewEncoder(w).Encode(item)
// return
// }
// }
// json.NewEncoder(w).Encode(&Person{})
// }
// // GetPeopleEndpoint is the people end point.
// func (s *Service) GetPeopleEndpoint(w http.ResponseWriter, r *http.Request) {
// json.NewEncoder(w).Encode(s.people)
// }
// // CreatePersonEndpoint is post people/{id} end point.
// func (s *Service) CreatePersonEndpoint(w http.ResponseWriter, r *http.Request) {
// params := mux.Vars(r)
// var person Person
// _ = json.NewDecoder(r.Body).Decode(&person)
// person.ID = params["id"]
// s.people = append(s.people, person)
// json.NewEncoder(w).Encode(s.people)
// }
// // DeletePersonEndpoint is delete people/{id} end point.
// func (s *Service) DeletePersonEndpoint(w http.ResponseWriter, r *http.Request) {
// params := mux.Vars(r)
// for index, item := range s.people {
// if item.ID == params["id"] {
// s.people = append(s.people[:index], s.people[index+1:]...)
// break
// }
// json.NewEncoder(w).Encode(s.people)
// }
// }

@ -7,6 +7,7 @@ package explorer
// Data ...
type Data struct {
Blocks []Block `json:"blocks"`
Block Block2 `json:"block"`
Address Address `json:"address"`
}
@ -29,9 +30,18 @@ type Transaction struct {
// Block ...
type Block struct {
Height int `json:"height"`
ID string `json:"id"`
Height string `json:"height"`
Timestamp string `json:"timestamp"`
TXCount string `json:"txCount"`
Size string `json:"size"`
}
// Block2 ...
type Block2 struct {
Height string `json:"height"`
Hash string `json:"hash"`
TXCount int `json:"txCount"`
TXCount string `json:"txCount"`
Timestamp string `json:"timestamp"`
MerkleRoot string `json:"merkleRoot"`
PrevBlock RefBlock `json:"prevBlock"`
@ -44,5 +54,5 @@ type Block struct {
// RefBlock ...
type RefBlock struct {
ID string `json:"id"`
Height int `json:"height"`
Height string `json:"height"`
}

@ -17,19 +17,25 @@ func main() {
os.Exit(1)
}
fmt.Println("Successfully Opened users.json")
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// we initialize our Users array
var data explorer.Data
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &data)
err = json.Unmarshal(byteValue, &data)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(data)
}

@ -0,0 +1,9 @@
package main
import "github.com/harmony-one/harmony/services/explorer"
func main() {
service := &explorer.Service{}
service.Init()
service.Run()
}
Loading…
Cancel
Save