fireup / example-time-api read public
fireme example: a tiny Go HTTP API with its own Dockerfile
A tiny Go time API with its own Dockerfile
Browse files| @@ -0,0 +1,11 @@ | |
| + | FROM golang:1.23 AS build |
| + | WORKDIR /src |
| + | COPY go.mod ./ |
| + | COPY *.go ./ |
| + | RUN CGO_ENABLED=0 go build -o /out/time-api . |
| + | |
| + | FROM gcr.io/distroless/static-debian12 |
| + | COPY --from=build /out/time-api /time-api |
| + | ENV PORT=8080 |
| + | EXPOSE 8080 |
| + | ENTRYPOINT ["/time-api"] |
| @@ -0,0 +1,14 @@ | |
| + | # time-api |
| + | |
| + | A tiny HTTP API that returns the current UTC time. |
| + | |
| + | - `GET /` returns `{"utc": "2026-10-19T21:00:00Z"}` |
| + | - `GET /healthz` returns `ok` |
| + | |
| + | It listens on `$PORT` (default 8080). |
| + | |
| + | ```sh |
| + | go run . |
| + | # or |
| + | docker build -t time-api . && docker run -p 8080:8080 time-api |
| + | ``` |
| @@ -0,0 +1,3 @@ | |
| + | module example.com/time-api |
| + | |
| + | go 1.23 |
| @@ -0,0 +1,25 @@ | |
| + | package main |
| + | |
| + | import ( |
| + | "encoding/json" |
| + | "log" |
| + | "net/http" |
| + | "os" |
| + | "time" |
| + | ) |
| + | |
| + | func main() { |
| + | port := os.Getenv("PORT") |
| + | if port == "" { |
| + | port = "8080" |
| + | } |
| + | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| + | w.Header().Set("Content-Type", "application/json") |
| + | json.NewEncoder(w).Encode(map[string]string{"utc": time.Now().UTC().Format(time.RFC3339)}) |
| + | }) |
| + | http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { |
| + | w.Write([]byte("ok\n")) |
| + | }) |
| + | log.Printf("listening on :%s", port) |
| + | log.Fatal(http.ListenAndServe(":"+port, nil)) |
| + | } |