generated from templates/go-generic
Compare commits
No commits in common. "f393d982f588054b91ac654800ada959fb6f8865" and "7751cf2fe8cc7612c0c8ec75db5d54a3982fa41d" have entirely different histories.
f393d982f5
...
7751cf2fe8
16 changed files with 33 additions and 280 deletions
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
name: CI
|
name: Workflow
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
pull_request:
|
||||||
|
@ -8,10 +8,12 @@ on:
|
||||||
- synchronize
|
- synchronize
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
tests:
|
test:
|
||||||
name: Tests
|
|
||||||
if: ${{ ! github.event.pull_request.draft }}
|
if: ${{ ! github.event.pull_request.draft }}
|
||||||
runs-on: docker
|
runs-on: docker
|
||||||
|
env:
|
||||||
|
GO_TEST_VERBOSE: "1"
|
||||||
|
GO_TEST_COVER: "1"
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout Repository
|
- name: Checkout Repository
|
||||||
uses: https://code.forgejo.org/actions/checkout@v4
|
uses: https://code.forgejo.org/actions/checkout@v4
|
||||||
|
@ -22,19 +24,3 @@ jobs:
|
||||||
env:
|
env:
|
||||||
PROJECT_TEST_VERBOSE: "1"
|
PROJECT_TEST_VERBOSE: "1"
|
||||||
PROJECT_TEST_COVER: "1"
|
PROJECT_TEST_COVER: "1"
|
||||||
- name: Gosec
|
|
||||||
uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main
|
|
||||||
with:
|
|
||||||
target: gosec
|
|
||||||
|
|
||||||
style:
|
|
||||||
name: Style
|
|
||||||
if: ${{ ! github.event.pull_request.draft }}
|
|
||||||
runs-on: docker
|
|
||||||
steps:
|
|
||||||
- name: Checkout Repository
|
|
||||||
uses: https://code.forgejo.org/actions/checkout@v4
|
|
||||||
- name: Run staticcheck
|
|
||||||
uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main
|
|
||||||
with:
|
|
||||||
target: staticcheck
|
|
||||||
|
|
|
@ -13,14 +13,6 @@ output:
|
||||||
sort-results: true
|
sort-results: true
|
||||||
|
|
||||||
linters-settings:
|
linters-settings:
|
||||||
depguard:
|
|
||||||
rules:
|
|
||||||
main:
|
|
||||||
files:
|
|
||||||
- $all
|
|
||||||
allow:
|
|
||||||
- $gostd
|
|
||||||
- codeflow.dananglin.me.uk/apollo/indieauth-server
|
|
||||||
lll:
|
lll:
|
||||||
line-length: 140
|
line-length: 140
|
||||||
|
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/indieauth-server/internal/executors"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
binaryVersion string
|
|
||||||
buildTime string
|
|
||||||
goVersion string
|
|
||||||
gitCommit string
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// Set up logging
|
|
||||||
loggingLevel := new(slog.LevelVar)
|
|
||||||
|
|
||||||
slogOpts := slog.HandlerOptions{
|
|
||||||
Level: loggingLevel,
|
|
||||||
}
|
|
||||||
|
|
||||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slogOpts))
|
|
||||||
slog.SetDefault(logger)
|
|
||||||
loggingLevel.Set(slog.LevelInfo)
|
|
||||||
|
|
||||||
if err := executors.Execute(os.Args[1:]); err != nil {
|
|
||||||
slog.Error(err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
3
go.mod
3
go.mod
|
@ -1,3 +0,0 @@
|
||||||
module codeflow.dananglin.me.uk/apollo/indieauth-server
|
|
||||||
|
|
||||||
go 1.23.2
|
|
|
@ -1,27 +0,0 @@
|
||||||
package executors
|
|
||||||
|
|
||||||
type command struct {
|
|
||||||
name string
|
|
||||||
args []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func newCommand(args []string) command {
|
|
||||||
if len(args) == 0 {
|
|
||||||
return command{
|
|
||||||
name: "help",
|
|
||||||
args: make([]string, 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(args) == 1 {
|
|
||||||
return command{
|
|
||||||
name: args[0],
|
|
||||||
args: make([]string, 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return command{
|
|
||||||
name: args[0],
|
|
||||||
args: args[1:],
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
package executors
|
|
||||||
|
|
||||||
type UnrecognisedCommandError struct {
|
|
||||||
command string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e UnrecognisedCommandError) Error() string {
|
|
||||||
return "unrecognised command: " + e.command
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
package executors
|
|
||||||
|
|
||||||
func Execute(args []string) error {
|
|
||||||
command := newCommand(args)
|
|
||||||
|
|
||||||
executorFuncMap := map[string]func(args []string) error{
|
|
||||||
"serve": executeServeCommand,
|
|
||||||
"version": executeVersionCommand,
|
|
||||||
}
|
|
||||||
|
|
||||||
executeFunc, ok := executorFuncMap[command.name]
|
|
||||||
if !ok {
|
|
||||||
return UnrecognisedCommandError{command.name}
|
|
||||||
}
|
|
||||||
|
|
||||||
return executeFunc(command.args)
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
package executors
|
|
|
@ -1,55 +0,0 @@
|
||||||
package executors
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/indieauth-server/internal/info"
|
|
||||||
"codeflow.dananglin.me.uk/apollo/indieauth-server/internal/router"
|
|
||||||
)
|
|
||||||
|
|
||||||
type serveExecutor struct {
|
|
||||||
*flag.FlagSet
|
|
||||||
|
|
||||||
address string
|
|
||||||
}
|
|
||||||
|
|
||||||
func executeServeCommand(args []string) error {
|
|
||||||
executorName := "serve"
|
|
||||||
|
|
||||||
executor := serveExecutor{
|
|
||||||
FlagSet: flag.NewFlagSet(executorName, flag.ExitOnError),
|
|
||||||
}
|
|
||||||
|
|
||||||
executor.StringVar(&executor.address, "address", "0.0.0.0:8080", "The address that the server will listen on")
|
|
||||||
|
|
||||||
if err := executor.Parse(args); err != nil {
|
|
||||||
return fmt.Errorf("(%s) flag parsing error: %w", executorName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := executor.execute(); err != nil {
|
|
||||||
return fmt.Errorf("(%s) execution error: %w", executorName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *serveExecutor) execute() error {
|
|
||||||
server := http.Server{
|
|
||||||
Addr: e.address,
|
|
||||||
Handler: router.NewServeMux(),
|
|
||||||
ReadHeaderTimeout: 1 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Info(info.ApplicationName+" is listening for web requests", "address", e.address)
|
|
||||||
|
|
||||||
err := server.ListenAndServe()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error running the server: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,58 +0,0 @@
|
||||||
package executors
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"text/tabwriter"
|
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/indieauth-server/internal/info"
|
|
||||||
)
|
|
||||||
|
|
||||||
type versionExecutor struct {
|
|
||||||
*flag.FlagSet
|
|
||||||
|
|
||||||
showFullVersion bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func executeVersionCommand(args []string) error {
|
|
||||||
executorName := "version"
|
|
||||||
|
|
||||||
executor := versionExecutor{
|
|
||||||
FlagSet: flag.NewFlagSet(executorName, flag.ExitOnError),
|
|
||||||
}
|
|
||||||
|
|
||||||
executor.BoolVar(&executor.showFullVersion, "full", false, "Print the applications full build information")
|
|
||||||
|
|
||||||
if err := executor.Parse(args); err != nil {
|
|
||||||
return fmt.Errorf("(%s) flag parsing error: %w", executorName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
executor.printVersion()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *versionExecutor) printVersion() {
|
|
||||||
if !e.showFullVersion {
|
|
||||||
fmt.Fprintf(os.Stdout, "%s %s\n", info.ApplicationName, info.BinaryVersion)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var builder strings.Builder
|
|
||||||
|
|
||||||
builder.WriteString(info.ApplicationName + "\n\n")
|
|
||||||
|
|
||||||
tableWriter := tabwriter.NewWriter(&builder, 0, 4, 1, ' ', 0)
|
|
||||||
|
|
||||||
_, _ = tableWriter.Write([]byte("Version:" + "\t" + info.BinaryVersion + "\n"))
|
|
||||||
_, _ = tableWriter.Write([]byte("Git commit:" + "\t" + info.GitCommit + "\n"))
|
|
||||||
_, _ = tableWriter.Write([]byte("Go version:" + "\t" + info.GoVersion + "\n"))
|
|
||||||
_, _ = tableWriter.Write([]byte("Build date:" + "\t" + info.BuildTime + "\n"))
|
|
||||||
|
|
||||||
tableWriter.Flush()
|
|
||||||
|
|
||||||
os.Stdout.WriteString(builder.String())
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
package info
|
|
||||||
|
|
||||||
const (
|
|
||||||
ApplicationName string = "indieauth-server"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
BinaryVersion string //nolint:gochecknoglobals
|
|
||||||
BuildTime string //nolint:gochecknoglobals
|
|
||||||
GoVersion string //nolint:gochecknoglobals
|
|
||||||
GitCommit string //nolint:gochecknoglobals
|
|
||||||
)
|
|
|
@ -1,9 +0,0 @@
|
||||||
package router
|
|
||||||
|
|
||||||
import "net/http"
|
|
||||||
|
|
||||||
func NewServeMux() *http.ServeMux {
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
|
|
||||||
return mux
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
module codeflow.dananglin.me.uk/apollo/indieauth-server/magefiles
|
|
||||||
|
|
||||||
go 1.23.2
|
|
||||||
|
|
||||||
require github.com/magefile/mage v1.15.0
|
|
|
@ -1,2 +0,0 @@
|
||||||
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
|
|
||||||
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
app = "indieauth-server"
|
app = "binary"
|
||||||
defaultInstallPrefix = "/usr/local"
|
defaultInstallPrefix = "/usr/local"
|
||||||
envInstallPrefix = "PROJECT_INSTALL_PREFIX"
|
envInstallPrefix = "PROJECT_INSTALL_PREFIX"
|
||||||
envTestVerbose = "PROJECT_TEST_VERBOSE"
|
envTestVerbose = "PROJECT_TEST_VERBOSE"
|
||||||
|
@ -52,19 +52,11 @@ func Lint() error {
|
||||||
return sh.RunV("golangci-lint", "run", "--color", "always")
|
return sh.RunV("golangci-lint", "run", "--color", "always")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Gosec() error {
|
|
||||||
return sh.RunV("gosec", "./...")
|
|
||||||
}
|
|
||||||
|
|
||||||
func Staticcheck() error {
|
|
||||||
return sh.RunV("staticcheck", "./...")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build build the executable.
|
// Build build the executable.
|
||||||
// To rebuild packages that are already up-to-date set PROJECT_BUILD_REBUILD_ALL=1
|
// To rebuild packages that are already up-to-date set PROJECT_BUILD_REBUILD_ALL=1
|
||||||
// To enable verbose mode set PROJECT_BUILD_VERBOSE=1
|
// To enable verbose mode set PROJECT_BUILD_VERBOSE=1
|
||||||
func Build() error {
|
func Build() error {
|
||||||
main := "./cmd/" + app
|
main := "main.go"
|
||||||
flags := ldflags()
|
flags := ldflags()
|
||||||
build := sh.RunCmd("go", "build")
|
build := sh.RunCmd("go", "build")
|
||||||
args := []string{"-ldflags=" + flags, "-o", binary}
|
args := []string{"-ldflags=" + flags, "-o", binary}
|
||||||
|
@ -118,21 +110,10 @@ func Clean() error {
|
||||||
|
|
||||||
// ldflags returns the build flags.
|
// ldflags returns the build flags.
|
||||||
func ldflags() string {
|
func ldflags() string {
|
||||||
versionPackage := "codeflow.dananglin.me.uk/apollo/indieauth-server/internal/info"
|
ldflagsfmt := "-s -w -X main.binaryVersion=%s -X main.gitCommit=%s -X main.goVersion=%s -X main.buildTime=%s"
|
||||||
binaryVersionVar := versionPackage + "." + "BinaryVersion"
|
|
||||||
gitCommitVar := versionPackage + "." + "GitCommit"
|
|
||||||
goVersionVar := versionPackage + "." + "GoVersion"
|
|
||||||
buildTimeVar := versionPackage + "." + "BuildTime"
|
|
||||||
ldflagsfmt := "-s -w -X %s=%s -X %s=%s -X %s=%s -X %s=%s"
|
|
||||||
buildTime := time.Now().UTC().Format(time.RFC3339)
|
buildTime := time.Now().UTC().Format(time.RFC3339)
|
||||||
|
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(ldflagsfmt, version(), gitCommit(), runtime.Version(), buildTime)
|
||||||
ldflagsfmt,
|
|
||||||
binaryVersionVar, version(),
|
|
||||||
gitCommitVar, gitCommit(),
|
|
||||||
goVersionVar, runtime.Version(),
|
|
||||||
buildTimeVar, buildTime,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// version returns the latest git tag using git describe.
|
// version returns the latest git tag using git describe.
|
||||||
|
|
24
main.go
Normal file
24
main.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
binaryVersion string
|
||||||
|
buildTime string
|
||||||
|
goVersion string
|
||||||
|
gitCommit string
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := run(); err != nil {
|
||||||
|
fmt.Printf("ERROR: %v.\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func run() error {
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in a new issue