Skip to content

Commit

Permalink
introduce loop calc demo service (#218)
Browse files Browse the repository at this point in the history
* introduce loop calc demo service
* changelog
* fix changelog
* update image tag
* remove vscode config
  • Loading branch information
mitchdraft authored and soloio-bulldozer[bot] committed Jun 10, 2019
1 parent 354b7c1 commit f0052c7
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog/v0.5.16/loop-demo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
changelog:
- type: NON_USER_FACING
description: Add a version of the calculator (service2) that produces 500s when the sum is in the range 500-600.
7 changes: 7 additions & 0 deletions contrib/example/service2-500/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine:3.5

COPY service2ise /service2ise

ENTRYPOINT ["/service2ise"]

EXPOSE 8080
12 changes: 12 additions & 0 deletions contrib/example/service2-500/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DOCKER_REPO ?= soloio
VERSION ?= dev

.PHONY: build
build:
GOOS=linux CGO_ENABLED=0 go build -o service2ise -gcflags "-N -l" main.go
docker build -t $(DOCKER_REPO)/example-service2ise:$(VERSION) .

.PHONY: push
push: build
docker push $(DOCKER_REPO)/example-service2ise:$(VERSION)

56 changes: 56 additions & 0 deletions contrib/example/service2-500/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/solo-io/go-utils/contextutils"
)

type Calculator struct {
Op1, Op2 int
IsAdd bool
}

func main() {
http.HandleFunc("/calculate", calchandler)

contextutils.LoggerFrom(context.TODO()).Fatal(http.ListenAndServe(":8080", nil))
}

func calchandler(w http.ResponseWriter, r *http.Request) {
var req Calculator
dec := json.NewDecoder(r.Body)
err := dec.Decode(&req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}

isadd := req.IsAdd
op1 := req.Op1
op2 := req.Op2
sum, err := computeSum(isadd, op1, op2)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%v", err)
}

fmt.Fprintf(w, "%d", sum)
}

func computeSum(isadd bool, op1, op2 int) (int, error) {
var val int
if isadd {
val = op1 + op2
} else {
val = op1 - op2
}
// Happy 5**th birthday Kubernetes! :D
if val >= 500 && val < 600 {
return 0, fmt.Errorf("result is a 500")
}

return val, nil
}
33 changes: 33 additions & 0 deletions contrib/example/service2-500/service2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: example-service2
spec:
replicas: 1
selector:
matchLabels:
app: example-service2
template:
metadata:
labels:
app: example-service2
spec:
containers:
- name: example-service2
image: soloio/example-service2ise:0.1.0
ports:
- containerPort: 8080
protocol: TCP
---
kind: Service
apiVersion: v1
metadata:
name: example-service2
spec:
selector:
app: example-service2
ports:
- name: http
protocol: TCP
port: 80
targetPort: 8080

0 comments on commit f0052c7

Please sign in to comment.