mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-01-19 08:57:55 +00:00
add gzip support
This commit is contained in:
parent
16a8d5d575
commit
fac052f052
3 changed files with 43 additions and 11 deletions
12
cmd/main.go
12
cmd/main.go
|
@ -20,6 +20,7 @@ import (
|
||||||
"codeberg.org/codeberg/pages/server/certificates"
|
"codeberg.org/codeberg/pages/server/certificates"
|
||||||
"codeberg.org/codeberg/pages/server/database"
|
"codeberg.org/codeberg/pages/server/database"
|
||||||
"codeberg.org/codeberg/pages/server/gitea"
|
"codeberg.org/codeberg/pages/server/gitea"
|
||||||
|
"codeberg.org/codeberg/pages/server/gzip"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AllowedCorsDomains lists the domains for which Cross-Origin Resource Sharing is allowed.
|
// AllowedCorsDomains lists the domains for which Cross-Origin Resource Sharing is allowed.
|
||||||
|
@ -88,14 +89,13 @@ func Serve(ctx *cli.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create handler based on settings
|
// Create handler based on settings
|
||||||
handler := server.Handler(mainDomainSuffix, rawDomain,
|
httpsHandler := gzip.SetupCompression(server.Handler(mainDomainSuffix, rawDomain,
|
||||||
giteaClient,
|
giteaClient,
|
||||||
giteaRoot, rawInfoPage,
|
giteaRoot, rawInfoPage,
|
||||||
BlacklistedPaths, allowedCorsDomains,
|
BlacklistedPaths, allowedCorsDomains,
|
||||||
dnsLookupCache, canonicalDomainCache)
|
dnsLookupCache, canonicalDomainCache))
|
||||||
|
|
||||||
fastServer := server.SetupServer(handler)
|
httpHandler := server.SetupHTTPACMEChallengeServer(challengeCache)
|
||||||
httpServer := server.SetupHTTPACMEChallengeServer(challengeCache)
|
|
||||||
|
|
||||||
// Setup listener and TLS
|
// Setup listener and TLS
|
||||||
log.Info().Msgf("Listening on https://%s", listeningAddress)
|
log.Info().Msgf("Listening on https://%s", listeningAddress)
|
||||||
|
@ -135,7 +135,7 @@ func Serve(ctx *cli.Context) error {
|
||||||
if enableHTTPServer {
|
if enableHTTPServer {
|
||||||
go func() {
|
go func() {
|
||||||
log.Info().Msg("Start HTTP server listening on :80")
|
log.Info().Msg("Start HTTP server listening on :80")
|
||||||
err := http.ListenAndServe("[::]:80", httpServer)
|
err := http.ListenAndServe("[::]:80", httpHandler)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic().Err(err).Msg("Couldn't start HTTP fastServer")
|
log.Panic().Err(err).Msg("Couldn't start HTTP fastServer")
|
||||||
}
|
}
|
||||||
|
@ -144,7 +144,7 @@ func Serve(ctx *cli.Context) error {
|
||||||
|
|
||||||
// Start the web fastServer
|
// Start the web fastServer
|
||||||
log.Info().Msgf("Start listening on %s", listener.Addr())
|
log.Info().Msgf("Start listening on %s", listener.Addr())
|
||||||
if err := http.Serve(listener, fastServer); err != nil {
|
if err := http.Serve(listener, httpsHandler); err != nil {
|
||||||
log.Panic().Err(err).Msg("Couldn't start fastServer")
|
log.Panic().Err(err).Msg("Couldn't start fastServer")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
37
server/gzip/gzip.go
Normal file
37
server/gzip/gzip.go
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package gzip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"compress/gzip"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type gzipResponseWriter struct {
|
||||||
|
Writer *gzip.Writer
|
||||||
|
ResponseWriter http.ResponseWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gz gzipResponseWriter) Header() http.Header {
|
||||||
|
return gz.ResponseWriter.Header()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gz gzipResponseWriter) Write(b []byte) (int, error) {
|
||||||
|
return gz.Writer.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gz gzipResponseWriter) WriteHeader(statusCode int) {
|
||||||
|
gz.ResponseWriter.WriteHeader(statusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetupCompression(handler http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||||
|
handler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Encoding", "gzip")
|
||||||
|
gz := gzip.NewWriter(w)
|
||||||
|
defer gz.Close()
|
||||||
|
handler(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,11 +9,6 @@ import (
|
||||||
"codeberg.org/codeberg/pages/server/utils"
|
"codeberg.org/codeberg/pages/server/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupServer(handler http.HandlerFunc) http.HandlerFunc {
|
|
||||||
// TODO: enagle gzip compression
|
|
||||||
return handler
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetupHTTPACMEChallengeServer(challengeCache cache.SetGetKey) http.HandlerFunc {
|
func SetupHTTPACMEChallengeServer(challengeCache cache.SetGetKey) http.HandlerFunc {
|
||||||
challengePath := "/.well-known/acme-challenge/"
|
challengePath := "/.well-known/acme-challenge/"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue