Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Oct 5, 2023
0 parents commit 228a630
Show file tree
Hide file tree
Showing 16 changed files with 534 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
changelog:
exclude:
labels:
- tagpr
categories:
- title: Breaking Changes 🛠
labels:
- breaking-change
- title: New Features 🎉
labels:
- enhancement
- title: Fix bug 🐛
labels:
- bug
- title: Other Changes
labels:
- "*"
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: build

on:
push:
branches:
- main
pull_request:

jobs:
test:
name: Test
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Check out source code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: go.mod

- name: Run go mod tidy for lint
run: go mod tidy

- name: Run lint
uses: reviewdog/action-golangci-lint@v2

- name: Run gostyle
uses: k1LoW/gostyle-action@v1
with:
config-file: .gostyle.yml

- name: Run tests
run: make ci

- name: Run octocov
uses: k1LoW/octocov-action@v0
23 changes: 23 additions & 0 deletions .github/workflows/tagpr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: tagpr
on:
push:
branches:
- main

jobs:
tagpr:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Check out source code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: go.mod

- id: run-tagpr
name: Run tagpr
uses: Songmu/tagpr@v1
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.envrc
.tmp/
coverage.out
custom_metrics_benchmark.json
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
run:
go: 1.21
modules-download-mode: mod
linters:
fast: false
linters-settings:
staticcheck:
go: 1.16
issues:
exclude:
- SA3000
14 changes: 14 additions & 0 deletions .gostyle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
analyzers:
disable:
- mixedcaps # disable mixedcaps analyzer. because the underscores analyzer is more detailed.
analyzers-settings:
repetition:
exclude:
- urlstr
ifacenames:
all: true
varnames:
small-varname-max: 4 # max length of variable name for small scope (default: -1)
medium-varname-max: 10 # max length of variable name for medium scope (default: -1)
large-varname-max: 16 # max length of variable name for large scope (default: -1)
very-large-varname-max: 32 # max length of variable name for very large scope (default: -1)
23 changes: 23 additions & 0 deletions .octocov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
coverage:
if: true
codeToTestRatio:
code:
- '**/*.go'
- '!testutil/**/*.go'
- '!**/*_test.go'
test:
- 'testutil/**/*.go'
- '**/*_test.go'
testExecutionTime:
if: true
diff:
datastores:
- artifact://${GITHUB_REPOSITORY}
comment:
if: is_pull_request
summary:
if: true
report:
if: is_default_branch
datastores:
- artifact://${GITHUB_REPOSITORY}
36 changes: 36 additions & 0 deletions .tagpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# config file for the tagpr in git config format
# The tagpr generates the initial configuration, which you can rewrite to suit your environment.
# CONFIGURATIONS:
# tagpr.releaseBranch
# Generally, it is "main." It is the branch for releases. The pcpr tracks this branch,
# creates or updates a pull request as a release candidate, or tags when they are merged.
#
# tagpr.versionFile
# Versioning file containing the semantic version needed to be updated at release.
# It will be synchronized with the "git tag".
# Often this is a meta-information file such as gemspec, setup.cfg, package.json, etc.
# Sometimes the source code file, such as version.go or Bar.pm, is used.
# If you do not want to use versioning files but only git tags, specify the "-" string here.
# You can specify multiple version files by comma separated strings.
#
# tagpr.vPrefix
# Flag whether or not v-prefix is added to semver when git tagging. (e.g. v1.2.3 if true)
# This is only a tagging convention, not how it is described in the version file.
#
# tagpr.changelog (Optional)
# Flag whether or not changelog is added or changed during the release.
#
# tagpr.command (Optional)
# Command to change files just before release.
#
# tagpr.tmplate (Optional)
# Pull request template in go template format
#
# tagpr.release (Optional)
# GitHub Release creation behavior after tagging [true, draft, false]
# If this value is not set, the release is to be created.
[tagpr]
vPrefix = true
releaseBranch = main
versionFile = -
command = "make prerelease_for_tagpr"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2023 Ken'ichiro Oyama <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export GO111MODULE=on

default: test

ci: depsdev test

test:
cp go.mod testdata/go_test.mod
go mod tidy -modfile=testdata/go_test.mod
go test ./... -modfile=testdata/go_test.mod -coverprofile=coverage.out -covermode=count

benchmark:
go mod tidy -modfile=testdata/go_test.mod
go test -modfile=testdata/go_test.mod -bench . -benchmem -benchtime 10000x -run Benchmark | octocov-go-test-bench --tee > custom_metrics_benchmark.json

lint:
go mod tidy
golangci-lint run ./...
-@go vet -vettool=`which gostyle` -gostyle.config=$(PWD)/.gostyle.yml ./...
rm go.sum
git restore go.*

depsdev:
go install github.com/Songmu/ghch/cmd/ghch@latest
go install github.com/Songmu/gocredits/cmd/gocredits@latest
go install github.com/k1LoW/octocov-go-test-bench/cmd/octocov-go-test-bench@latest

prerelease:
git pull origin main --tag
go mod download
ghch -w -N ${VER}
gocredits -w .
git add CHANGELOG.md CREDITS go.mod go.sum
git commit -m'Bump up version number'
git tag ${VER}

prerelease_for_tagpr: depsdev
gocredits -w .
git add CHANGELOG.md CREDITS go.mod go.sum

release:
git push origin main --tag
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# mm

mm is a **m**iddleware-**m**iddleware for multiple rules.

It provides middleware that changes the middlewares used based on the request.

## Usage

Prepare an instance that implements [`mm.Builder`](https://pkg.go.dev/github.com/k1LoW/mm#Builder) interface.

Then, generate the middleware ( `func(next http.Handler) http.Handler` ) with [`mm.New`](https://pkg.go.dev/github.com/k1LoW/mm#New)

```go
package main

import (
"log"
"net/http"

"github.com/k1LoW/mm"
)

func main() {
r := http.NewServeMux()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})

var b mm.Builder = newMyBuilder()
m := mm.New(b)

log.Fatal(http.ListenAndServe(":8080", m(r)))
}
```
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/k1LoW/mm

go 1.21.1
37 changes: 37 additions & 0 deletions mm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mm

import (
"net/http"
)

// Builder is a middleware builder.
type Builder interface { //nostyle:ifacenames
Middleware(req *http.Request) (func(next http.Handler) http.Handler, bool)
}

type mMw struct {
builders []Builder
}

func newMMw(builders []Builder) *mMw {
return &mMw{builders: builders}
}

func (mm *mMw) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, b := range mm.builders {
mw, ok := b.Middleware(r)
if !ok {
continue
}
next = mw(next)
}
next.ServeHTTP(w, r)
})
}

// New returns a new middleware-middleware.
func New(builders ...Builder) func(next http.Handler) http.Handler {
mm := newMMw(builders)
return mm.Handler
}
Loading

0 comments on commit 228a630

Please sign in to comment.