Skip to content

Commit aa128a3

Browse files
committed
🔄 Synced file(s) with devstream-io/devstream
1 parent 03c87ce commit aa128a3

11 files changed

+255
-1
lines changed

‎.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/

‎Dockerfile.tpl

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM golang:alpine AS build-env
2+
WORKDIR $GOPATH/src/github.com/[[.Repo.Owner]]/[[.Repo.Name]]
3+
COPY . .
4+
RUN apk add git
5+
RUN go get ./... && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app cmd/[[.AppName]]/main.go
6+
7+
FROM alpine
8+
WORKDIR /app
9+
COPY --from=build-env /go/src/github.com/[[.Repo.Owner]]/[[.Repo.Name]]/app /app/
10+
CMD ["./app"]
11+
USER 1000
12+
EXPOSE 8080/tcp

‎Makefile.tpl

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
SHELL := /bin/bash
2+
BASEDIR = $(shell pwd)
3+
4+
.PHONY: help
5+
help:
6+
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
7+
8+
.PHONY: install
9+
install: ## Install dependencies
10+
@go mod download
11+
@go mod vendor
12+
13+
.PHONY: dev
14+
dev: ## Run with Dev
15+
@go run cmd/[[.AppName]]/main.go
16+
17+
.PHONY: build
18+
build: ## Build todomvc
19+
@go build -o build/[[.AppName]] cmd/[[.AppName]]/main.go
20+
21+
clean: ### Remove build dir
22+
@rm -rf build

