Skip to content

Commit

Permalink
Merge pull request #1 from AdiAkhileshSingh15/deployment
Browse files Browse the repository at this point in the history
Add docker
  • Loading branch information
AdiAkhileshSingh15 authored Jul 30, 2023
2 parents 4af090f + 9bb457f commit aad3ac8
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 29 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ jobs:
test:
name: Test
runs-on: ubuntu-latest
env:
DB_NAME: ${{ secrets.DB_NAME }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASS: ${{ secrets.DB_PASS }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_PORT: ${{ secrets.DB_PORT }}
SSL_MODE: ${{ secrets.SSL_MODE }}
PRODUCTION: ${{ secrets.PRODUCTION }}
CACHE: ${{ secrets.CACHE }}

services:
postgres:
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Deploy to production

on:
push:
branches: [ "main" ]

jobs:

build:
name: Build image
runs-on: ubuntu-latest

steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-south-1

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push docker image to Amazon ECR
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: bookmyroom
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coverage.out
.vscode
.vscode
.env
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Build stage
FROM golang:1.20.6-alpine3.18 AS builder
WORKDIR /app
COPY . .
RUN go build -o bookmyroom cmd/web/main.go cmd/web/middleware.go cmd/web/routes.go cmd/web/send-mail.go
RUN go install github.com/gobuffalo/pop/v6/soda@latest

# Run stage
FROM alpine:3.18
WORKDIR /app
COPY --from=builder /app/bookmyroom .
COPY --from=builder /go/bin/soda ./soda
COPY cmd/web/.env .
COPY migrations ./migrations
COPY database.yml .
COPY start.sh .
COPY wait-for.sh .

EXPOSE 8080
CMD ["/app/bookmyroom"]
ENTRYPOINT [ "/app/start.sh" ]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
go run cmd/web/main.go cmd/web/middleware.go cmd/web/routes.go cmd/web/send-mail.go -dbname=bookmyroom -dbuser=postgres -dbpass=adi123
go run cmd/web/main.go cmd/web/middleware.go cmd/web/routes.go cmd/web/send-mail.go

test:
go test -v ./...
Expand Down
49 changes: 30 additions & 19 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"encoding/gob"
"flag"
"fmt"
"log"
"net/http"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/AdiAkhileshSingh15/bookmyroom/internal/models"
"github.com/AdiAkhileshSingh15/bookmyroom/internal/render"
"github.com/alexedwards/scs/v2"
"github.com/joho/godotenv"
)

const portNumber = ":8080"
Expand All @@ -34,10 +34,10 @@ func main() {

defer close(app.MailChan)

fmt.Println(fmt.Sprintf("Starting mail listener..."))
fmt.Println("Starting mail listener...")
listenForMail()

fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber))
fmt.Printf("Starting application on port %s", portNumber)

