From aa7dfb532f0b9a918a74d74f1a13ddced996dc5c Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 7 Jul 2025 21:04:42 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Scaffold=20basic=20functionality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 25 +++++++++++++++ .golangci.yaml | 39 +++++++++++++++++++++++ Taskfile.yml | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ handler.go | 13 ++++++++ handler_test.go | 1 + helper.go | 30 ++++++++++++++++++ helper_test.go | 1 + 7 files changed, 192 insertions(+) create mode 100644 .gitignore create mode 100644 .golangci.yaml create mode 100644 Taskfile.yml create mode 100644 handler.go create mode 100644 handler_test.go create mode 100644 helper.go create mode 100644 helper_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..71f6a2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Dependency directories +vendor/ + +# Go workspace file +go.work +go.work.sum + +# env file +.env + +build/ +.task/ diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..3f33683 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,39 @@ +version: "2" + +linters: + enable: + - errcheck + - govet + - ineffassign + - staticcheck + - unused + - copyloopvar + - dupl + - err113 + - errname + - exptostd + - fatcontext + - funlen + - gocognit + - goconst + - gocritic + - gocyclo + - godot + - godox + - gosec + - perfsprint + - testifylint + settings: + testifylint: + enable-all: true + disable: + - require-error + gocognit: + min-complexity: 16 + gocyclo: + min-complexity: 15 + gocritic: + enable-all: true + settings: + hugeParam: + sizeThreshold: 255 diff --git a/Taskfile.yml b/Taskfile.yml new file mode 100644 index 0000000..a8de3bb --- /dev/null +++ b/Taskfile.yml @@ -0,0 +1,83 @@ +# https://taskfile.dev + +version: '3' + +vars: + GOBIN_ENV: + sh: go env GOBIN + GOPATH_ENV: + sh: go env GOPATH + BIN: '{{if .GOBIN_ENV}}{{.GOBIN_ENV}}{{else}}{{.GOPATH_ENV}}/bin{{end}}' + +tasks: + default: + desc: fmt, vet, and build + deps: + - fmt + - lint + - build-all + + fmt: + desc: Format go files + sources: + - "**/*.go" + cmds: + - go fmt ./... + + lint: + desc: Statically analyze code + sources: + - '**/*.go' + cmds: + - golangci-lint run + + test: + desc: Run unit tests + deps: [fmt] + sources: + - '**/*.go' + generates: + - build/cover.out + cmds: + - go test -race -cover -coverprofile build/cover.out ./... + + + coverage-report: + desc: Build coverage report + deps: [test] + sources: + - build/cover.out + generates: + - build/cover.html + cmds: + - go tool cover -html=build/cover.out -o build/cover.html + + serve-report: + desc: Serve the coverage report + deps: [coverage-report] + cmds: + - ip addr list | grep inet + - php -S 0.0.0.0:3434 -t build + + serve-docs: + desc: Serve the current docs + cmds: + - godoc -http=0.0.0.0:3434 -play + + cmd-build: + internal: true + cmds: + - go build -o build/ ./cmd/{{.CMD}} + + + build-all: + desc: Builds all available commands + sources: + - "**/*.go" + generates: + - build/convids + - build/cool-down + cmds: + - task: cmd-build + vars: + CMD: "*" diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..68d6e60 --- /dev/null +++ b/handler.go @@ -0,0 +1,13 @@ +package ezhandler + +import "net/http" + +type Handler interface { + ServeHTTP(w http.ResponseWriter, r *http.Request) error +} + +type HandlerFunc func(w http.ResponseWriter, r *http.Request) error + +func (fn HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error { + return fn(w, r) +} diff --git a/handler_test.go b/handler_test.go new file mode 100644 index 0000000..98ce5de --- /dev/null +++ b/handler_test.go @@ -0,0 +1 @@ +package ezhandler_test diff --git a/helper.go b/helper.go new file mode 100644 index 0000000..043668e --- /dev/null +++ b/helper.go @@ -0,0 +1,30 @@ +package ezhandler + +import "net/http" + +func NewHelper(errH ErrorHandler) *Helper { + return &Helper{errH} +} + +// ErrorHandler writes an appropriate response to the [http.ResponseWriter] for the provided [error]. +// If the [error] is nil, it should be a no op. +type ErrorHandler func(http.ResponseWriter, *http.Request, error) + +// Helper provides methods to make easier, more idiomatic [http.Handler]s. +type Helper struct{ + errHandler ErrorHandler +} + +// Handler returns an [http.Handler] for the provided [Handler]. +// If hnd returns an error, an appropriate error response is written using the ErrorHandler. +func (help *Helper) Handler(hnd Handler) http.Handler { + // TODO implement this + panic("unimplemented") +} + +// Handler returns an [http.Handler] for the provided [HandlerFunc]. +// If hnd returns an error, an appropriate error response is written using the ErrorHandler. +func (help *Helper) HandlerFunc(hnd HandlerFunc) http.Handler { + // TODO implement this + panic("unimplemented") +} diff --git a/helper_test.go b/helper_test.go new file mode 100644 index 0000000..98ce5de --- /dev/null +++ b/helper_test.go @@ -0,0 +1 @@ +package ezhandler_test