Skip to content

Commit

Permalink
Update dependencies and README (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jun 30, 2021
1 parent bbc387a commit c78f450
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: bench
on:
pull_request:
Expand Down Expand Up @@ -68,8 +69,7 @@ jobs:
run: |
export REF_NAME=master
cd __base
BENCH_COUNT=5 make bench-run bench-stat
cp bench-master.txt ../bench-master.txt
make | grep bench-run && (BENCH_COUNT=5 make bench-run bench-stat && cp bench-master.txt ../bench-master.txt) || echo "No benchmarks in base"
- name: Benchmark
id: bench
run: |
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/cloc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This script is provided by github.com/bool64/dev.
name: cloc
on:
pull_request:
jobs:
cloc:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: pr
- name: Checkout base code
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base
- name: Count Lines Of Code
id: loc
run: |
curl -OL https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
echo "::set-output name=diff::$OUTPUT"
- name: Comment Code Lines
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: LOC
message: |
### Lines Of Code
${{ steps.loc.outputs.diff }}
5 changes: 3 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: lint
on:
push:
Expand All @@ -14,10 +15,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/[email protected].1
uses: golangci/[email protected].2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.38.0
version: v1.40.1

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: test-unit
on:
push:
Expand Down Expand Up @@ -51,8 +52,7 @@ jobs:
if: matrix.go-version == '1.16.x' && env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
run: |
cd __base
make test-unit
go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt
make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt) || echo "No test-unit in base"
- name: Test
id: test
run: |
Expand Down
12 changes: 5 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
.idea
coverage.txt
unit.coverprofile
*_last_run.json
.vscode
bench-*.txt
vendor
/.idea
/*.coverprofile
/.vscode
/bench-*.txt
/vendor
10 changes: 6 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ linters-settings:
check-exported: false
unparam:
check-exported: true
cyclop:
max-complexity: 12

linters:
enable-all: true
disable:
- testpackage
- goerr113
- lll
- maligned
- gochecknoglobals
Expand All @@ -30,9 +30,11 @@ linters:
- paralleltest
- forbidigo
- exhaustivestruct
- cyclop
- interfacer
- interfacer # deprecated
- forcetypeassert
- scopelint # deprecated
- ifshort # too many false positives
- golint # deprecated

issues:
exclude-use-default: false
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#GOLANGCI_LINT_VERSION := "v1.38.0" # Optional configuration to pinpoint golangci-lint version.
#GOLANGCI_LINT_VERSION := "v1.40.1" # Optional configuration to pinpoint golangci-lint version.

# The head of Makefile determines location of dev-go to include standard targets.
GO ?= go
Expand Down
50 changes: 44 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ serving them directly to capable user agents. This library implements such funct

Read more in a [blog post](https://dev.to/vearutop/serving-compressed-static-assets-with-http-in-go-1-16-55bb).

> **_NOTE:_** Guarding new api (`embed`) with build tags is not a viable option, since it imposes
> **_NOTE:_** Guarding new api (`embed`) with build tags is not a viable option, since it imposes
> [issue](https://github.com/golang/go/issues/40067) in older versions of Go.
## Example
Expand Down Expand Up @@ -78,8 +78,8 @@ decompressed are served with [`http.ServeContent`](https://golang.org/pkg/net/ht

### Brotli support

Support for `brotli` is optional. Using `brotli` adds about 260 KB to binary size, that's why it is moved to
a separate package.
Support for `brotli` is optional. Using `brotli` adds about 260 KB to binary size, that's why it is moved to a separate
package.

> **_NOTE:_** Although [`brotli`](https://github.com/google/brotli) has better compression than `gzip` and already
> has wide support in browsers, it has limitations for non-https servers,
Expand All @@ -88,10 +88,48 @@ a separate package.
### Runtime encoding

Recommended way of embedding assets is to compress assets before the build, so that binary includes `*.gz` or `*.br` files.
This can be inconvenient in some cases, there is `EncodeOnInit` option to compress assets in runtime when creating file server.
Once compressed, assets will be served directly without additional dynamic compression.
Recommended way of embedding assets is to compress assets before the build, so that binary includes `*.gz` or `*.br`
files. This can be inconvenient in some cases, there is `EncodeOnInit` option to compress assets in runtime when
creating file server. Once compressed, assets will be served directly without additional dynamic compression.

Files with extensions ".gz", ".br", ".gif", ".jpg", ".png", ".webp" are excluded from runtime encoding by default.

> **_NOTE:_** Compressing assets in runtime can degrade startup performance and increase memory usage to prepare and store compressed data.
### Mounting a subdirectory

It may be convenient to strip leading directory from an embedded file system, you can do that with `fs.Sub` and a type
assertion.

```go
package main

import (
"embed"
"io/fs"
"log"
"net/http"

"github.com/vearutop/statigz"
"github.com/vearutop/statigz/brotli"
)

// Declare your embedded assets.

//go:embed static/*
var st embed.FS

func main() {
// Retrieve sub directory.
sub, err := fs.Sub(st, "static")
if err != nil {
log.Fatal(err)
}

// Plug static assets handler to your server or router.
err = http.ListenAndServe(":80", statigz.FileServer(sub.(fs.ReadDirFS), brotli.AddEncoding))
if err != nil {
log.Fatal(err)
}
}
```
8 changes: 7 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package statigz_test

import (
"embed"
"io/fs"
"log"
"net/http"

Expand All @@ -15,8 +16,13 @@ import (
var st embed.FS

func ExampleFileServer() {
s, err := fs.Sub(st, "_testdata")
if err != nil {
log.Fatal(err)
}

// Plug static assets handler to your server or router.
err := http.ListenAndServe(":80", statigz.FileServer(st, brotli.AddEncoding))
err = http.ListenAndServe(":80", statigz.FileServer(s.(fs.ReadDirFS), brotli.AddEncoding))
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/vearutop/statigz
go 1.16

require (
github.com/andybalholm/brotli v1.0.1
github.com/bool64/dev v0.1.25
github.com/andybalholm/brotli v1.0.3
github.com/bool64/dev v0.1.35
github.com/stretchr/testify v1.4.0
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/bool64/dev v0.1.25 h1:iSfcaS1FP6F2TlMI1daAvqk6PYl23XDf6oZu7WRXB8Y=
github.com/bool64/dev v0.1.25/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/andybalholm/brotli v1.0.3 h1:fpcw+r1N1h0Poc1F/pHbW40cUm/lMEQslZtCkBQ0UnM=
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/bool64/dev v0.1.35 h1:uouBAq2kAJ+k9UypYRs118bAYttNQWDyK4IzjfLb5fc=
github.com/bool64/dev v0.1.35/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down

0 comments on commit c78f450

Please sign in to comment.