Skip to content

Commit

Permalink
Merge pull request #17 from wednesday-solutions/feat/updating-env-seeder
Browse files Browse the repository at this point in the history
Feat/updating env seeder
  • Loading branch information
gang-wednesday authored Apr 17, 2023
2 parents a5915bf + c79cbf6 commit 507607d
Show file tree
Hide file tree
Showing 24 changed files with 327 additions and 127 deletions.
14 changes: 14 additions & 0 deletions .env.base
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ENV_INJECTION=false
SERVER_DEBUG=true
SERVER_READ_TIMEOUT=10
SERVER_WRITE_TIMEOUT=5
JWT_MIN_SECRET_LENGTH=64
JWT_DURATION_MINUTES=1440
JWT_REFRESH_DURATION=3499200
JWT_MAX_REFRESH=1440
JWT_SIGNING_ALGORITHM=HS256
DB_LOG_QUERIES=true
DB_TIMEOUT_SECONDS=5
APP_MIN_PASSWORD_STR=1
SERVER_PORT=9000
COPILOT_DB_CREDS_VIA_SECRETS_MANAGER=false
19 changes: 6 additions & 13 deletions .env.docker
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
APP_MIN_PASSWORD_STR=1
DB_LOG_QUERIES=true
DB_TIMEOUT_SECONDS=5
ENVIRONMENT_NAME=docker
JWT_DURATION_MINUTES=1440
JWT_MAX_REFRESH=1440
JWT_MIN_SECRET_LENGTH=64
JWT_REFRESH_DURATION=3499200
JWT_SECRET=aa60b52e-7974-11ea-bc55-0242ac130003b08d6654-7974-11ea-bc55-0242ac130003-b08d6654-7974-11ea-bc55-0242ac130003
JWT_SIGNING_ALGORITHM=HS256

REDIS_ADDRESS=redis:6379
COPILOT_DB_CREDS_VIA_SECRETS_MANAGER=false
SERVER_PORT=9000


MYSQL_DBNAME=go_template
MYSQL_DEBUG=true
MYSQL_HOST=db
Expand All @@ -19,8 +17,3 @@ MYSQL_USER=def_user
REDIS_ADDRESS=redis:6379
SERVER_DEBUG=true
SERVER_PORT=9000
SERVER_READ_TIMEOUT=10
SERVER_WRITE_TIMEOUT=5
SERVICE_NAME=goTemplateMySQL
INSECURE_MODE=true
OTEL_EXPORTER_OTLP_ENDPOINT=otel:4317
19 changes: 9 additions & 10 deletions .env.local
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
APP_MIN_PASSWORD_STR=1
DB_LOG_QUERIES=true
DB_SSL=disable
DB_TIMEOUT_SECONDS=5
ENVIRONMENT_NAME=local
JWT_DURATION_MINUTES=1440
JWT_MAX_REFRESH=1440
JWT_MIN_SECRET_LENGTH=64
JWT_REFRESH_DURATION=3499200
JWT_SECRET=aa60b52e-7974-11ea-bc55-0242ac130003b08d6654-7974-11ea-bc55-0242ac130003-b08d6654-7974-11ea-bc55-0242ac130003
JWT_SIGNING_ALGORITHM=HS256
REDIS_ADDRESS=localhost:6379
SERVICE_NAME=goTemplate
INSECURE_MODE=true
OTEL_EXPORTER_OTLP_ENDPOINT=localhost:3306
COPILOT_DB_CREDS_VIA_SECRETS_MANAGER=false