‎README.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
1-
# dtm-repo-scaffolding-golang-gin
1+
# dtm-repo-scaffolding-golang-gin
2+
3+
This repo contains templates used by DevStream plugin "repo-scaffolding" (thereafter: the plugin).
4+
5+
This repo isn't intended to be used directly without DevStream. It should only be consumed by the plugin automatically.
6+
7+
The plugin (together with this repo of templates) can create a repo in GitHub and set up the project layout and initialize the reop with necessary files that are typical for a Go web app. The followings can be created automatically:
8+
9+
- a Go web app example (source code from [here](https://go.dev/doc/tutorial/web-service-gin)) with the [Gin Web Framework](https://github.com/gin-gonic/gin)
10+
- directory structure, following [Standard Go Project Layout](https://github.com/golang-standards/project-layout) best practice
11+
- `.gitignore`, generated by [Toptal | gitignore.io](https://www.toptal.com/developers/gitignore/api/go,vim,macos,visualstudiocode) with minimum changes
12+
- Makefile, with install/dev/build/clean
13+
- Dockerfile, with multistage build
14+
- a simplified Helm chart with Deployment and Service
15+
16+
## Usage
17+
18+
- Render all files using go template whose name end with `.tpl` suffix.
19+
- Files whose name don't end with `.tpl` extension don't need to be rendered.
20+
- subdirectory "helm/**_app_name_**" (the **_app_name_** part) should be rendered with `AppName`
21+
- subdicrectory "cmd/**_app_name_**" (the **_app_name_** part) should be rendered with `AppName`
22+
23+
Example of required parameters to render these templates:
24+
25+
```yaml
26+
AppName: my-hello-world
27+
Repo:
28+
Owner: ironcore864
29+
Name: my-hello-world
30+
ImageRepo: ironcore864/my-hello-world # dockerhub
31+
```
32+
33+
## Where does this repo come from?
34+
35+
`dtm-repo-scaffolding-golang-gin` is synced from https://github.com/devstream-io/devstream/blob/main/staging/dtm-repo-scaffolding-golang-gin.
36+
Code changes are made in that location, merged into `devstream-io/devstream` and later synced here.

‎README.md.tpl

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# [[.AppName]]
2+
3+
This is a repo for app [[.AppName]]; bootstrapped by DevStream.
4+
5+
By default, the automatically generated scaffolding contains:
6+
7+
- a piece of sample go web app code using the [Gin Web Framework](https://github.com/gin-gonic/gin)
8+
- .gitignore
9+
- Makefile
10+
- Dockerfile
11+
- Helm chart

‎cmd/_app_name_/main.go.tpl

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
6+
"github.com/[[.Repo.Owner]]/[[.Repo.Name]]/internal/pkg/album"
7+
)
8+
9+
func main() {
10+
router := gin.Default()
11+
12+
router.GET("/albums", album.GetAlbums)
13+
router.GET("/albums/:id", album.GetAlbumByID)
14+
router.POST("/albums", album.PostAlbums)
15+
16+
router.Run("localhost:8080")
17+
}

‎go.mod.tpl

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module github.com/[[.Repo.Owner]]/[[.Repo.Name]]
2+
3+
go 1.17
4+
5+
require github.com/gin-gonic/gin v1.7.7
6+
7+
require (
8+
github.com/gin-contrib/sse v0.1.0 // indirect
9+
github.com/go-playground/locales v0.13.0 // indirect
10+
github.com/go-playground/universal-translator v0.17.0 // indirect
11+
github.com/go-playground/validator/v10 v10.4.1 // indirect
12+
github.com/golang/protobuf v1.3.3 // indirect
13+
github.com/json-iterator/go v1.1.9 // indirect
14+
github.com/leodido/go-urn v1.2.0 // indirect
15+
github.com/mattn/go-isatty v0.0.12 // indirect
16+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
17+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
18+
github.com/ugorji/go/codec v1.1.7 // indirect
19+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
20+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 // indirect
21+
gopkg.in/yaml.v2 v2.2.8 // indirect
22+
)

‎go.sum.tpl

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
5+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
6+
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
7+
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
8+
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
9+
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
10+
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
11+
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
12+
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
13+
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
14+
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
15+
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
16+
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
17+
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
18+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
19+
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
20+
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
21+
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
22+
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
23+
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
24+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
25+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
26+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
27+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
28+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
29+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
30+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
31+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
32+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
33+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
34+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
35+
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
36+
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
37+
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
38+
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
39+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
40+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
41+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
42+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
43+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
44+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
45+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
46+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
47+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
48+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
49+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
50+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
51+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
52+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
53+
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
54+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

‎internal/pkg/album/album.go.tpl

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package album
2+
3+
// album represents data about a record album.
4+
type album struct {
5+
ID string `json:"id"`
6+
Title string `json:"title"`
7+
Artist string `json:"artist"`
8+
Price float64 `json:"price"`
9+
}
10+
11+
// albums slice to seed record album data.
12+
var albums = []album{
13+
{ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99},
14+
{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99},
15+
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99},
16+
}

‎internal/pkg/album/get.go.tpl

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package album
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
// GetAlbums responds with the list of all albums as JSON.
10+
func GetAlbums(c *gin.Context) {
11+
c.IndentedJSON(http.StatusOK, albums)
12+
}
13+
14+
// GetAlbumByID locates the album whose ID value matches the id
15+
// parameter sent by the client, then returns that album as a response.
16+
func GetAlbumByID(c *gin.Context) {
17+
id := c.Param("id")
18+
19+
// Loop through the list of albums, looking for
20+
// an album whose ID value matches the parameter.
21+
for _, a := range albums {
22+
if a.ID == id {
23+
c.IndentedJSON(http.StatusOK, a)
24+
return
25+
}
26+
}
27+
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "album not found"})
28+
}

‎internal/pkg/album/post.go.tpl

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package album
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
// PostAlbums adds an album from JSON received in the request body.
10+
func PostAlbums(c *gin.Context) {
11+
var newAlbum album
12+
13+
// Call BindJSON to bind the received JSON to
14+
// newAlbum.
15+
if err := c.BindJSON(&newAlbum); err != nil {
16+
return
17+
}
18+
19+
// Add the new album to the slice.
20+
albums = append(albums, newAlbum)
21+
c.IndentedJSON(http.StatusCreated, newAlbum)
22+
}

0 commit comments

Comments
 (0)