Skip to content

Commit a4d2d82

Browse files
committed
Initial commit
1 parent db7c58f commit a4d2d82

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+7237
-1
lines changed

.env.example

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ───────────────────────────────────────────────
2+
# 🌟 Application Configuration
3+
# ───────────────────────────────────────────────
4+
SERVER_PORT=8080
5+
DEBUG=true
6+
DISABLE_LOGS=false
7+
LOG_FORMAT=json
8+
LOG_CALLER=true
9+
LOG_STACKTRACE=false
10+
11+
# ───────────────────────────────────────────────
12+
# 🌟 Database Configuration
13+
# ───────────────────────────────────────────────
14+
DB_HOST=db
15+
DB_PORT=3306
16+
DB_USER=myuser
17+
DB_PASSWORD=mypassword
18+
DB_NAME=mydatabase
19+
20+
# ───────────────────────────────────────────────
21+
# 🌟 Golang Tools Versions
22+
# ───────────────────────────────────────────────
23+
SWAG_VERSION=v1.16.4
24+
MIGRATE_VERSION=v4.16.2
25+
LINT_VERSION=v1.59.1
26+
IMPORTS_VERSION=v0.24.0
27+
VULN_VERSION=v1.1.3

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
go-version: '1.21'
2323

2424
- name: Install dependencies
25-
run: go get .
25+
run: go get ./...
2626

2727
- name: Build
2828
run: go build -v ./...

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ go.work.sum
2323

2424
# env file
2525
.env
26+
27+
#ignore build
28+
build/*
29+
/bkp
30+
31+
#ignore test coverage
32+
.coverage

Dockerfile

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# ───────────────────────────────────────────────────────────
2+
# 🌟 STAGE 1: BUILD GO APPLICATION
3+
# ───────────────────────────────────────────────────────────
4+
FROM golang:1.21 AS builder
5+
6+
# Accept build arguments
7+
ARG SERVER_PORT
8+
ARG DB_HOST
9+
ARG DB_PORT
10+
ARG DB_USER
11+
ARG DB_PASSWORD
12+
ARG DB_NAME
13+
ARG DEBUG
14+
ARG DISABLE_LOGS
15+
ARG LOG_FORMAT
16+
ARG LOG_CALLER
17+
ARG LOG_STACKTRACE
18+
ARG SWAG_VERSION
19+
ARG MIGRATE_VERSION
20+
ARG LINT_VERSION
21+
ARG IMPORTS_VERSION
22+
ARG VULN_VERSION
23+
24+
# Set environment variables for versions (same as Makefile)
25+
ENV SERVER_PORT=$SERVER_PORT
26+
ENV DB_HOST=$DB_HOST
27+
ENV DB_PORT=$DB_PORT
28+
ENV DB_USER=$DB_USER
29+
ENV DB_PASSWORD=$DB_PASSWORD
30+
ENV DB_NAME=$DB_NAME
31+
ENV DEBUG=$DEBUG
32+
ENV DISABLE_LOGS=$DISABLE_LOGS
33+
ENV LOG_FORMAT=$LOG_FORMAT
34+
ENV LOG_CALLER=$LOG_CALLER
35+
ENV LOG_STACKTRACE=$LOG_STACKTRACE
36+
ARG SWAG_VERSION=$SWAG_VERSION
37+
ARG MIGRATE_VERSION=$MIGRATE_VERSION
38+
ARG LINT_VERSION=$LINT_VERSION
39+
ARG IMPORTS_VERSION=$IMPORTS_VERSION
40+
ARG VULN_VERSION=$VULN_VERSION
41+
42+
# Set the working directory inside the container
43+
WORKDIR /app
44+
45+
# Copy Go modules manifests
46+
COPY go.mod go.sum ./
47+
48+
# Download Go dependencies
49+
RUN go mod tidy && go mod download
50+
51+
# Install Swagger, GolangCI-Lint, GoImports, and Govulncheck
52+
RUN go install github.com/swaggo/swag/cmd/swag@${SWAG_VERSION} && \
53+
go install golang.org/x/tools/cmd/goimports@${IMPORTS_VERSION} && \
54+
go install golang.org/x/vuln/cmd/govulncheck@${VULN_VERSION} && \
55+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@${LINT_VERSION}
56+
57+
# ───────────────────────────────────────────────────────────
58+
# ✅ INSTALL `golang-migrate` USING OFFICIAL METHOD
59+
# ───────────────────────────────────────────────────────────
60+
RUN curl -L https://github.com/golang-migrate/migrate/releases/download/${MIGRATE_VERSION}/migrate.linux-amd64.tar.gz -o migrate.tar.gz && \
61+
tar -xvf migrate.tar.gz && \
62+
mv migrate /usr/local/bin/migrate && \
63+
chmod +x /usr/local/bin/migrate && \
64+
rm -f migrate.tar.gz
65+
66+
# Copy the entire project into the container
67+
COPY . .
68+
69+
# 🔥 Ensure `/app/build/` exists
70+
RUN mkdir -p /app/build
71+
72+
# Generate Swagger documentation
73+
RUN make generate_docs
74+
75+
# 🔥 Build the Go application using Makefile
76+
RUN make build
77+
78+
# ───────────────────────────────────────────────────────────
79+
# 🌟 STAGE 2: CREATE A SMALLER FINAL IMAGE
80+
# ───────────────────────────────────────────────────────────
81+
FROM debian:bullseye-slim
82+
83+
# Set the working directory
84+
WORKDIR /app
85+
86+
# Install dependencies required to run the app
87+
RUN apt-get update && apt-get install -y ca-certificates curl && \
88+
rm -rf /var/lib/apt/lists/*
89+
90+
# Copy the compiled Go binary from the builder stage
91+
COPY --from=builder /app/build/server /app/server
92+
93+
# Copy the Swagger docs to serve them later
94+
COPY --from=builder /app/docs /app/docs
95+
96+
# Copy the migration files
97+
COPY --from=builder /app/package/database/migrations /app/migrations
98+
99+
# Copy the migrate tool from the builder stage
100+
COPY --from=builder /usr/local/bin/migrate /usr/local/bin/migrate
101+
102+
# Expose the application port
103+
EXPOSE ${SERVER_PORT}
104+
105+
# Run the application
106+
CMD ["/app/server"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Mitul Shah
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)