generated from inc4/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a9a67a3
Showing
15 changed files
with
264 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Build | ||
|
||
env: | ||
go-version: "1.19" | ||
|
||
on: push | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: ${{ env.go-version }} | ||
|
||
- name: Build | ||
run: make | ||
|
||
- name: Release | ||
uses: softprops/action-gh-release@v1 | ||
if: startsWith(github.ref, 'refs/tags/') | ||
with: | ||
files: bin/example |
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,30 @@ | ||
name: Checks | ||
|
||
env: | ||
go-version: "1.19" | ||
|
||
on: push | ||
|
||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: ${{ env.go-version }} | ||
|
||
- uses: golangci/golangci-lint-action@v3 | ||
|
||
tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: ${{ env.go-version }} | ||
|
||
- name: Run tests | ||
run: make test |
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,35 @@ | ||
name: Publish docker image | ||
|
||
on: | ||
push: | ||
branches: main | ||
tags: v* | ||
|
||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
packages: write | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: docker/login-action@v2 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- uses: docker/metadata-action@v4 | ||
id: meta | ||
with: | ||
images: ghcr.io/inc4/go-template | ||
|
||
- uses: docker/build-push-action@v3 | ||
with: | ||
push: true | ||
build-args: | | ||
GH_TOKEN=${{ secrets.GH_TOKEN }} | ||
tags: ${{ steps.meta.outputs.tags }} | ||
labels: ${{ steps.meta.outputs.labels }} |
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,3 @@ | ||
/bin | ||
|
||
.DS_Store |
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,22 @@ | ||
linters: | ||
enable: | ||
- bodyclose | ||
- depguard | ||
- dupl | ||
- goconst | ||
- gocritic | ||
- gofmt | ||
- goimports | ||
- misspell | ||
- nakedret | ||
- prealloc | ||
- revive | ||
- unconvert | ||
- whitespace | ||
linters-settings: | ||
depguard: | ||
rules: | ||
main: | ||
allow: | ||
- $gostd | ||
- github.com/inc4/go-template |
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 @@ | ||
FROM golang:1.19 as build | ||
|
||
WORKDIR /app | ||
COPY go.mod . | ||
COPY go.sum . | ||
COPY Makefile . | ||
RUN go mod download | ||
|
||
COPY . . | ||
RUN make | ||
|
||
FROM alpine:3.16 | ||
COPY --from=build /app/bin/example /usr/bin/ | ||
|
||
EXPOSE 8080 | ||
|
||
CMD ["/usr/bin/example"] |
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 @@ | ||
BINDIR := bin | ||
SRC := $(shell find . -type f -name '*.go' -print) go.mod go.sum | ||
GOFLAGS := -trimpath | ||
|
||
.PHONY: all build clean lint test | ||
|
||
all: build | ||
|
||
# TODO Use this if cmd/main.go used | ||
build: $(SRC) | ||
go build $(GOFLAGS) -o $(BINDIR)/example ./cmd | ||
|
||
# TODO Use this if cmd/<names> used | ||
#build: $(SRC) | ||
# go build $(GOFLAGS) -o $(BINDIR)/ ./cmd/... | ||
|
||
clean: | ||
@rm -rf '$(BINDIR)' | ||
|
||
lint: | ||
golangci-lint run | ||
|
||
test: | ||
go test ./... |
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,30 @@ | ||
# go-template | ||
|
||
[](https://github.com/inc4/go-template/actions?query=workflow%3A%22Checks%22) | ||
|
||
This template can be used when creating a new Golang repository. Choose it when | ||
you create a repository in GitHub web interface. Or use `--template` flag when | ||
staring `gh repo create` command. | ||
|
||
Directory structure is inspired by https://github.com/golang-standards/project-layout. | ||
|
||
## Getting started | ||
|
||
- Review all files if you haven't done so before. | ||
- Remove examples and all READMEs (this one needs to be rewritten). | ||
- Change 'example' and 'go-template' names everywhere, specifically in | ||
workflows. | ||
|
||
## Where to put the code | ||
|
||
`/cmd` should not contain a lot of code, most of the code is in packages. | ||
|
||
https://github.com/golang-standards/project-layout propose to have the code | ||
in `/pkg` and `/internal`. But it's ok to have it in `/yourpkg` if you have few | ||
packages. Do not name packages like `pkg/api-db`, do this `pkg/api/db` instead. | ||
|
||
`/pkg` is for packages that can be used externally. Unless you have a big | ||
project use `/pkg` for all packages. | ||
|
||
`/internal` is for internal packages, not intended for external usage, but | ||
use it if you really need to fine tune what can be imported from outside. |
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,7 @@ | ||
# `/cmd` | ||
|
||
This directory contains a code for the executable. If the project has only | ||
one executable you can use `/cmd/main.go`. Otherwise, it is necessary to create | ||
`/cmd/<bin>/<bin>.go`. Where "bin" is the name of the executable. | ||
|
||
Don't put a lot of code here, use packages from `/pkg` or `/internal`. |
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,4 @@ | ||
package main | ||
|
||
func main() { | ||
} |
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,22 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/inc4/go-template/pkg/api" | ||
) | ||
|
||
func main() { | ||
// Basic command-line flag parsing | ||
port := flag.Int("port", 8080, "example message for port number") | ||
exampleStr := flag.String("example", "foo", "help message") | ||
flag.Parse() | ||
|
||
fmt.Println("command-line:", *port, *exampleStr) | ||
|
||
http.HandleFunc("/hello", api.HandleHello) | ||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) | ||
} |
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,3 @@ | ||
module github.com/inc4/go-template | ||
|
||
go 1.19 |
Empty file.
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 api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func HandleHello(res http.ResponseWriter, req *http.Request) { | ||
var replyType string | ||
|
||
reply, ok := req.URL.Query()["reply"] | ||
if ok && len(reply) > 0 { | ||
replyType = reply[0] | ||
} | ||
|
||
switch replyType { | ||
case "not-found": | ||
http.NotFound(res, req) | ||
case "err": | ||
http.Error(res, "error", 500) | ||
default: | ||
fmt.Fprintf(res, "Hello, %v", req.RemoteAddr) | ||
} | ||
} |
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,18 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestHello(t *testing.T) { | ||
req, _ := http.NewRequest(http.MethodGet, "/hello", nil) | ||
req.RemoteAddr = "1.2.3.4" | ||
res := httptest.NewRecorder() | ||
HandleHello(res, req) | ||
|
||
if res.Body.String() != "Hello, 1.2.3.4" { | ||
t.Error("Wrong /hello response") | ||
} | ||
} |