Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add keploy coverage support #177

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ REDIS_ADDRESS=redis:6379
COPILOT_DB_CREDS_VIA_SECRETS_MANAGER=false
SERVER_PORT=9000
ENV_INJECTION=false
APP_PATH=/Users/charankamarapu/wednesday/go-template
GOCOVERDIR=coverage-reports
56 changes: 33 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM golang:1.22-alpine3.19 AS builder
RUN apk add build-base

RUN mkdir /app
ADD . /app
ARG APP_PATH

WORKDIR /app
RUN mkdir -p ${APP_PATH}
ADD . ${APP_PATH}

WORKDIR ${APP_PATH}
ARG ENVIRONMENT_NAME
ENV ENVIRONMENT_NAME=$ENVIRONMENT_NAME
RUN GOARCH=amd64 \
Expand All @@ -14,39 +16,47 @@ RUN GOARCH=amd64 \


RUN go run ./cmd/seeder/main.go
RUN go build -o ./output/server ./cmd/server/main.go
RUN go build -o ./output/migrations ./cmd/migrations/main.go
RUN go build -o ./output/seeder ./cmd/seeder/exec/seed.go
RUN go build -cover -o ./output/server ./cmd/server/main.go
RUN go build -cover -o ./output/migrations ./cmd/migrations/main.go
RUN go build -cover -o ./output/seeder ./cmd/seeder/exec/seed.go


FROM alpine:latest
RUN apk add --no-cache libc6-compat
RUN apk add --no-cache --upgrade bash
RUN apk add --no-cache dumb-init
RUN addgroup -S nonroot \
&& adduser -S nonroot -G nonroot


ARG APP_PATH
ARG ENVIRONMENT_NAME
ENV ENVIRONMENT_NAME=$ENVIRONMENT_NAME

RUN mkdir -p /app/
WORKDIR /app
USER nonroot
RUN echo ${APP_PATH}
RUN mkdir -p ${APP_PATH}/
WORKDIR ${APP_PATH}

COPY /scripts /app/scripts/
COPY --from=builder /app/output/ /app/
COPY --from=builder /app/cmd/seeder/exec/build/ /app/cmd/seeder/exec/build/

COPY ./.env.* /app/output/
COPY ./.env.* /app/output/cmd/seeder/exec/build/
COPY ./.env.* /app/output/cmd/seeder/exec/
COPY ./.env.* /app/output/cmd/seeder/
COPY ./.env.* /app/output/cmd/
COPY ./.env.* /app/
COPY ./scripts/ /app/
COPY --from=builder /app/internal/migrations/ /app/internal/migrations/
CMD ["bash","./migrate-and-run.sh"]
COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Clean the coverage-reports folder
USER nonroot

COPY /scripts ${APP_PATH}/scripts/
COPY --from=builder ${APP_PATH}/output/ ${APP_PATH}/
COPY --from=builder ${APP_PATH}/cmd/seeder/exec/build/ ${APP_PATH}/cmd/seeder/exec/build/

COPY ./.env.* ${APP_PATH}/output/
COPY ./.env.* ${APP_PATH}/output/cmd/seeder/exec/build/
COPY ./.env.* ${APP_PATH}/output/cmd/seeder/exec/
COPY ./.env.* ${APP_PATH}/output/cmd/seeder/
COPY ./.env.* ${APP_PATH}/output/cmd/
COPY ./.env.* ${APP_PATH}/
COPY ./scripts/ ${APP_PATH}/
COPY --from=builder ${APP_PATH}/internal/migrations/ ${APP_PATH}/internal/migrations/

ENTRYPOINT ["dumb-init", "--"]
STOPSIGNAL SIGINT
CMD ["/entrypoint.sh"]
EXPOSE 9000

16 changes: 12 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
version: '3.8'
version: "3.8"

services:
db:
image: postgres:13
restart: always
env_file:
- .env.docker

environment:
- POSTGRES_USER=${PSQL_USER}
- POSTGRES_PASSWORD=${PSQL_PASS}
Expand All @@ -26,7 +26,12 @@ services:
command: tcp db:${PSQL_PORT} -t 30s -i 250ms

app:
build: .
build:
context: .
args:
APP_PATH: ${APP_PATH}
image: go-template_app:latest
container_name: got
restart: always
env_file:
- ./.env.${ENVIRONMENT_NAME}
Expand All @@ -35,5 +40,8 @@ services:
condition: service_completed_successfully
ports:
- ${SERVER_PORT}:${SERVER_PORT}
volumes:
- ${APP_PATH}/coverage-reports:${APP_PATH}/coverage-reports
environment:
ENVIRONMENT_NAME: ${ENVIRONMENT_NAME}
ENVIRONMENT_NAME: ${ENVIRONMENT_NAME}
stop_grace_period: 50s
7 changes: 7 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# Clean the coverage-reports folder
rm -rf ./coverage-reports/*

# Run your application or migration script
exec bash ./migrate-and-run.sh
3 changes: 2 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"os/signal"
"syscall"
"time"

controller "go-template/internal/controller"
Expand Down Expand Up @@ -91,7 +92,7 @@ func Start(e *echo.Echo, cfg *Config) {
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 10 seconds.
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
signal.Notify(quit, syscall.SIGINT)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer func() {
Expand Down
21 changes: 20 additions & 1 deletion scripts/migrate-and-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@

echo $ENVIRONMENT_NAME

handle_int() {
echo "SIGINT received, forwarding to child process..."
kill -INT "$child" 2>/dev/null
echo "Waiting for child process to exit..."
wait "$child"
echo "Child process exited. Waiting for coverage data to be fully written..."
echo "Exiting after delay..."
exit 0
}

# Trap SIGINT signal
trap 'handle_int' INT TERM

./migrations

if [[ $ENVIRONMENT_NAME == "docker" ]]; then
echo "seeding"
./seeder
fi

./server
./server &

child=$!
echo "Started process with PID $child"

# Wait for child process to finish
wait "$child"