Created
June 11, 2020 20:53
-
-
Save maurorappa/42ed4feb2e7df7615cf25ebecbfd53aa to your computer and use it in GitHub Desktop.
mock API infrastructure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.5' | |
services: | |
api: | |
build: /Users/maurorappa/mockapi | |
restart: always | |
ports: | |
- 8081:8081 | |
environment: | |
PORT: 8081 | |
BACKEND1: 172.17.0.1:8082 | |
BACKEND2: 172.17.0.1:8083 | |
healthcheck: | |
test: ["CMD", "wget" ,"-q", "-O", "/dev/null", "http://localhost:8081/health"] | |
interval: 30s | |
timeout: 10s | |
retries: 3 | |
start_period: 60s | |
api2: | |
build: /Users/maurorappa/mockapi | |
restart: always | |
ports: | |
- 8082:8082 | |
environment: | |
PORT: 8082 | |
BACKEND1: 172.17.0.1:8084 | |
healthcheck: | |
test: ["CMD", "wget" ,"-q", "-O", "/dev/null", "http://localhost:8082/health"] | |
interval: 30s | |
timeout: 10s | |
retries: 3 | |
start_period: 60s | |
api3: | |
build: /Users/maurorappa/mockapi | |
restart: always | |
ports: | |
- 8083:8083 | |
environment: | |
PORT: 8083 | |
BACKEND1: 172.17.0.1:8084 | |
BACKEND2: 172.17.0.1:8085 | |
healthcheck: | |
test: ["CMD", "wget" ,"-q", "-O", "/dev/null", "http://localhost:8083/health"] | |
interval: 30s | |
timeout: 10s | |
retries: 3 | |
start_period: 60s | |
api4: | |
build: /Users/maurorappa/mockapi | |
restart: always | |
ports: | |
- 8084:8084 | |
environment: | |
PORT: 8084 | |
healthcheck: | |
test: ["CMD", "wget" ,"-q", "-O", "/dev/null", "http://localhost:8084/health"] | |
interval: 30s | |
timeout: 10s | |
retries: 3 | |
start_period: 60s | |
api5: | |
build: /Users/maurorappa/mockapi | |
restart: always | |
ports: | |
- 8085:8085 | |
environment: | |
PORT: 8085 | |
BACKEND1: 172.17.0.1:8083 | |
healthcheck: | |
test: ["CMD", "wget" ,"-q", "-O", "/dev/null", "http://localhost:8085/health"] | |
interval: 30s | |
timeout: 10s | |
retries: 3 | |
start_period: 60s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM golang:buster as builder | |
WORKDIR /go/src/mockApi | |
COPY . /go/src/mockApi | |
RUN go get github.com/gorilla/mux | |
RUN CGO_ENABLED=0 GOOS=linux go build -o mock_api *.go | |
FROM busybox:latest | |
COPY --from=builder /go/src/mockApi/mock_api /bin/mock_api | |
RUN chmod +x /bin/mock_api | |
ENTRYPOINT [ "/bin/mock_api" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/ecdsa" | |
"crypto/elliptic" | |
cr "crypto/rand" | |
"crypto/sha256" | |
"fmt" | |
"github.com/gorilla/mux" | |
"io/ioutil" | |
"log" | |
"math/rand" | |
"net/http" | |
"os" | |
"strconv" | |
"time" | |
) | |
var ( | |
listenPort string | |
serverA string | |
serverB string | |
) | |
func init() { | |
rand.Seed(time.Now().UTC().UnixNano()) | |
listenPort = os.Getenv("PORT") | |
serverA = os.Getenv("BACKEND1") | |
serverB = os.Getenv("BACKEND2") | |
} | |
func home(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Welcome to the Test API server") | |
} | |
func health(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(http.StatusOK) | |
} | |
func number(w http.ResponseWriter, r *http.Request) { | |
rnd := rand.Intn(1000) | |
log.Println("generated ", rnd) | |
fmt.Fprintf(w, "%d", rnd) | |
} | |
func callremote(server string) (number string) { | |
res, err := http.Get("http://" + server + "/number") | |
if err != nil { | |
log.Println(err) | |
} | |
remote, err := ioutil.ReadAll(res.Body) | |
res.Body.Close() | |
return string(remote) | |
} | |
func callbckend1(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "%s", callremote(serverA)) | |
} | |
func callbckend2(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "%s", callremote(serverB)) | |
} | |
func sumbckends(w http.ResponseWriter, r *http.Request) { | |
a, _ := strconv.Atoi(callremote(serverA)) | |
b, _ := strconv.Atoi(callremote(serverB)) | |
c := a + b | |
log.Println("sum: ", c) | |
fmt.Fprintf(w, "%s", strconv.Itoa(c)) | |
} | |
func crypt(w http.ResponseWriter, r *http.Request) { | |
privateKey, _ := ecdsa.GenerateKey(elliptic.P521(), cr.Reader) | |
msg := "hello, world" | |
hash := sha256.Sum256([]byte(msg)) | |
rnd, bigint, _ := ecdsa.Sign(cr.Reader, privateKey, hash[:]) | |
signature := fmt.Sprintf("0x%x, 0x%x", rnd, bigint) | |
fmt.Fprintln(w, signature) | |
valid := ecdsa.Verify(&privateKey.PublicKey, hash[:], rnd, bigint) | |
fmt.Fprintln(w, "signature verified:", valid) | |
} | |
func main() { | |
log.Println("listening to " + listenPort + "...") | |
router := mux.NewRouter().StrictSlash(true) | |
router.HandleFunc("/", home) | |
router.HandleFunc("/health", health) | |
router.HandleFunc("/number", number) | |
router.HandleFunc("/callbackend1", callbckend1) | |
router.HandleFunc("/callbackend2", callbckend2) | |
router.HandleFunc("/sumbackends", sumbckends) | |
router.HandleFunc("/crypt", crypt) | |
log.Fatal(http.ListenAndServe(":"+listenPort, router)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment