aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-11-15 06:33:10 +0000
committerterminaldweller <thabogre@gmail.com>2022-11-15 06:33:10 +0000
commit14c8da596041a1f4c73b7065acfd4f185f35ca68 (patch)
treee2956e27cb910d771261e7774496f0f54cd763ce
parentadded coincap endpoint (diff)
downloadhived-14c8da596041a1f4c73b7065acfd4f185f35ca68.tar.gz
hived-14c8da596041a1f4c73b7065acfd4f185f35ca68.zip
update
-rw-r--r--arbiter/arbiter.go4
-rwxr-xr-xarbiter/tests.sh4
-rw-r--r--hived/hived.go40
-rw-r--r--hived/hived_responses.go31
-rw-r--r--hived/hived_test.go238
5 files changed, 294 insertions, 23 deletions
diff --git a/arbiter/arbiter.go b/arbiter/arbiter.go
index 7390cfa..8fce2be 100644
--- a/arbiter/arbiter.go
+++ b/arbiter/arbiter.go
@@ -363,8 +363,8 @@ func main() {
setupLogging()
var handlerFuncs = []HttpHandler{
- {name: "/arb/gecko", function: arbHandler},
- {name: "/arb/coincap", function: coincapHandler},
+ {name: "/crypto/v1/arb/gecko", function: arbHandler},
+ {name: "/crypto/v1/arb/coincap", function: coincapHandler},
}
startServer(gracefulWait, handlerFuncs, SERVER_DEPLOYMENT_TYPE, *flagPort)
diff --git a/arbiter/tests.sh b/arbiter/tests.sh
index ece5801..be923f3 100755
--- a/arbiter/tests.sh
+++ b/arbiter/tests.sh
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
-curl -k -X GET "https://localhost:8009/arb/gecko?name=ethereum&unit=usd"
-curl -k -X GET "https://localhost:8009/arb/coincap?name=ethereum"
+curl -k -X GET "https://localhost:8009/crypto/v1/arb/gecko?name=ethereum&unit=usd"
+curl -k -X GET "https://localhost:8009/crypto/v1/arb/coincap?name=ethereum"
diff --git a/hived/hived.go b/hived/hived.go
index a205352..51ea933 100644
--- a/hived/hived.go
+++ b/hived/hived.go
@@ -145,6 +145,7 @@ func getPriceFromCryptoCompare(
priceChan <- priceChanStruct{name: name, price: 0.}
errChan <- errorChanStruct{hasError: true, err: err}
log.Error().Err(err)
+ return
}
defer resp.Body.Close()
@@ -169,7 +170,7 @@ func getPriceFromCryptoCompare(
errChan <- errorChanStruct{hasError: false, err: nil}
}
-func priceHandler(w http.ResponseWriter, r *http.Request) {
+func PriceHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
if r.Method != "GET" {
http.Error(w, "Method is not supported.", http.StatusNotFound)
@@ -232,7 +233,7 @@ func priceHandler(w http.ResponseWriter, r *http.Request) {
"isSuccessful": true})
}
-func pairHandler(w http.ResponseWriter, r *http.Request) {
+func PairHandler(w http.ResponseWriter, r *http.Request) {
var err error
w.Header().Add("Content-Type", "application/json")
if r.Method != "GET" {
@@ -415,7 +416,7 @@ type addAlertJSONType struct {
Expr string `json:"expr"`
}
-func handleAlertPost(w http.ResponseWriter, r *http.Request) {
+func (this AlertHandler) HandleAlertPost(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
@@ -435,14 +436,14 @@ func handleAlertPost(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
key := "alert:" + bodyJSON.Name
- rdb.Set(ctx, bodyJSON.Name, bodyJSON.Expr, 0)
- rdb.SAdd(ctx, "alertkeys", key)
+ this.rdb.Set(ctx, bodyJSON.Name, bodyJSON.Expr, 0)
+ this.rdb.SAdd(ctx, "alertkeys", key)
json.NewEncoder(w).Encode(map[string]interface{}{
"isSuccessful": true,
"error": ""})
}
-func handleAlertDelete(w http.ResponseWriter, r *http.Request) {
+func (this AlertHandler) HandleAlertDelete(w http.ResponseWriter, r *http.Request) {
var Id string
w.Header().Add("Content-Type", "application/json")
params := r.URL.Query()
@@ -465,9 +466,9 @@ func handleAlertDelete(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
- rdb.Del(ctx, Id)
+ this.rdb.Del(ctx, Id)
setKey := "alert:" + Id
- rdb.SRem(ctx, "alertkeys", setKey)
+ this.rdb.SRem(ctx, "alertkeys", setKey)
log.Printf(setKey)
json.NewEncoder(w).Encode(struct {
@@ -476,7 +477,7 @@ func handleAlertDelete(w http.ResponseWriter, r *http.Request) {
}{IsSuccessful: true, Err: ""})
}
-func handleAlertGet(w http.ResponseWriter, r *http.Request) {
+func (this AlertHandler) HandleAlertGet(w http.ResponseWriter, r *http.Request) {
var Id string
w.Header().Add("Content-Type", "application/json")
params := r.URL.Query()
@@ -499,7 +500,7 @@ func handleAlertGet(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
- redisResult := rdb.Get(ctx, Id)
+ redisResult := this.rdb.Get(ctx, Id)
redisResultString, err := redisResult.Result()
if err != nil {
log.Err(err)
@@ -524,12 +525,13 @@ func handleAlertGet(w http.ResponseWriter, r *http.Request) {
func alertHandler(w http.ResponseWriter, r *http.Request) {
addSecureHeaders(&w)
+ alertHandler := AlertHandler{rdb: rdb}
if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
- handleAlertPost(w, r)
+ alertHandler.HandleAlertPost(w, r)
} else if r.Method == "DELETE" {
- handleAlertDelete(w, r)
+ alertHandler.HandleAlertDelete(w, r)
} else if r.Method == "GET" {
- handleAlertGet(w, r)
+ alertHandler.HandleAlertGet(w, r)
} else {
http.Error(w, "Method is not supported.", http.StatusNotFound)
}
@@ -667,12 +669,12 @@ func startServer(gracefulWait time.Duration) {
Handler: r,
TLSConfig: cfg,
}
- r.HandleFunc("/crypto/health", healthHandler)
- r.HandleFunc("/crypto/price", priceHandler)
- r.HandleFunc("/crypto/pair", pairHandler)
- r.HandleFunc("/crypto/alert", alertHandler)
- r.HandleFunc("/crypto/ex", exHandler)
- r.HandleFunc("/crypto/robots.txt", robotsHandler)
+ r.HandleFunc("/crypto/v1/health", healthHandler)
+ r.HandleFunc("/crypto/v1/price", PriceHandler)
+ r.HandleFunc("/crypto/v1/pair", PairHandler)
+ r.HandleFunc("/crypto/v1/alert", alertHandler)
+ r.HandleFunc("/crypto/v1/ex", exHandler)
+ r.HandleFunc("/crypto/v1/robots.txt", robotsHandler)
go func() {
var certPath, keyPath string
diff --git a/hived/hived_responses.go b/hived/hived_responses.go
new file mode 100644
index 0000000..72a340c
--- /dev/null
+++ b/hived/hived_responses.go
@@ -0,0 +1,31 @@
+package main
+
+import "github.com/go-redis/redis/v8"
+
+type AlertHandler struct {
+ rdb *redis.Client
+}
+
+type HivedPriceResponse struct {
+ Name string `json:"name"`
+ Price float64 `json:"price"`
+ Unit string `json:"unit"`
+ Err string `json:"err"`
+ IsSuccessful bool `json:"isSuccessful"`
+}
+
+type HivedPairResponse struct {
+ Ratio float64 `json:"ratio"`
+}
+
+type HivedAlertGetResponse struct {
+ IsSuccessful bool `json:"isSuccessful"`
+ Err string `json:"err"`
+ Key string `json:"key"`
+ Expr string `json:"expr"`
+}
+
+type HivedAlertGenericResponse struct {
+ Err string `json:"err"`
+ IsSuccessful bool `json:"isSuccessful"`
+}
diff --git a/hived/hived_test.go b/hived/hived_test.go
new file mode 100644
index 0000000..96bdd1e
--- /dev/null
+++ b/hived/hived_test.go
@@ -0,0 +1,238 @@
+package main
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/go-redis/redis/v8"
+)
+
+const (
+ endpoint = "https://api.terminaldweller.com/crypto/v1"
+)
+
+func errorHandler(recorder *httptest.ResponseRecorder, t *testing.T, err error) {
+ if err != nil {
+ t.Errorf(err.Error())
+ }
+
+ if recorder.Code != 200 {
+ t.Errorf("returned code: %d", recorder.Code)
+ }
+}
+
+func TestPriceHandler(t *testing.T) {
+ req, err := http.NewRequest("GET", endpoint+"/price?name=BTC&unit=USD", nil)
+ recorder := httptest.NewRecorder()
+ PriceHandler(recorder, req)
+ errorHandler(recorder, t, err)
+
+ var hivedPriceResponse HivedPriceResponse
+ bodyBytes, err := ioutil.ReadAll(recorder.Body)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ err = json.Unmarshal(bodyBytes, &hivedPriceResponse)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+
+ if !hivedPriceResponse.IsSuccessful {
+ fmt.Println(err.Error())
+ errorHandler(recorder, t, err)
+ }
+}
+
+func TestPairHandler(t *testing.T) {
+ req, err := http.NewRequest("GET", endpoint+"/pair?one=ETH&two=CAKE&multiplier=4.0", nil)
+ recorder := httptest.NewRecorder()
+ PairHandler(recorder, req)
+ errorHandler(recorder, t, err)
+
+ var hivedPairResponse HivedPairResponse
+ bodyBytes, err := ioutil.ReadAll(recorder.Body)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ err = json.Unmarshal(bodyBytes, &hivedPairResponse)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+}
+
+func TestAlertHandlerPhase1(t *testing.T) {
+ rdb = redis.NewClient(&redis.Options{
+ Addr: *redisAddress,
+ Password: *redisPassword,
+ DB: int(*redisDB),
+ })
+ defer rdb.Close()
+
+ postValues := map[string]string{"name": "alertTest", "expr": "ETH < 10000"}
+ postData, err := json.Marshal(postValues)
+ if err != nil {
+ fmt.Println(err.Error())
+ }
+ req, err := http.NewRequest("POST", endpoint+"/alert", bytes.NewBuffer(postData))
+ req.Header.Set("Content-Type", "application/json")
+ recorder := httptest.NewRecorder()
+ alertHandler := AlertHandler{rdb: rdb}
+ alertHandler.HandleAlertPost(recorder, req)
+ errorHandler(recorder, t, err)
+
+ var hivedAlertGenericResponse HivedAlertGenericResponse
+ bodyBytes, err := ioutil.ReadAll(recorder.Body)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ err = json.Unmarshal(bodyBytes, &hivedAlertGenericResponse)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ if !hivedAlertGenericResponse.IsSuccessful {
+ fmt.Println(err.Error())
+ errorHandler(recorder, t, err)
+ }
+}
+
+func TestAlertHandlerPhase2(t *testing.T) {
+ rdb = redis.NewClient(&redis.Options{
+ Addr: *redisAddress,
+ Password: *redisPassword,
+ DB: int(*redisDB),
+ })
+ defer rdb.Close()
+
+ req, err := http.NewRequest("GET", endpoint+"/alert?key=alertTest", nil)
+ recorder := httptest.NewRecorder()
+ alertHandler := AlertHandler{rdb: rdb}
+ alertHandler.HandleAlertGet(recorder, req)
+ errorHandler(recorder, t, err)
+
+ var hivedAlertGetResponse HivedAlertGetResponse
+ bodyBytes, err := ioutil.ReadAll(recorder.Body)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ err = json.Unmarshal(bodyBytes, &hivedAlertGetResponse)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ if !hivedAlertGetResponse.IsSuccessful {
+ fmt.Println(err.Error())
+ errorHandler(recorder, t, err)
+ }
+}
+
+func TestAlertHandlerPhase3(t *testing.T) {
+ rdb = redis.NewClient(&redis.Options{
+ Addr: *redisAddress,
+ Password: *redisPassword,
+ DB: int(*redisDB),
+ })
+ defer rdb.Close()
+
+ postValues := map[string]string{"name": "alertTest", "expr": "ETH > 10000"}
+ postData, err := json.Marshal(postValues)
+ if err != nil {
+ fmt.Println(err.Error())
+ }
+ req, err := http.NewRequest("PUT", endpoint+"/alert", bytes.NewBuffer(postData))
+ req.Header.Set("Content-Type", "application/json")
+ recorder := httptest.NewRecorder()
+ alertHandler := AlertHandler{rdb: rdb}
+ alertHandler.HandleAlertGet(recorder, req)
+ errorHandler(recorder, t, err)
+}
+
+func TestAlertHandlerPhase4(t *testing.T) {
+ rdb = redis.NewClient(&redis.Options{
+ Addr: *redisAddress,
+ Password: *redisPassword,
+ DB: int(*redisDB),
+ })
+ defer rdb.Close()
+
+ req, err := http.NewRequest("GET", endpoint+"/alert?key=alertTest", nil)
+ recorder := httptest.NewRecorder()
+ alertHandler := AlertHandler{rdb: rdb}
+ alertHandler.HandleAlertGet(recorder, req)
+ errorHandler(recorder, t, err)
+
+ var hivedAlertGetResponse HivedAlertGetResponse
+ bodyBytes, err := ioutil.ReadAll(recorder.Body)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ err = json.Unmarshal(bodyBytes, &hivedAlertGetResponse)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ if !hivedAlertGetResponse.IsSuccessful {
+ fmt.Println(err.Error())
+ errorHandler(recorder, t, err)
+ }
+}
+
+func TestAlertHandlerPhase5(t *testing.T) {
+ rdb = redis.NewClient(&redis.Options{
+ Addr: *redisAddress,
+ Password: *redisPassword,
+ DB: int(*redisDB),
+ })
+ defer rdb.Close()
+
+ req, err := http.NewRequest("DELETE", endpoint+"/alert?key=alertTest", nil)
+ recorder := httptest.NewRecorder()
+ alertHandler := AlertHandler{rdb: rdb}
+ alertHandler.HandleAlertGet(recorder, req)
+ errorHandler(recorder, t, err)
+
+ var hivedAlertGenericResponse HivedAlertGenericResponse
+ bodyBytes, err := ioutil.ReadAll(recorder.Body)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ err = json.Unmarshal(bodyBytes, &hivedAlertGenericResponse)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ if !hivedAlertGenericResponse.IsSuccessful {
+ fmt.Println(err.Error())
+ errorHandler(recorder, t, err)
+ }
+}
+
+func TestAlertHandlerPhase6(t *testing.T) {
+ rdb = redis.NewClient(&redis.Options{
+ Addr: *redisAddress,
+ Password: *redisPassword,
+ DB: int(*redisDB),
+ })
+ defer rdb.Close()
+
+ req, err := http.NewRequest("GET", endpoint+"/alert?key=alertTest", nil)
+ recorder := httptest.NewRecorder()
+ alertHandler := AlertHandler{rdb: rdb}
+ alertHandler.HandleAlertGet(recorder, req)
+ errorHandler(recorder, t, err)
+
+ var hivedAlertGetResponse HivedAlertGetResponse
+ bodyBytes, err := ioutil.ReadAll(recorder.Body)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ err = json.Unmarshal(bodyBytes, &hivedAlertGetResponse)
+ if err != nil {
+ errorHandler(recorder, t, err)
+ }
+ if !hivedAlertGetResponse.IsSuccessful {
+ fmt.Println(err.Error())
+ errorHandler(recorder, t, err)
+ }
+}