Skip to content

Commit

Permalink
Create controller layer and inject dependencies (#7)
Browse files Browse the repository at this point in the history
* create controller layer

* create codeowners
  • Loading branch information
tobyscott25 authored Sep 5, 2024
1 parent 179be30 commit eac9396
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SUPER_SECRET_KEY=
STARTER_SUPER_SECRET_KEY=
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @tobyscott25
28 changes: 28 additions & 0 deletions .github/workflows/ci.yaml
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
17 changes: 17 additions & 0 deletions .github/workflows/stale-pr.yaml
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ tmp

# MacOS
.DS_Store

# Compiled binary
gin-air-docker-boilerplate
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Toby Scott
Copyright (c) 2024 Toby Scott

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Project boilerplate: Go + Gin + Docker + Air

A containerised [Gin](https://github.com/gin-gonic/gin) app boilerplate, with a containerised development environment using Air and Docker Compose.

| Component | Choice |
| ----------------- | ---------------------------------------- |
| Language | [Go](https://go.dev/) |
| Framework | [Gin](https://github.com/gin-gonic/gin) |
| Hot Reloading | [Air](https://github.com/cosmtrek/air) |
| Containerisation | [Docker](https://www.docker.com/) |
| Component | Choice |
| ---------------- | --------------------------------------- |
| Language | [Go](https://go.dev/) |
| Framework | [Gin](https://github.com/gin-gonic/gin) |
| Hot Reloading | [Air](https://github.com/cosmtrek/air) |
| Containerisation | [Docker](https://www.docker.com/) |

## Development

Expand All @@ -16,7 +17,6 @@ Install the dependancies:
```bash
go mod download
go mod vendor
go mod verify
```

Expand Down
1 change: 0 additions & 1 deletion docker-compose.yaml
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
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/tobyscott25/gin-air-docker-boilerplate

go 1.19

require github.com/gin-gonic/gin v1.9.1
require (
github.com/gin-gonic/gin v1.9.1
github.com/kelseyhightower/envconfig v1.4.0
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
Expand Down
34 changes: 16 additions & 18 deletions main.go
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))
}
24 changes: 24 additions & 0 deletions src/config/config.go
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
}
26 changes: 26 additions & 0 deletions src/controller/healthCheckController.go
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,
})
}
}
13 changes: 13 additions & 0 deletions src/controller/routes.go
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))
}
13 changes: 13 additions & 0 deletions src/di/dependencies.go
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
}

0 comments on commit eac9396

Please sign in to comment.