disable brotli support without CGO
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
23fd5fe1d4
commit
5624c7af87
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -76,3 +76,6 @@
|
|||||||
[submodule "vendor/github.com/smartystreets/assertions"]
|
[submodule "vendor/github.com/smartystreets/assertions"]
|
||||||
path = vendor/github.com/smartystreets/assertions
|
path = vendor/github.com/smartystreets/assertions
|
||||||
url = https://github.com/smartystreets/assertions
|
url = https://github.com/smartystreets/assertions
|
||||||
|
[submodule "vendor/github.com/itchio/go-brotli"]
|
||||||
|
path = vendor/github.com/itchio/go-brotli
|
||||||
|
url = https://github.com/itchio/go-brotli
|
||||||
|
@ -17,7 +17,7 @@ steps:
|
|||||||
- name: test
|
- name: test
|
||||||
image: golang:latest
|
image: golang:latest
|
||||||
environment:
|
environment:
|
||||||
CGO_ENABLED: 0
|
CGO_ENABLED: 1
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
commands:
|
commands:
|
||||||
@ -29,7 +29,7 @@ steps:
|
|||||||
- name: build for linux
|
- name: build for linux
|
||||||
image: golang:latest
|
image: golang:latest
|
||||||
environment:
|
environment:
|
||||||
CGO_ENABLED: 0
|
CGO_ENABLED: 1
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
commands:
|
commands:
|
||||||
@ -48,7 +48,7 @@ steps:
|
|||||||
- name: build for freebsd
|
- name: build for freebsd
|
||||||
image: golang:latest
|
image: golang:latest
|
||||||
environment:
|
environment:
|
||||||
CGO_ENABLED: 0
|
CGO_ENABLED: 1
|
||||||
GOOS: freebsd
|
GOOS: freebsd
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
commands:
|
commands:
|
||||||
@ -59,7 +59,7 @@ steps:
|
|||||||
- name: build for macos
|
- name: build for macos
|
||||||
image: golang:latest
|
image: golang:latest
|
||||||
environment:
|
environment:
|
||||||
CGO_ENABLED: 0
|
CGO_ENABLED: 1
|
||||||
GOOS: darwin
|
GOOS: darwin
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
commands:
|
commands:
|
||||||
@ -70,7 +70,7 @@ steps:
|
|||||||
- name: build for windows
|
- name: build for windows
|
||||||
image: golang:latest
|
image: golang:latest
|
||||||
environment:
|
environment:
|
||||||
CGO_ENABLED: 0
|
CGO_ENABLED: 1
|
||||||
GOOS: windows
|
GOOS: windows
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
FILEEXT: .exe
|
FILEEXT: .exe
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
This:
|
This:
|
||||||
GoTo: main/home
|
GoTo: main/home/
|
@ -1,2 +1,2 @@
|
|||||||
This:
|
This:
|
||||||
GoTo: main/home
|
GoTo: main/home/
|
48
pkg/mark2web/brotli.go
Normal file
48
pkg/mark2web/brotli.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
// +build cgo
|
||||||
|
|
||||||
|
package mark2web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gitbase.de/apairon/mark2web/pkg/helper"
|
||||||
|
"github.com/itchio/go-brotli/enc"
|
||||||
|
)
|
||||||
|
|
||||||
|
var brotliSupported = true
|
||||||
|
|
||||||
|
func handleBrotliCompression(filename string, content []byte) {
|
||||||
|
brFilename := filename + ".br"
|
||||||
|
|
||||||
|
helper.Log.Infof("writing to compressed output file: %s", brFilename)
|
||||||
|
|
||||||
|
f, err := os.Create(brFilename)
|
||||||
|
if err != nil {
|
||||||
|
helper.Log.Panicf("could not create file '%s': %s", brFilename, err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
bw := enc.NewBrotliWriter(f, nil)
|
||||||
|
defer bw.Close()
|
||||||
|
|
||||||
|
if content != nil {
|
||||||
|
// content given
|
||||||
|
_, err = bw.Write(content)
|
||||||
|
if err != nil {
|
||||||
|
helper.Log.Panicf("could not write brotli content for '%s': %s", filename, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// read file
|
||||||
|
r, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
helper.Log.Panicf("could not open file '%s': %s", filename, err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(bw, r)
|
||||||
|
if err != nil {
|
||||||
|
helper.Log.Panicf("could not write brotli file for '%s': %s", filename, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package mark2web
|
package mark2web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -15,16 +14,26 @@ func handleCompression(filename string, content []byte) {
|
|||||||
ThreadStart(func() {
|
ThreadStart(func() {
|
||||||
if _, ok := Config.Compress.Extensions[path.Ext(filename)]; ok {
|
if _, ok := Config.Compress.Extensions[path.Ext(filename)]; ok {
|
||||||
|
|
||||||
|
if Config.Compress.Brotli {
|
||||||
|
handleBrotliCompression(filename, content)
|
||||||
|
}
|
||||||
|
|
||||||
if Config.Compress.GZIP {
|
if Config.Compress.GZIP {
|
||||||
gzFilename := filename + ".gz"
|
gzFilename := filename + ".gz"
|
||||||
|
|
||||||
helper.Log.Infof("writing to compressed output file: %s", gzFilename)
|
helper.Log.Infof("writing to compressed output file: %s", gzFilename)
|
||||||
var buf bytes.Buffer
|
|
||||||
|
|
||||||
zw, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
|
f, err := os.Create(gzFilename)
|
||||||
|
if err != nil {
|
||||||
|
helper.Log.Panicf("could not create file '%s': %s", gzFilename, err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
zw, err := gzip.NewWriterLevel(f, gzip.BestCompression)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Log.Panicf("could not initialize gzip writer for '%s': %s", filename, err)
|
helper.Log.Panicf("could not initialize gzip writer for '%s': %s", filename, err)
|
||||||
}
|
}
|
||||||
|
defer zw.Close()
|
||||||
|
|
||||||
if content != nil {
|
if content != nil {
|
||||||
// content given
|
// content given
|
||||||
@ -34,32 +43,18 @@ func handleCompression(filename string, content []byte) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// read file
|
// read file
|
||||||
f, err := os.Open(filename)
|
r, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Log.Panicf("could not open file '%s': %s", filename, err)
|
helper.Log.Panicf("could not open file '%s': %s", filename, err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer r.Close()
|
||||||
_, err = io.Copy(zw, f)
|
|
||||||
|
_, err = io.Copy(zw, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Log.Panicf("could not gzip file '%s': %s", filename, err)
|
helper.Log.Panicf("could not gzip file '%s': %s", filename, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = zw.Close()
|
|
||||||
if err != nil {
|
|
||||||
helper.Log.Panicf("could not close gziped content for '%s': %s", filename, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(gzFilename)
|
|
||||||
if err != nil {
|
|
||||||
helper.Log.Panicf("could not create file '%s': %s", gzFilename, err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
_, err = buf.WriteTo(f)
|
|
||||||
if err != nil {
|
|
||||||
helper.Log.Panicf("could not write to file '%s': %s", gzFilename, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -28,6 +28,7 @@ type GlobalConfig struct {
|
|||||||
} `yaml:"OtherFiles"`
|
} `yaml:"OtherFiles"`
|
||||||
|
|
||||||
Compress struct {
|
Compress struct {
|
||||||
|
Brotli bool `yaml:"Brotli"`
|
||||||
GZIP bool `yaml:"GZIP"`
|
GZIP bool `yaml:"GZIP"`
|
||||||
Extensions map[string]string `yaml:"Extensions"`
|
Extensions map[string]string `yaml:"Extensions"`
|
||||||
} `yaml:"Compress"`
|
} `yaml:"Compress"`
|
||||||
|
@ -26,35 +26,52 @@ func WriteWebserverConfig() {
|
|||||||
switch Config.Webserver.Type {
|
switch Config.Webserver.Type {
|
||||||
case "apache":
|
case "apache":
|
||||||
configStr := `
|
configStr := `
|
||||||
AddCharset UTF-8 .html
|
AddCharset UTF-8 .html
|
||||||
AddCharset UTF-8 .json
|
AddCharset UTF-8 .json
|
||||||
AddCharset UTF-8 .js
|
AddCharset UTF-8 .js
|
||||||
AddCharset UTF-8 .css
|
AddCharset UTF-8 .css
|
||||||
|
|
||||||
|
RemoveLanguage .br
|
||||||
|
|
||||||
|
<IfModule mod_headers.c>
|
||||||
|
|
||||||
RewriteEngine on
|
RewriteEngine on
|
||||||
`
|
`
|
||||||
|
|
||||||
if Config.Compress.GZIP {
|
rewriteMacro := func(e, c, x, xx string) string {
|
||||||
for ext, contentType := range Config.Compress.Extensions {
|
return `
|
||||||
rExt := regexp.QuoteMeta(ext)
|
|
||||||
|
|
||||||
configStr += `
|
######` + e + `.` + x + `
|
||||||
RewriteCond "%{HTTP:Accept-encoding}" "gzip"
|
|
||||||
RewriteCond "%{REQUEST_FILENAME}\.gz" -s
|
|
||||||
RewriteRule "^(.*)` + rExt + `" "$1` + rExt + `\.gz" [QSA]
|
|
||||||
|
|
||||||
RewriteRule "` + rExt + `\.gz$" "-" [E=no-gzip:1]
|
RewriteCond "%{HTTP:Accept-encoding}" "` + xx + `"
|
||||||
|
RewriteCond "%{REQUEST_FILENAME}\.` + x + `" -s
|
||||||
|
RewriteRule "^(.*)` + e + `" "$1` + e + `\.` + x + `" [QSA]
|
||||||
|
|
||||||
<FilesMatch "` + rExt + `\.gz$">
|
RewriteRule "` + e + `\.` + x + `$" "-" [E=no-gzip:1,E=no-brotli]
|
||||||
ForceType '` + contentType + `; charset=UTF-8'
|
|
||||||
Header append Content-Encoding gzip
|
<FilesMatch "` + e + `\.` + x + `$">
|
||||||
|
ForceType '` + c + `; charset=UTF-8'
|
||||||
|
Header append Content-Encoding ` + xx + `
|
||||||
Header append Vary Accept-Encoding
|
Header append Vary Accept-Encoding
|
||||||
</FilesMatch>
|
</FilesMatch>
|
||||||
|
|
||||||
`
|
`
|
||||||
|
}
|
||||||
|
|
||||||
|
for ext, contentType := range Config.Compress.Extensions {
|
||||||
|
rExt := regexp.QuoteMeta(ext)
|
||||||
|
if brotliSupported && Config.Compress.Brotli {
|
||||||
|
configStr += rewriteMacro(rExt, contentType, "br", "br")
|
||||||
|
}
|
||||||
|
if Config.Compress.GZIP {
|
||||||
|
configStr += rewriteMacro(rExt, contentType, "gz", "gzip")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configStr += `
|
||||||
|
</IfModule>
|
||||||
|
`
|
||||||
|
|
||||||
if configStr != "" {
|
if configStr != "" {
|
||||||
htaccessFile := Config.Directories.Output + "/.htaccess"
|
htaccessFile := Config.Directories.Output + "/.htaccess"
|
||||||
helper.Log.Noticef("writing webserver config to: %s", htaccessFile)
|
helper.Log.Noticef("writing webserver config to: %s", htaccessFile)
|
||||||
|
14
pkg/mark2web/no_cgo.go
Normal file
14
pkg/mark2web/no_cgo.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// +build !cgo
|
||||||
|
|
||||||
|
package mark2web
|
||||||
|
|
||||||
|
import "gitbase.de/apairon/mark2web/pkg/helper"
|
||||||
|
|
||||||
|
var brotliSupported = false
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
helper.Log.Warning("cgo is disabled, so brotli compression is not supported")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleBrotliCompression(filename string, content []byte) {
|
||||||
|
}
|
1
vendor/github.com/itchio/go-brotli
generated
vendored
Submodule
1
vendor/github.com/itchio/go-brotli
generated
vendored
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 29899a447037af0c68028d70df58ac2faebf280c
|
@ -14,6 +14,7 @@ OtherFiles:
|
|||||||
Action: "copy"
|
Action: "copy"
|
||||||
|
|
||||||
Compress:
|
Compress:
|
||||||
|
Brotli: True
|
||||||
GZIP: True
|
GZIP: True
|
||||||
Extensions:
|
Extensions:
|
||||||
.html: text/html
|
.html: text/html
|
||||||
|
Loading…
Reference in New Issue
Block a user