Skip to content

Commit c0d1f4d

Browse files
authored
Initial commit
0 parents  commit c0d1f4d

Some content is hidden

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

49 files changed

+2447
-0
lines changed

.dockerignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor
2+
bin
3+
Dockerfile
4+
.dockerignore

.env.example

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
API_NAME=gomora
2+
API_ENV=local
3+
API_URL_GRPC=http://localhost
4+
API_URL_GRPC_PORT=9090
5+
API_URL_REST=http://localhost
6+
API_URL_REST_PORT=8000
7+
API_VERSION=v1.4.0
8+
9+
DB_HOST=localhost
10+
DB_PORT=3306
11+
DB_DATABASE=
12+
DB_USERNAME=
13+
DB_PASSWORD=
14+
15+
OPENAPI_DOCS_PASSWORD=

.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.dll
4+
*.so
5+
*.dylib
6+
7+
# Test binary, build with `go test -c`
8+
*.test
9+
10+
# Output of the go coverage tool, specifically when used with LiteIDE
11+
*.out
12+
13+
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
14+
.glide/
15+
16+
#directories
17+
.idea/
18+
.vscode/
19+
vendor/
20+
21+
#single files
22+
.env
23+
24+
#executable files
25+
bin

Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM golang:1.21
2+
3+
ENV GO111MODULE=on
4+
5+
WORKDIR /app/build
6+
COPY go.mod .
7+
COPY go.sum .
8+
9+
RUN go mod download
10+
COPY . .
11+
12+
CMD ["make", "run"]

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Karl Anthony B. Baluyot
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.

Makefile

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
include .env
2+
3+
# set default shell
4+
SHELL = bash -e -o pipefail
5+
6+
default: run-dev
7+
8+
.PHONY: install
9+
install:
10+
go mod tidy
11+
go mod vendor
12+
13+
.PHONY: lint
14+
lint:
15+
golangci-lint run
16+
17+
.PHONY: build
18+
build:
19+
mkdir -p bin
20+
go build -o bin/gomora \
21+
cmd/main.go
22+
23+
.PHONY: build-dev
24+
build-dev:
25+
mkdir -p bin
26+
go build -race -o bin/gomora \
27+
cmd/main.go
28+
29+
.PHONY: test
30+
test:
31+
go test -race -v -p 1 ./...
32+
33+
.PHONY: run
34+
run: build
35+
./bin/gomora
36+
37+
.PHONY: run-dev
38+
run-dev: build-dev
39+
./bin/gomora
40+
41+
.PHONY: up
42+
up:
43+
docker compose down
44+
docker compose up -d --build
45+
46+
.PHONY: schema
47+
schema:
48+
mkdir -p infrastructures/database/mysql/migrations
49+
migrate create -ext sql -dir infrastructures/database/mysql/migrations -seq ${NAME}
50+
51+
.PHONY: migrate-up
52+
migrate-up:
53+
migrate -path infrastructures/database/mysql/migrations -database "mysql://${DB_USERNAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_DATABASE}" -verbose up ${STEPS}
54+
55+
.PHONY: migrate-down
56+
migrate-down:
57+
migrate -path infrastructures/database/mysql/migrations -database "mysql://${DB_USERNAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_DATABASE}" -verbose down ${STEPS}
58+
59+
.PHONY: migrate-version
60+
migrate-version:
61+
migrate -path infrastructures/database/mysql/migrations -database "mysql://${DB_USERNAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_DATABASE}" version
62+
63+
.PHONY: migrate-force
64+
migrate-force:
65+
migrate -path infrastructures/database/mysql/migrations -database "mysql://${DB_USERNAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_DATABASE}" force ${STEPS}
66+
67+
proto-record:
68+
protoc --go_out=plugins=grpc:. --go_opt=paths=source_relative module/record/interfaces/http/grpc/pb/record.proto

