Skip to content

Instantly share code, notes, and snippets.

@paambaati
Last active June 22, 2023 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paambaati/be7dada31ad691939663210993035c63 to your computer and use it in GitHub Desktop.
Save paambaati/be7dada31ad691939663210993035c63 to your computer and use it in GitHub Desktop.
Simple Go server that only prints version as response, a Dockerfile to quickly deploy it, and a k6 script that tests it
FROM golang:alpine AS builder
# Install git.
# Git is required for fetching the dependencies.
RUN apk update && apk add --no-cache git
WORKDIR $GOPATH/src/vserver/
COPY . .
# Fetch dependencies.
# Using go get.
RUN go get -d -v
# Build the binary.
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/vserver
FROM scratch
COPY --from=builder /go/bin/vserver /go/bin/vserver
ENV SERVER_PORT=8080
ENV SERVER_VERSION="1.0.0"
ENTRYPOINT ["/go/bin/vserver"]
import http from "k6/http";
export const options = {
vus: 1,
duration: "10m",
noConnectionReuse: true,
insecureSkipTLSVerify: true,
};
export default function () {
const response = http.get("https://api.foldapp.dev/");
console.log(new Date().toISOString(), "Running version: ", response.body)
}
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
port, ok := os.LookupEnv("SERVER_PORT")
if !ok {
log.Fatal("$SERVER_PORT not set")
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
version, ok := os.LookupEnv("SERVER_VERSION")
if !ok {
http.Error(w, "$SERVER_VERSION not set", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%s", version)
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment