Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SigismundSchlomo authored Sep 4, 2023
0 parents commit a9a67a3
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/build.yaml
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
30 changes: 30 additions & 0 deletions .github/workflows/checks.yaml
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
35 changes: 35 additions & 0 deletions .github/workflows/docker.yaml
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 }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/bin

.DS_Store
22 changes: 22 additions & 0 deletions .golangci.yml
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
17 changes: 17 additions & 0 deletions Dockerfile
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"]
24 changes: 24 additions & 0 deletions Makefile
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 ./...
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# go-template

[![Test status](https://github.com/inc4/go-template/workflows/Checks/badge.svg)](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.
7 changes: 7 additions & 0 deletions cmd/README.md
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`.
4 changes: 4 additions & 0 deletions cmd/example/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

func main() {
}
22 changes: 22 additions & 0 deletions cmd/main.go
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))
}
3 changes: 3 additions & 0 deletions go.mod
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 added go.sum
Empty file.
24 changes: 24 additions & 0 deletions pkg/api/api.go
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)
}
}
18 changes: 18 additions & 0 deletions pkg/api/api_test.go
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")
}
}

0 comments on commit a9a67a3

Please sign in to comment.