Skip to content

Commit

Permalink
Add building docker im GHAction
Browse files Browse the repository at this point in the history
  • Loading branch information
matbur committed Nov 16, 2024
1 parent ad3e281 commit 999534e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ jobs:

- name: Test
run: go test -v ./...

- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: matbur/image-text:latest
12 changes: 5 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN go build -o app ./cmd/image-text
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o bin ./cmd/image-text

FROM alpine:3.20

USER nobody:nobody
FROM scratch

WORKDIR /app
COPY --from=build-env app .
CMD ["./app"]
COPY --from=build-env /app/bin /server
ENTRYPOINT ["/server"]
9 changes: 6 additions & 3 deletions cmd/image-text/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

type config struct {
Addr string `envconfig:"ADDR" default:":8021"`
Port string `envconfig:"PORT" default:"8080"`
Mode string `envconfig:"MODE"`
}

Expand All @@ -32,18 +32,21 @@ func main() {
os.Exit(1)
}

addr := fmt.Sprintf(":%s", cfg.Port)

switch cfg.Mode {
case "TEST":
mode2(cfg.Addr)
mode2(addr)
default:
mode1(cfg.Addr)
mode1(addr)
}
}

func mode1(addr string) {
slog.Info("Starting server", "addr", addr)

http.HandleFunc("/favicon.ico", server.HandleFavicon)
http.HandleFunc("/healthz", server.HandleHealthz)
http.HandleFunc("/", server.HandleMain())
if err := http.ListenAndServe(addr, nil); err != nil {
if errors.Is(err, http.ErrServerClosed) {
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ services:
build:
dockerfile: Dockerfile
ports:
- "8022:8022"
environment:
- ADDR=:8022
- "8022:8080"
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ func handleDocs(w http.ResponseWriter, r *http.Request) {
slog.Error("Failed to write docs", "err", err)
}
}

func HandleHealthz(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
w.WriteHeader(http.StatusOK)
}

0 comments on commit 999534e

Please sign in to comment.