srv := &http.Server{
Addr: portNumber,
Expand All @@ -54,29 +54,40 @@ func run() (*driver.DB, error) {
gob.Register(models.Restriction{})
gob.Register(map[string]int{})

inProduction := flag.Bool("production", true, "Application is in production")
useCache := flag.Bool("cache", true, "Use template cache")
dbHost := flag.String("dbhost", "localhost", "Database host")
dbName := flag.String("dbname", "bookmyroom", "Database name")
dbUser := flag.String("dbuser", "postgres", "Database user")
dbPass := flag.String("dbpass", "adi123", "Database password")
dbPort := flag.String("dbport", "5432", "Database port")
dbSSL := flag.String("dbssl", "disable", "Database ssl settings (disable, prefer, require)")
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}

inProduction := os.Getenv("PRODUCTION")
useCache := os.Getenv("CACHE")
dbHost := os.Getenv("DB_HOST")
dbName := os.Getenv("DB_NAME")
dbUser := os.Getenv("DB_USER")
dbPass := os.Getenv("DB_PASS")
dbPort := os.Getenv("DB_PORT")
dbSSL := os.Getenv("SSL_MODE")

if inProduction == "true" {
app.InProduction = true
} else {
app.InProduction = false
}

flag.Parse()
if useCache == "true" {
app.UseCache = true
} else {
app.UseCache = false
}

if *dbName == "" || *dbUser == "" || *dbPass == "" {
fmt.Println("Missing required flags")
if dbName == "" || dbUser == "" || dbPass == "" {
fmt.Println("Missing required environment variables")
os.Exit(1)
}

mailChan := make(chan models.MailData)
app.MailChan = mailChan

// change this to true when in production
app.InProduction = *inProduction
app.UseCache = *useCache

infoLog = log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
app.InfoLog = infoLog

Expand All @@ -94,7 +105,7 @@ func run() (*driver.DB, error) {
// connect to database

log.Println("Connecting to database...")
connectionString := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s", *dbHost, *dbPort, *dbName, *dbUser, *dbPass, *dbSSL)
connectionString := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s", dbHost, dbPort, dbName, dbUser, dbPass, dbSSL)
db, err := driver.ConnectSQL(connectionString)
if err != nil {
log.Fatal("Cannot connect to database! Dying...")
Expand Down
5 changes: 2 additions & 3 deletions cmd/web/middleware_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"net/http"
"testing"
)
Expand All @@ -15,7 +14,7 @@ func TestNoSurf(t *testing.T) {
case http.Handler:
// do nothing
default:
t.Error(fmt.Sprintf("type is not http.Handler, but it %T", v))
t.Errorf("type is not http.Handler, but it %T", v)
}
}

Expand All @@ -28,6 +27,6 @@ func TestSessionLoad(t *testing.T) {
case http.Handler:
// do nothing
default:
t.Error(fmt.Sprintf("type is not http.Handler, but it %T", v))
t.Errorf("type is not http.Handler, but it %T", v)
}
}
3 changes: 1 addition & 2 deletions cmd/web/routes_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"testing"

"github.com/AdiAkhileshSingh15/bookmyroom/internal/config"
Expand All @@ -16,7 +15,7 @@ func TestRoutes(t *testing.T) {
case *chi.Mux:
//do nothing
default:
t.Error(fmt.Sprintf("type is not *chi.Mux, but it %T", v))
t.Errorf("type is not *chi.Mux, but it %T", v)
}

}
6 changes: 3 additions & 3 deletions database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ development:
database: bookmyroom
user: postgres
password: adi123
host: 127.0.0.1
host: postgres
pool: 5

test:
url: {{envOr "TEST_DATABASE_URL" "postgres://postgres:postgres@127.0.0.1:5432/myapp_test"}}
url: {{envOr "TEST_DATABASE_URL" "postgres://postgres:postgres@postgres:5432/myapp_test"}}

production:
url: {{envOr "DATABASE_URL" "postgres://postgres:postgres@127.0.0.1:5432/myapp_production"}}
url: {{envOr "DATABASE_URL" "postgres://postgres:postgres@postgres:5432/myapp_production"}}
21 changes: 21 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
services:
postgres:
image: postgres:alpine
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=adi123
- POSTGRES_DB=bookmyroom
ports:
- "5432:5432"
api:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
- DB_HOST=postgres
depends_on:
- postgres
entrypoint: ["/app/wait-for.sh", "postgres:5432", "--", "/app/start.sh"]
command: ["/app/bookmyroom"]
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 // indirect
golang.org/x/text v0.9.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ github.com/jackc/pgx/v5 v5.4.1 h1:oKfB/FhuVtit1bBM3zNRRsZ925ZkMN3HXL+LgLUM9lE=
github.com/jackc/pgx/v5 v5.4.1/go.mod h1:q6iHT8uDNXWiFNOlRqJzBTaSH3+2xCXkokxHZC5qWFY=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/justinas/nosurf v1.1.1 h1:92Aw44hjSK4MxJeMSyDa7jwuI9GR2J/JCQiaKvXXSlk=
github.com/justinas/nosurf v1.1.1/go.mod h1:ALpWdSbuNGy2lZWtyXdjkYv4edL23oSEgfBT1gPJ5BQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
9 changes: 9 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

set -e

echo "run db migrations"
/app/soda migrate

echo "start the app"
exec "$@"
Loading

0 comments on commit aad3ac8

Please sign in to comment.