-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create controller layer and inject dependencies (#7)
* create controller layer * create codeowners
- Loading branch information
1 parent
179be30
commit eac9396
Showing
15 changed files
with
156 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
SUPER_SECRET_KEY= | ||
STARTER_SUPER_SECRET_KEY= |
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* @tobyscott25 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: CI checks | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build: | ||
name: Check out code | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.22.5 | ||
- run: go mod download | ||
# - name: Run tests | ||
# run: go test ./... | ||
# - name: Lint | ||
# run: go vet ./... | ||
- run: go build ./... | ||
# - name: Run | ||
# run: ./quick-links |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: "Stale PRs" | ||
|
||
on: | ||
schedule: | ||
- cron: "30 1 * * *" | ||
|
||
jobs: | ||
stale: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/stale@v4 | ||
with: | ||
stale-pr-message: "This PR is stale because it has been open 30 days with no activity. Remove `pr: stale` label or comment or this will be closed in 10 days." | ||
close-pr-message: "This PR was closed because it has been stalled for 10 days with no activity." | ||
stale-pr-label: "pr: stale" | ||
days-before-pr-stale: 30 | ||
days-before-pr-close: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ tmp | |
|
||
# MacOS | ||
.DS_Store | ||
|
||
# Compiled binary | ||
gin-air-docker-boilerplate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
version: '3.9' | ||
services: | ||
api: | ||
container_name: gin-air-docker-boilerplate | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"fmt" | ||
"log" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/tobyscott25/gin-air-docker-boilerplate/src/config" | ||
"github.com/tobyscott25/gin-air-docker-boilerplate/src/controller" | ||
"github.com/tobyscott25/gin-air-docker-boilerplate/src/di" | ||
) | ||
|
||
func main() { | ||
router := gin.Default() | ||
router.GET("/", healthCheckHandler) | ||
router.Run(":8080") | ||
} | ||
|
||
func healthCheckHandler(c *gin.Context) { | ||
var superSecretKey string = os.Getenv("SUPER_SECRET_KEY") | ||
config := config.Load() | ||
|
||
if superSecretKey == "" { | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": "Missing environment variables", | ||
}) | ||
return | ||
dependencies, err := di.InitDependencies(config) | ||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"healthy": true, | ||
"secret": superSecretKey, | ||
}) | ||
router := gin.Default() | ||
|
||
controller.InitialiseAppRouter(router, dependencies) | ||
|
||
portStr := strconv.Itoa(int(config.Port)) | ||
router.Run(fmt.Sprintf(":%s", portStr)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/kelseyhightower/envconfig" | ||
) | ||
|
||
type Config struct { | ||
Port int16 `default:"8080" split_words:"true"` | ||
SuperSecretKey string `default:"supersecret" split_words:"true"` | ||
} | ||
|
||
func Load() *Config { | ||
|
||
c := &Config{} | ||
err := envconfig.Process("STARTER", c) | ||
|
||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/tobyscott25/gin-air-docker-boilerplate/src/di" | ||
) | ||
|
||
// Swagger spec docs here | ||
|
||
func HandleHealthCheck(dependencies *di.Dependencies) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
var superSecretKey string = dependencies.Config.SuperSecretKey | ||
|
||
if superSecretKey == "" { | ||
c.AbortWithStatus(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"healthy": true, | ||
"secret": superSecretKey, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/tobyscott25/gin-air-docker-boilerplate/src/di" | ||
) | ||
|
||
func InitialiseAppRouter(router *gin.Engine, deps *di.Dependencies) { | ||
v1 := router.Group("/v1") | ||
v1.Use(gin.Recovery()) | ||
|
||
v1.GET("/health", HandleHealthCheck(deps)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package di | ||
|
||
import "github.com/tobyscott25/gin-air-docker-boilerplate/src/config" | ||
|
||
type Dependencies struct { | ||
Config *config.Config | ||
} | ||
|
||
func InitDependencies(config *config.Config) (*Dependencies, error) { | ||
return &Dependencies{ | ||
Config: config, | ||
}, nil | ||
} |