This commit is contained in:
2024-02-20 17:15:27 +08:00
committed by huty
parent 6706e1a633
commit 34158042ad
1529 changed files with 177765 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
FROM golang:1.15-alpine AS builder
ENV CGO_ENABLED=0
WORKDIR /src
COPY go.mod .
RUN go mod download
COPY main.go .
RUN go build -o /server
# app
FROM alpine:3.15
ENV IMAGE_API_URL="http://apod-api/image" \
ACCESS_API_URL="http://apod-log/access-log"
CMD ["/web/server"]
WORKDIR /web
COPY index.html .
COPY --from=builder /server .

View File

@@ -0,0 +1,5 @@
module kiamol/image-gallery
go 1.15
require github.com/prometheus/client_golang v1.7.1

View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image Gallery</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
crossorigin="anonymous"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"/>
</head>
<body>
<div class="text-center">
<h1 class="display-4">Image Gallery</h1>
<h2>{{.Caption}}</h2>
<img src="{{.Url}}"/>
<footer class="blockquote-footer">&copy; {{.Copyright}}</footer>
</div>
</body>
</html>

View File

@@ -0,0 +1,78 @@
package main
import (
"bytes"
"encoding/json"
"html/template"
"io/ioutil"
"math/rand"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus"
)
type Image struct {
Url string `json:"url"`
Caption string `json:"caption"`
Copyright string `json:"copyright"`
}
type AccessLog struct {
ClientIP string `json:"clientIp"`
}
func main() {
tmpl := template.Must(template.ParseFiles("index.html"))
imageApiUrl := os.Getenv("IMAGE_API_URL")
logApiUrl := os.Getenv("ACCESS_API_URL")
//re-use HTTP client with minimal keep-alive
tr := &http.Transport{
MaxIdleConns: 1,
IdleConnTimeout: 1 * time.Second,
}
client := &http.Client{Transport: tr}
//create Prometheus metrics
inFlightGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "image_gallery_in_flight_requests",
Help: "Image Gallery - in-flight requests",
})
requestCounter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "image_gallery_requests_total",
Help: "Image Gallery - total requests",
},
[]string{"code", "method"},
)
prometheus.MustRegister(inFlightGauge, requestCounter)
rand.Seed(time.Now().UnixNano())
indexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response,_ := client.Get(imageApiUrl)
defer response.Body.Close()
data,_ := ioutil.ReadAll(response.Body)
image := Image{}
json.Unmarshal([]byte(data), &image)
tmpl.Execute(w, image)
log := AccessLog{
ClientIP: r.RemoteAddr,
}
jsonLog,_ := json.Marshal(log)
response,_ = client.Post(logApiUrl, "application/json", bytes.NewBuffer(jsonLog))
defer response.Body.Close()
})
wrappedIndexHandler := promhttp.InstrumentHandlerInFlight(inFlightGauge,
promhttp.InstrumentHandlerCounter(requestCounter, indexHandler))
http.Handle("/", wrappedIndexHandler)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":80", nil)
}