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

fix: setup for the project #172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
FROM golang:1.22-alpine3.19 AS builder
RUN apk add build-base
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enforce version pinning in apk add commands to enhance security.

Pinning versions of packages in apk add commands is crucial for ensuring the reproducibility and security of Docker builds.

- RUN apk add build-base
- RUN apk add --no-cache libc6-compat 
- RUN apk add --no-cache --upgrade bash
+ RUN apk add build-base=<version>
+ RUN apk add --no-cache libc6-compat=<version> 
+ RUN apk add --no-cache --upgrade bash=<version>

Also applies to: 29-30

Tools
Hadolint

[info] 2-2: Use the --no-cache switch to avoid the need to use --update and remove /var/cache/apk/* when done installing packages (DL3019)


[warning] 2-2: Pin versions in apk add. Instead of apk add <package> use apk add <package>=<version> (DL3018)


RUN mkdir /app
ADD . /app

WORKDIR /app
# copy the go.mod and go.sum and download the dependency first
# before copying the project
COPY go.mod /app
COPY go.sum /app
RUN go mod download
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize Docker build layers by merging RUN commands.

The consecutive RUN instructions can be consolidated to reduce the number of layers in the Docker image, which can lead to a smaller image size and potentially faster builds.

- RUN GOARCH=amd64 \
-     GOOS=linux \
-     CGO_ENABLED=0 \
-     go mod vendor
- 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 GOARCH=amd64 GOOS=linux CGO_ENABLED=0 go mod vendor && \
+    go run ./cmd/seeder/main.go && \
+    go build -o ./output/server ./cmd/server/main.go && \
+    go build -o ./output/migrations ./cmd/migrations/main.go && \
+    go build -o ./output/seeder ./cmd/seeder/exec/seed.go

Committable suggestion was skipped due to low confidence.


# NOW COPY the whole root project
COPY . /app

ARG ENVIRONMENT_NAME
ENV ENVIRONMENT_NAME=$ENVIRONMENT_NAME
RUN GOARCH=amd64 \
Expand Down
105 changes: 48 additions & 57 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ An enterprise go template application showcasing - Testing strategies, middlewar
</p>
___


<p>
<h4>
Expert teams of digital product strategists, developers, and designers.
Expand All @@ -28,23 +27,17 @@ An enterprise go template application showcasing - Testing strategies, middlewar
<img src="https://uploads-ssl.webflow.com/5ee36ce1473112550f1e1739/5f6ae88bb1958c3253756c39_button_follow_on_github.svg" width="168" height="34">
</a>
</div>

___
---

<span>We’re always looking for people who value their work, so come and join
us. <a href="https://www.wednesday.is/hiring">We are hiring!</a></span>


</div>

---

<br/>





The Go Template is a template/starter go project.

## Out of the box support for
Expand Down Expand Up @@ -72,24 +65,24 @@ to configure the following:

3. Install the sqlboiler, sql-migrate and gqlgen using

```bash
go get -v github.com/rubenv/sql-migrate/... \
github.com/volatiletech/sqlboiler \
github.com/99designs/gqlgen
```
go get -v github.com/rubenv/sql-migrate/... \
github.com/volatiletech/sqlboiler \
github.com/99designs/gqlgen
```

For Go 1.16 or above, you need to install sqlboiler using

```
go install github.com/volatiletech/sqlboiler/v4@latest
go install github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-psql@latest
```
```bash
go install github.com/volatiletech/sqlboiler/v4@latest
go install github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-psql@latest
```

For Go 1.18 and above install the sql-migrate using

```
go install github.com/rubenv/sql-migrate/...@latest
```
```bash
go install github.com/rubenv/sql-migrate/...@latest
```

4. To run all the migrations using the script setup-local.sh as follows `make setup-local`.

Expand All @@ -101,20 +94,20 @@ For Go 1.18 and above install the sql-migrate using
go run cmd/server/main.go
```

**NOTE:** Please do not delete ```.env.base``` file of the project and rebuild the using docker-compose everytime you
**NOTE:** Please do not delete `.env.base` file of the project and rebuild the using docker-compose everytime you
make changes to it

# Setting up database (postgres)

- Requirement [postgresql](https://www.postgresql.org/)

Steps to set up database with ```username``` and ```role``` using terminal
Steps to set up database with `username` and `role` using terminal

- Enter postgres terminal ```psql postgres```
- Create new database ```CREATE DATABASE go_template;```
- Create a new role with password ```CREATE ROLE go_template_role WITH LOGIN PASSWORD 'go_template_role456';```
- Enter postgres terminal `psql postgres`
- Create new database `CREATE DATABASE go_template;`
- Create a new role with password `CREATE ROLE go_template_role WITH LOGIN PASSWORD 'go_template_role456';`

**NOTE:** Replace these credentials in ```.env``` file of the project
**NOTE:** Replace these credentials in `.env` file of the project

# Using Docker

Expand All @@ -132,27 +125,27 @@ Set up signoz locally by following the steps [here](https://signoz.io/docs/insta

# Running migrations

Migrations are present in ```internal/migrations``` package. Run below command to run all migrations at once:
Migrations are present in `internal/migrations` package. Run below command to run all migrations at once:

```
```bash
sql-migrate up -env postgres
```

To drop migration use following

```
```bash
sql-migrate down -env postgres -limit=0
```

To check status of migration use following

```
```bash
sql-migrate new -env postgres <name>
```

To add new migration use following, it creates a new empty migration template with pattern `<current time>-<name>.sql`

```
```bash
sql-migrate new -env postgres <name>
```

Expand Down Expand Up @@ -203,7 +196,7 @@ go-template/
│ └──line-formatter.sh # auto format to adhere to the lll.line-length criteria
└──schema/ # this directory will have all the .graphql files which make the graphql api
└──.env.local # a sample .env file for reference
└──.env.base # a base .env file should be included in all environments
└──.env.base # a base .env file should be included in all environments
└──.pre-commit-config.yaml # config to run pre-commit utility
└──docker-compose.*.yml # docker-compose file corresponding to the state of project (local, prod, test)
└──docker-compose.yml # docker-compose file which serves as a base to other docker-compose files
Expand All @@ -220,16 +213,17 @@ go-template/

generate your database models

```
```bash
sqlboiler psql --no-hooks
```

# Seed your Database

For seeding Your database models use

```
go run cmd/seeder/exec/seed.go
```bash
go run cmd/seeder/main.go ## to build the execs for seeding
go run cmd/seeder/exec/seed.go ## to seed
```

Note: We have Seeder directory because we are using it while building docker image for application
Expand All @@ -238,33 +232,33 @@ Note: We have Seeder directory because we are using it while building docker ima

generate the graphql models from the database schema

```
```bash
gqlgen generate
```

## API (for graphQL to operate)

- Graphql endpoint ```POST``` request ```/graphql```
- Graphql endpoint `POST` request `/graphql`

- Playground endpoint for schema ```/playground```
- Playground endpoint for schema `/playground`

Take a look at the following file

- [pkg/api/api.go](pkg/api/api.go)
- [pkg/api/api.go](pkg/api/api.go)

## Schema

- Schema can generated or altered manually

Take a look at the following folder

- [schema](./schema/)
- [schema](./schema/)

## Resolver

- Queries and mutation are autogenerated using gqlgen and are to be extended. Take a look at the following files

- [resolver](./resolver)
- [resolver](./resolver)

## Infrastructure

Expand All @@ -279,75 +273,72 @@ gqlgen generate

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

```
```bash
make setup-ecs name=gotemplate env=dev
```

Please change the ENV_INJECTION variable as true in .env.base file to true, so it will not try to find a local .env file

Also add the environment variables to the task,add this block of yml code in ${service name}/manifest.yaml:

```

variables:
ENVIRONMENT_NAME: develop
```yaml
variables:
ENVIRONMENT_NAME: develop

#to inject our .env file from aws s3 inside the container
#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'

type: "s3"
value: "arn:aws:s3:::gotemplate-dev-bucket/develop/.env"
```

Make sure that the manifest.yml has http.path: '/'

```
```yaml
http:
# Requests to this path will be forwarded to your service.
# To match all requests you can use the "/" path.
path: '/'
path: "/"
# You can specify a custom health check path. The default is "/".
# healthcheck: '/'

```

Also make sure the execution role has an appropriate policy attached to it so it can access our .env file inside the s3
bucket, and inject it as environment variables.

### To deploy

```
```bash
make deploy-ecs name=gotemplate env=dev
```

### Update infrastructure

```
```bash
make update-ecs name=gotemplate env=dev
```

## Testing

Get test coverage using

```
```bash
go test -cover
```

## Generate mocks

Install Mockgen Using

```
```bash
go install github.com/gleisonmv/mockgen@latest
```

Sample command to generate mocks

```
```bash
mockgen --build_flags=--mod=mod github.com/go-playground/validator FieldError
```

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module go-template

go 1.22
go 1.22.0

require (
github.com/99designs/gqlgen v0.17.24
Expand Down Expand Up @@ -29,9 +29,8 @@ require (
github.com/vektah/gqlparser/v2 v2.5.1
github.com/volatiletech/null/v8 v8.1.2
github.com/volatiletech/randomize v0.0.1
github.com/volatiletech/sqlboiler v3.7.1+incompatible
github.com/volatiletech/sqlboiler/v4 v4.11.0
github.com/volatiletech/strmangle v0.0.4
github.com/volatiletech/strmangle v0.0.6
go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.33.0
go.opentelemetry.io/otel v1.8.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.8.0
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,12 @@ github.com/volatiletech/null/v8 v8.1.2 h1:kiTiX1PpwvuugKwfvUNX/SU/5A2KGZMXfGD0DU
github.com/volatiletech/null/v8 v8.1.2/go.mod h1:98DbwNoKEpRrYtGjWFctievIfm4n4MxG0A6EBUcoS5g=
github.com/volatiletech/randomize v0.0.1 h1:eE5yajattWqTB2/eN8df4dw+8jwAzBtbdo5sbWC4nMk=
github.com/volatiletech/randomize v0.0.1/go.mod h1:GN3U0QYqfZ9FOJ67bzax1cqZ5q2xuj2mXrXBjWaRTlY=
github.com/volatiletech/sqlboiler v3.7.1+incompatible h1:dm9/NjDskQVwAarmpeZ2UqLn1NKE8M3WHSHBS4jw2x8=
github.com/volatiletech/sqlboiler v3.7.1+incompatible/go.mod h1:jLfDkkHWPbS2cWRLkyC20vQWaIQsASEY7gM7zSo11Yw=
github.com/volatiletech/sqlboiler/v4 v4.11.0 h1:jItTUGIXfCfFiNEGIBZZj4rFMO/gXhjqX03sJ5LiDk8=
github.com/volatiletech/sqlboiler/v4 v4.11.0/go.mod h1:AAaQj77uX6nyU+Q5q6OcVCFFEs/gs+qsthM18/NVemo=
github.com/volatiletech/strmangle v0.0.1/go.mod h1:F6RA6IkB5vq0yTG4GQ0UsbbRcl3ni9P76i+JrTBKFFg=
github.com/volatiletech/strmangle v0.0.4 h1:CxrEPhobZL/PCZOTDSH1aq7s4Kv76hQpRoTVVlUOim4=
github.com/volatiletech/strmangle v0.0.4/go.mod h1:ycDvbDkjDvhC0NUU8w3fWwl5JEMTV56vTKXzR3GeR+0=
github.com/volatiletech/strmangle v0.0.6 h1:AdOYE3B2ygRDq4rXDij/MMwq6KVK/pWAYxpC7CLrkKQ=
github.com/volatiletech/strmangle v0.0.6/go.mod h1:ycDvbDkjDvhC0NUU8w3fWwl5JEMTV56vTKXzR3GeR+0=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
Expand Down
1 change: 1 addition & 0 deletions scripts/setup-pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go install golang.org/x/lint/golint@latest
go install github.com/BurntSushi/toml/cmd/tomlv@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/segmentio/golines@latest
go install github.com/masahiro331/[email protected]

touch .git/hooks/commit-msg
echo "go-commitlinter" >> .git/hooks/commit-msg
Expand Down
2 changes: 1 addition & 1 deletion tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import (
_ "github.com/99designs/gqlgen/graphql/introspection"
_ "github.com/masahiro331/go-commitlinter"
_ "github.com/rubenv/sql-migrate"
_ "github.com/volatiletech/sqlboiler"
_ "github.com/volatiletech/sqlboiler/v4"
)