MYSQL_DBNAME=go_template
MYSQL_DEBUG=true
MYSQL_HOST=localhost
Expand All @@ -24,4 +23,4 @@ SERVER_READ_TIMEOUT=10
SERVER_WRITE_TIMEOUT=5
SERVICE_NAME=goTemplateMySQL
INSECURE_MODE=true
OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4317
OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4317
30 changes: 17 additions & 13 deletions .github/workflows/go-template-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-python@v3
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.19
- name: Install pre-commit dependencies
run: |
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
Expand All @@ -34,21 +36,23 @@ jobs:
run: go build ./...
- name: Test
run: ./scripts/test.sh
- uses: codecov/codecov-action@v2
- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action@master
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
files: ./coverage.out
verbose: true
- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
args: >
-Dsonar.scm.revision=${{ github.event.pull_request.head.sha }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
args: >
-Dsonar.organization=${{ secrets.SONAR_ORG}}
-Dsonar.projectKey=${{ secrets.SONAR_PROJECT_KEY}}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
- uses: sonarsource/sonarqube-quality-gate-action@master
timeout-minutes: 5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
# testing image url

golangci-lint:
runs-on: ubuntu-latest
steps:
Expand Down
52 changes: 35 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
FROM golang:1.18-alpine3.16
FROM golang:1.18-alpine3.16 AS builder
RUN apk add build-base

RUN mkdir -p /go/src/github.com/wednesday-solutions/go-template-mysql

ADD . /go/src/github.com/wednesday-solutions/go-template-mysql

WORKDIR /go/src/github.com/wednesday-solutions/go-template-mysql
RUN mkdir /app
ADD . /app

WORKDIR /app
ARG ENVIRONMENT_NAME
ENV ENVIRONMENT_NAME=$ENVIRONMENT_NAME
RUN GOARCH=amd64 \
GOOS=linux \
CGO_ENABLED=0 \
go mod vendor
RUN mkdir -p /go/src/github.com/wednesday-solutions/go-template-mysql/output
RUN go build -o ./output/main ./cmd/server/main.go
RUN go build -o ./output/seeder ./cmd/seeder/main.go


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


FROM golang:1.18-alpine3.16
ARG ENVIRONMENT_NAME
RUN mkdir -p /app/
ADD . /app
FROM alpine:latest
RUN apk add --no-cache libc6-compat
RUN apk add --no-cache --upgrade bash
RUN addgroup -S nonroot \
&& adduser -S nonroot -G nonroot

WORKDIR /app

COPY --from=0 /go/src/github.com/wednesday-solutions/go-template-mysql/output /app/
ARG ENVIRONMENT_NAME
ENV ENVIRONMENT_NAME=$ENVIRONMENT_NAME

CMD ["sh", "/app/scripts/migrate-and-run.sh"]
EXPOSE 9000
RUN mkdir -p /app/
WORKDIR /app
USER nonroot

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"]
38 changes: 35 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,39 @@ gqlgen generate

### Create infrastructure

### Precautions
1. Please ensure the maximum limit of number of vpc's in a region has not reached it's limit.
2. The maximum limit of number of buckets has not reached it's limits.
3. Please make the changes to the manifest file of the service.
4. Ensure that the aws cli has been installed and configured with the appropriate credentials and profiles.



Application name should container only lowercase letters. No hyphens and underscores or any other special characters.



```
./scripts/setup-ecs.sh gotemplatemysql dev
./scripts/setup-ecs.sh gotemplate dev
```



Also add the environment variables to the task,add this block of yml code in ${service name}/manifest.yaml:
```
variables:
ENVIRONMENT_NAME: develop
#to inject our .env file from aws s3 inside the container
taskdef_overrides:
- path: ContainerDefinitions[0].EnvironmentFiles[0]
value:
type: 's3'
value: 'arn:aws:s3:::gotemplate-dev-bucket/develop/.env'
```
Make sure that the manifest.yml has http.path: '/'

```
Expand All @@ -246,13 +273,18 @@ http:
# healthcheck: '/'
```
### To deploy

```
./scripts/deploy-ecs.sh gotemplate dev
```


### Update infrastructure

```
./scripts/update-ecs.sh gotemplatemysql dev
./scripts/update-ecs.sh gotemplate dev
```

## Testing

Get test coverage using
Expand Down
3 changes: 3 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
version: '2'

tasks:
hello:
cmds:
- echo "hello world"
test:
cmds:
- echo " *** Running Coverage Tests ***"
Expand Down
45 changes: 45 additions & 0 deletions cmd/seeder/exec/seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"

"golang.org/x/exp/slices"
)

func main() {

base, _ := os.Getwd()
log.Println(base)

base += "/cmd/seeder/exec/build"
fmt.Println(base)

files, err := os.ReadDir(base)
if err != nil {
log.Fatal(err)
}
log.Println(len(files))

for _, file := range files {

log.Println(file.Name())
if slices.Contains([]string{"seed.go", ".env.docker"}, file.Name()) {
continue
}
cmd := exec.
Command(fmt.Sprintf("%s/%s", base, file.Name()))
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Println("out:", outb.String(), "err:", errb.String())

}
}
13 changes: 11 additions & 2 deletions cmd/seeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import (
"log"
"os"
"os/exec"
"strings"

"golang.org/x/exp/slices"
)

func stripFileExtension(fileName string) string {
s := strings.TrimRight(fileName, ".go")
return s
}

func main() {
err := config.LoadEnv()
if err != nil {
Expand All @@ -26,18 +32,21 @@ func main() {
}

for _, file := range files {
if slices.Contains([]string{"main.go", "utls"}, file.Name()) {
if slices.Contains([]string{"main.go", "seed", "output", "exec", "build", "utls"}, file.Name()) {
continue
}

filepath := base + "/" + file.Name()
// outputPath := base + "/output"
files, err := os.ReadDir(filepath)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(filepath + "/" + file.Name())
cmd := exec.Command("go", "run", filepath+"/"+file.Name())
cmd := exec.
Command("go", "build", "-o",
fmt.Sprintf("./cmd/seeder/exec/build/%s", stripFileExtension(file.Name())), filepath+"/"+file.Name())
data, err := cmd.CombinedOutput()
if err != nil {
zaplog.Logger.Error(string(data), err)
Expand Down
7 changes: 6 additions & 1 deletion cmd/seeder/utls/utls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utls

import (
"fmt"
"go-template/internal/config"
"go-template/internal/mysql"
"go-template/pkg/utl/zaplog"
"log"
Expand All @@ -10,6 +11,10 @@ import (

// SeedData ...
func SeedData(tableName string, rawQuery string) error {
err := config.LoadEnv()
if err != nil {
fmt.Print(err)
}
db, err := mysql.Connect()

if err != nil {
Expand All @@ -23,7 +28,7 @@ func SeedData(tableName string, rawQuery string) error {
for _, v := range queries[0 : len(queries)-1] {
_, err := db.Exec(v)
if err != nil {
zaplog.Logger.Error("error while executing seed script for ", tableName, err)
zaplog.Logger.Error("error while executing seed script for", tableName, err)
log.Fatal(err)

return err
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ services:
condition: service_completed_successfully
ports:
- ${SERVER_PORT}:${SERVER_PORT}
environment:
ENVIRONMENT_NAME: ${ENVIRONMENT_NAME}


Loading

0 comments on commit 507607d

Please sign in to comment.