-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce loop calc demo service (#218)
* introduce loop calc demo service * changelog * fix changelog * update image tag * remove vscode config
- Loading branch information
1 parent
354b7c1
commit f0052c7
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
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
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. |
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
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 |
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
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) | ||
|
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
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 | ||
} |
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
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 |