Skip to content

Commit

Permalink
refactor: code and build updates
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz committed Apr 18, 2022
1 parent 5ab5a19 commit 4dd5a9b
Show file tree
Hide file tree
Showing 12 changed files with 675 additions and 221 deletions.
60 changes: 46 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@ jobs:
steps:
# dependencies
- name: goreleaser
run: |
curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sudo sh -s -- -b /usr/local/bin
uses: goreleaser/goreleaser-action@v2
with:
install-only: true
version: 1.7.0

- name: goreleaser info
run: goreleaser -v

- name: task
uses: arduino/setup-task@v1

- name: task info
run: task --version

- name: qemu
uses: docker/setup-qemu-action@v1
Expand All @@ -32,26 +43,43 @@ jobs:
- name: go
uses: actions/setup-go@v1
with:
go-version: 1.16
go-version: 1.18

- name: go info
run: |
go version
go env
# cache
- name: cache
uses: actions/cache@v1
with:
path: vendor
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
# cache
- name: cache-paths
id: go-cache-paths
run: |
echo "::set-output name=go-build::$(go env GOCACHE)"
echo "::set-output name=go-mod::$(go env GOMODCACHE)"
- name: cache-build
uses: actions/cache@v2
with:
path: ${{ steps.go-cache-paths.outputs.go-build }}
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }}

- name: cache-mod
uses: actions/cache@v2
with:
path: ${{ steps.go-cache-paths.outputs.go-mod }}
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}

- name: cache-task
uses: actions/cache@v2
with:
path: .task/**/*
key: ${{ runner.os }}-go-task

# vendor
- name: vendor
run: |
make vendor
task vendor
# git status
- name: git status
Expand All @@ -61,7 +89,7 @@ jobs:
- name: build
if: startsWith(github.ref, 'refs/tags/') == false
run: |
make snapshot
task snapshot
# publish
- name: publish
Expand All @@ -70,7 +98,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REF: ${{ github.ref }}
run: |
make publish
task publish
# artifacts
- name: artifact_linux
Expand Down Expand Up @@ -119,6 +147,8 @@ jobs:
platforms: linux/amd64,linux/arm64,linux/arm/v7
pull: true
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
tags: |
cloudb0x/cvm:${{ steps.releasetag.outputs.tag }}
cloudb0x/cvm:latest
Expand Down Expand Up @@ -148,6 +178,8 @@ jobs:
platforms: linux/amd64,linux/arm64,linux/arm/v7
pull: true
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
tags: |
cloudb0x/cvm:${{ steps.dockertag.outputs.replaced }}
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ config.yml

# dist folder
/dist/

# task folder
/.task/
7 changes: 6 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ builds:
flags:
- -trimpath

# MacOS Universal Binaries
universal_binaries:
-
replace: true

# Archive
archives:
-
Expand All @@ -38,7 +43,7 @@ checksum:

# Snapshot
snapshot:
name_template: "{{ .Major }}.{{ .Minor }}.{{ .Patch }}-dev+{{ .ShortCommit }}"
name_template: "{{ .Major }}.{{ .Minor }}.{{ .Patch }}-dev+{{ .Branch }}"

# Changelog
changelog:
Expand Down
59 changes: 0 additions & 59 deletions Makefile

This file was deleted.

66 changes: 66 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
version: '3'

vars:
APP: cvm
CGO_ENABLED: 0
GOOS:
sh: go env GOOS
GOARCH:
sh: go env GOARCH
DIST_PATH: dist
BUILD_PATH: "{{.DIST_PATH}}/{{.APP}}_{{.GOOS}}_{{.GOARCH}}"

env:
GIT_COMMIT:
sh: git rev-parse --short HEAD
TIMESTAMP: '{{now | unixEpoch}}'
VERSION: 0.0.0-dev

tasks:
test:
desc: Go tests
cmds:
- go test ./... -cover -v -race ${GO_PACKAGES}

vendor:
desc: Go vendor
sources:
- '**/*.go'
- ./go.sum
cmds:
- go mod vendor
- go mod tidy

vendor_update:
desc: Go vendor update
cmds:
- go get -u ./...
- task: vendor

build:
desc: Generate a development binary
dir: '{{.BUILD_PATH}}'
deps: [ vendor ]
cmds:
- |
CGO_ENABLED={{.CGO_ENABLED}} \
go build \
-mod vendor \
-trimpath \
-ldflags "-s -w -X github.com/Cloudbox/{{.APP}}/build.Version=${VERSION} -X github.com/Cloudbox/{{.APP}}/build.GitCommit=${GIT_COMMIT} -X github.com/Cloudbox/{{.APP}}/build.Timestamp=${TIMESTAMP}" \
../../cmd/{{.APP}}
release:
desc: Generate a release, but don't publish
cmds:
- goreleaser --skip-validate --skip-publish --rm-dist

snapshot:
desc: Generate a snapshot release
cmds:
- goreleaser --snapshot --skip-publish --rm-dist

publish:
desc: Generate a release, and publish
cmds:
- goreleaser --rm-dist
41 changes: 31 additions & 10 deletions cmd/cvm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ import (
"context"
"errors"
"fmt"
"github.com/Cloudbox/cvm/build"
"github.com/Cloudbox/cvm/web"
"github.com/alecthomas/kong"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/goccy/go-yaml"
"github.com/natefinch/lumberjack"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"io"
"net/http"
"os"
Expand All @@ -21,10 +12,23 @@ import (
"runtime"
"syscall"
"time"

"github.com/alecthomas/kong"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/goccy/go-yaml"
"github.com/natefinch/lumberjack"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/Cloudbox/cvm/build"
"github.com/Cloudbox/cvm/web"
)

type config struct {
Web web.Config `yaml:"web"`
Web web.Config `yaml:"web"`
TrustedProxies []string `yaml:"trusted_proxies"`
RemoteIPHeaders []string `yaml:"remote_ip_headers"`
}

var (
Expand Down Expand Up @@ -122,6 +126,23 @@ func main() {
gin.SetMode(gin.ReleaseMode)

r := gin.New()

if len(cfg.TrustedProxies) > 0 {
// use configured trusted proxies
if err := r.SetTrustedProxies(cfg.TrustedProxies); err != nil {
log.Fatal().
Err(err).
Msg("Failed initialising web server")
}

// use configured remote ip headers
if len(cfg.RemoteIPHeaders) > 0 {
r.RemoteIPHeaders = cfg.RemoteIPHeaders
}

r.ForwardedByClientIP = true
}

wc := web.New(&cfg.Web)

r.Use(gin.Recovery())
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM sc4h/alpine-s6overlay:3.12
FROM sc4h/alpine-s6overlay:v2-3.15

ARG TARGETOS
ARG TARGETARCH
Expand Down
Loading

0 comments on commit 4dd5a9b

Please sign in to comment.