Skip to content

Commit

Permalink
Merge pull request #5 from Ulas-Scan/4-swagger
Browse files Browse the repository at this point in the history
4 swagger
  • Loading branch information
iyoubee authored Jun 2, 2024
2 parents a98908b + 511abe5 commit bcea266
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
73 changes: 73 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Deploy to GCE

env:
APP_NAME: ulascan-be
GCE_INSTANCE_IP: ${{ secrets.GCE_INSTANCE_IP }}
GCE_INSTANCE_NAME: ${{ secrets.GCE_INSTANCE_NAME }}
GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }}
GCP_ZONE: ${{ secrets.GCP_ZONE }}
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}

- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}

- name: Configure docker for Artifact Registry
run: |
gcloud --quiet auth configure-docker $GCP_ZONE-docker.pkg.dev
- name: Build and push Docker image
run: |
docker build -t $GCP_ZONE-docker.pkg.dev/$GCP_PROJECT_ID/$APP_NAME:latest .
docker push $GCP_ZONE-docker.pkg.dev/$GCP_PROJECT_ID/$APP_NAME:latest
deploy:
needs: build
runs-on: ubuntu-latest

steps:
- name: Install SSH client
run: sudo apt-get install -y ssh

- name: Set up SSH
run: |
echo "$SSH_PRIVATE_KEY" > ssh_key
chmod 600 ssh_key
- name: Deploy to GCE
run: |
ssh -o StrictHostKeyChecking=no \
-i ssh_key \
ulascan@$GCE_INSTANCE_IP \
"gcloud --quiet auth configure-docker $GCP_ZONE-docker.pkg.dev \
&& docker pull $GCP_ZONE-docker.pkg.dev/$GCP_PROJECT_ID/$APP_NAME:latest \
&& docker stop $APP_NAME || true \
&& docker rm --force $APP_NAME || true \
&& docker run -d --name $APP_NAME -p 8080:80 $GCP_ZONE-docker.pkg.dev/$GCP_PROJECT_ID/$APP_NAME:latest \
&& docker image prune -f"
- name: Cleanup SSH key
run: rm ssh_key
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:latest

WORKDIR /app

COPY . .

RUN go mod download && go mod verify

RUN go build -v -o main .

EXPOSE 8080

CMD ["/app/main"]
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module ulascan-be

go 1.21.6

require github.com/gin-gonic/gin v1.10.0
require (
github.com/gin-gonic/gin v1.10.0
github.com/sirupsen/logrus v1.9.3
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -97,6 +99,7 @@ golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func main() {

// SERVER
server := gin.Default()
// Use middleware
server.Use(middleware.Logger())
server.Use(middleware.Recovery())
server.Use(middleware.CORSMiddleware())

// ROUTES
Expand Down
36 changes: 36 additions & 0 deletions middleware/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package middleware

import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

func Logger() gin.HandlerFunc {
return func(c *gin.Context) {
// Log incoming request
logrus.WithFields(logrus.Fields{
"method": c.Request.Method,
"path": c.Request.URL.Path,
}).Info("Incoming request")

c.Next()

// Log outgoing response
logrus.WithField("status", c.Writer.Status()).Info("Outgoing response")
}
}

func Recovery() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
// Log panic
logrus.WithField("error", err).Error("Panic recovered")

// Abort request
c.AbortWithStatus(500)
}
}()
c.Next()
}
}

0 comments on commit bcea266

Please sign in to comment.