README.md

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Gomora
2+
3+
A progressive framework-agnostic API template following CLEAN architecture and SOLID principles. DDD inspired :)
4+
5+
## Introduction
6+
7+
Gomora provides the example for a module-based gRPC and REST server suitable for building progressive APIs (from monolith to distributed microservices).
8+
9+
<img width="1416" alt="Screen Shot 2024-10-07 at 9 43 31 AM" src="https://github.com/user-attachments/assets/736b9813-f086-4fd4-aeb3-98e08df51e7e">
10+
11+
<img width="1416" alt="Screen Shot 2024-10-07 at 9 41 10 AM" src="https://github.com/user-attachments/assets/0225919b-53d1-4900-9b9e-8d5d5f616d73">
12+
13+
<img width="1416" alt="Screen Shot 2024-10-07 at 9 44 17 AM" src="https://github.com/user-attachments/assets/a5871a22-be66-4236-8635-6166624249c1">
14+
15+
<img width="1416" alt="Screen Shot 2024-10-07 at 9 46 05 AM" src="https://github.com/user-attachments/assets/578de61d-73cb-47b9-8806-ed58a7b281a1">
16+
17+
## Local Development
18+
19+
Setup the .env file first
20+
21+
```bash
22+
cp .env.example .env
23+
```
24+
25+
To bootstrap everything, run:
26+
27+
```bash
28+
make
29+
```
30+
31+
The command above will install, build, and run the binary
32+
33+
For manual install:
34+
35+
```bash
36+
make install
37+
```
38+
39+
For lint:
40+
41+
```bash
42+
make lint
43+
```
44+
45+
Just ensure you installed golangci-lint.
46+
47+
To test:
48+
49+
```bash
50+
make test
51+
```
52+
53+
For manual build:
54+
55+
```bash
56+
make build
57+
58+
# The output for this is in bin/
59+
```
60+
61+
## Docker Build
62+
63+
To build, run:
64+
65+
```bash
66+
make run
67+
```
68+
69+
To run the container:
70+
71+
```bash
72+
make up
73+
```
74+
75+
## Database Migration
76+
77+
Gomora uses go-migrate (https://github.com/golang-migrate/migrate) to handle migration. Download and change your migrate database command accordingly.
78+
79+
To create a schema, run:
80+
81+
```bash
82+
make schema NAME=<init_schema>
83+
```
84+
85+
To migrate up, run:
86+
87+
```bash
88+
STEPS=<remove STEPS to apply all or specify step number> make migrate-up
89+
```
90+
91+
To migrate down, run:
92+
93+
```bash
94+
STEPS=<remove STEPS to apply all or specify step number> make migrate-down
95+
```
96+
97+
To check migrate version, run:
98+
99+
```bash
100+
make migrate-version
101+
```
102+
103+
To force migrate, run:
104+
105+
```bash
106+
STEPS=<specify step number> make migrate-force
107+
```
108+
109+
## License
110+
111+
[MIT](https://choosealicense.com/licenses/mit/)
112+
113+
Made with ❤️ at [Nuxify](https://nuxify.tech)

cmd/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cmd

cmd/main.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
|--------------------------------------------------------------------------
3+
| Main
4+
|--------------------------------------------------------------------------
5+
|
6+
| This is the entry point for listeners of the project.
7+
| You can create and run goroutines for event listeners below before the HTTP listener.
8+
|
9+
*/
10+
package main
11+
12+
import (
13+
"fmt"
14+
"log"
15+
"os"
16+
"strconv"
17+
18+
"github.com/joho/godotenv"
19+
20+
"gomora/interfaces/http/grpc"
21+
"gomora/interfaces/http/rest"
22+
)
23+
24+
func init() {
25+
// load our environmental variables.
26+
if err := godotenv.Load(); err != nil {
27+
panic(err)
28+
}
29+
}
30+
31+
func main() {
32+
// grpc port
33+
grpcPort, err := strconv.Atoi(os.Getenv("API_URL_GRPC_PORT"))
34+
if err != nil {
35+
log.Fatalf("[SERVER] Invalid port")
36+
}
37+
if len(fmt.Sprintf("%d", grpcPort)) == 0 {
38+
grpcPort = 9090 // default grpcPort is 9090 if not set
39+
}
40+
41+
// rest port
42+
restPort, err := strconv.Atoi(os.Getenv("API_URL_REST_PORT"))
43+
if err != nil {
44+
log.Fatalf("[SERVER] Invalid port")
45+
}
46+
if len(fmt.Sprintf("%d", restPort)) == 0 {
47+
restPort = 8000 // default grpcPort is 8000 if not set
48+
}
49+
50+
// serve rest server
51+
go rest.ChiRouter().Serve(restPort)
52+
53+
// serve grpc server
54+
grpc.GRPCServer().Serve(grpcPort)
55+
}

configs/cors/cors.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cors
2+
3+
// Config holds the CORS configurations
4+
type Config struct{}
5+
6+
// AllowCredentials return a boolean expression whether credentials are allowed
7+
func (c *Config) AllowCredentials() bool {
8+
return true
9+
}
10+
11+
// AllowedHeaders returns list of allowed headers
12+
func (c *Config) AllowedHeaders() []string {
13+
return []string{
14+
"Accept",
15+
"Authorization",
16+
"Content-Type",
17+
"X-CSRF-Token",
18+
}
19+
}
20+
21+
// AllowedOrigins returns list of allowed origins
22+
func (c *Config) AllowedOrigins() []string {
23+
return []string{"*"}
24+
}
25+
26+
// AllowedMethods returns list of allowed methods
27+
func (c *Config) AllowedMethods() []string {
28+
return []string{
29+
"GET",
30+
"POST",
31+
"PUT",
32+
"PATCH",
33+
"DELETE",
34+
"OPTIONS",
35+
}
36+
}
37+
38+
// ExposedHeaders returns list of exposed headers
39+
func (c *Config) ExposedHeaders() []string {
40+
return []string{"Link"}
41+
}
42+
43+
// MaxAge returns the maximum number of age in browser in seconds
44+
func (c *Config) MaxAge() int {
45+
return 300
46+
}

configs/hystrix/hystrix.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package hystrix
2+
3+
import (
4+
"github.com/afex/hystrix-go/hystrix"
5+
)
6+
7+
// Config handles the hystrix configurations
8+
type Config struct{}
9+
10+
// Settings returns the hystrix command config
11+
func (c Config) Settings() hystrix.CommandConfig {
12+
return hystrix.CommandConfig{
13+
Timeout: 3000,
14+
}
15+
}

0 commit comments

Comments
 (0)