Skip to content

Commit

Permalink
Lab 12, bonus task
Browse files Browse the repository at this point in the history
  • Loading branch information
kolayne committed Apr 22, 2024
1 parent 93876dc commit 029be44
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app_go/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
go.work

# End of https://www.toptal.com/developers/gitignore/api/go


/persistent
4 changes: 4 additions & 0 deletions app_go/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM golang:1.22.0-alpine3.19 as builder

RUN ["mkdir", "/empty-dir"]

WORKDIR /usr/src/app/

COPY go.mod *.go /usr/src/app/
Expand All @@ -16,6 +18,8 @@ COPY --from=0 /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.

EXPOSE 5000

VOLUME /persistent
COPY --from=builder /usr/src/app/catfact_webapp /
USER 2004:2004
COPY --from=builder --chown=2004:2004 /empty-dir /persistent
ENTRYPOINT ["/catfact_webapp"]
46 changes: 43 additions & 3 deletions app_go/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package main

import (
"sync/atomic"
"encoding/binary"
"errors"
"path/filepath"
"fmt"
"log"
"net/http"
"os"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func index(w http.ResponseWriter, r *http.Request) {
const visitsFile = "persistent/visits.bin"
var visits atomic.Uint64

func indexHandler(w http.ResponseWriter, r *http.Request) {
fact, err := catFact()
if err == nil {
w.WriteHeader(http.StatusOK)
Expand All @@ -22,6 +30,10 @@ func index(w http.ResponseWriter, r *http.Request) {
}
}

func visitsHandler(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, "%d", visits.Load())
}


var (
reqCnt = promauto.NewCounter(prometheus.CounterOpts{
Expand All @@ -48,14 +60,42 @@ func noteTimeMiddleware(next http.Handler) http.Handler {
})
}

func countVisitsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
visits.Add(1) // Atomic increment, no race condition here, so counter
// is always correct
buf := make([]byte, binary.MaxVarintLen64)
binary.LittleEndian.PutUint64(buf, visits.Load())
// Race condition in the order in which threads will write the file out,
// so the file may not be correct, but as the same number of bytes is
// always written out, the counter in the file remains a valid number.
os.WriteFile(visitsFile, buf, 0644)
next.ServeHTTP(w, r)
})
}


func init() {
_ = os.MkdirAll(filepath.Dir(visitsFile), 0755)
buf, err := os.ReadFile(visitsFile)
if err == nil {
visits.Store(binary.LittleEndian.Uint64(buf))
} else if errors.Is(err, os.ErrNotExist) {
visits.Store(0)
} else {
panic(err)
}
}


func main() {
businessLogic := http.NewServeMux()
businessLogic.Handle("/", asHandler(index))
businessLogic.Handle("/", asHandler(indexHandler))
businessLogic.Handle("/visits", asHandler(visitsHandler))
// Note: keeping /metrics under middleware too for consistency with app_py
businessLogic.Handle("/metrics", promhttp.Handler())

wrapped := noteTimeMiddleware(businessLogic)
wrapped := noteTimeMiddleware(countVisitsMiddleware(businessLogic))

hostPort := "0.0.0.0:5000"
_, _ = fmt.Println("Listening on http://" + hostPort)
Expand Down
Binary file added k8s/app-go/charts/label-lib-0.1.0.tgz
Binary file not shown.
2 changes: 2 additions & 0 deletions k8s/app-go/files/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
seal: "2001"
monk_seal: "century"
6 changes: 6 additions & 0 deletions k8s/app-go/templates/config-map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: "v1"
kind: ConfigMap
metadata:
name: {{.Release.Name}}-config-map
data:
{{ .Files.Get "files/config.yaml" | indent 2 }}
11 changes: 10 additions & 1 deletion k8s/app-go/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ spec:
{{- toYaml . | nindent 12 }}
{{- end }}
env:
{{ include "app-go.environ" . | nindent 12 }}
- name: seal
valueFrom:
configMapKeyRef:
name: {{.Release.Name}}-config-map
key: seal
- name: monk_seal
valueFrom:
configMapKeyRef:
name: {{.Release.Name}}-config-map
key: monk_seal
{{- with .Values.volumes }}
volumes:
{{- toYaml . | nindent 8 }}
Expand Down
2 changes: 1 addition & 1 deletion k8s/app-go/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ image:
repository: kolay0ne/app_go
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: "lab8"
tag: "lab12"

serviceAccount:
# Specifies whether a service account should be created
Expand Down
5 changes: 4 additions & 1 deletion monitoring/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ networks:
volumes:
grafana-storage:
py-persistent:
go-persistent:

services:
app_py:
Expand All @@ -22,7 +23,7 @@ services:
- py-persistent:/app/persistent

app_go:
image: kolay0ne/app_go:lab8
image: kolay0ne/app_go:lab12
ports:
- "5500:5000"
logging:
Expand All @@ -32,6 +33,8 @@ services:
resources: {limits: {memory: 20M}}
networks:
- prometheus
volumes:
- go-persistent:/persistent

loki:
image: grafana/loki:2.9.2
Expand Down

0 comments on commit 029be44

Please sign in to comment.