Skip to content

Commit

Permalink
Add gzip compression and expires headers
Browse files Browse the repository at this point in the history
  • Loading branch information
endigma committed Sep 14, 2021
1 parent d2a8693 commit 85a7042
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/MichaelMure/go-term-markdown v0.1.3
github.com/MichaelMure/go-term-text v0.2.10 // indirect
github.com/NYTimes/gziphandler v1.1.1
github.com/alecthomas/chroma v0.8.2
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/fatih/color v1.10.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/MichaelMure/go-term-markdown v0.1.3/go.mod h1:WNYfAWS95/dLzuTMpjFo7Kh
github.com/MichaelMure/go-term-text v0.2.7/go.mod h1:6z+q5b/nP1V8I9KkWQcUi5QpmF8DVrz9vLJ4hdoxHnM=
github.com/MichaelMure/go-term-text v0.2.10 h1:5OGpCDINh6V7KcZUtff+T2gtnIgbDdYmNlFSa5Cct1k=
github.com/MichaelMure/go-term-text v0.2.10/go.mod h1:DrWFodEEZsSgK1PQY9dqTn+pw3zGeYDmVF5PA8ECZhs=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=
Expand Down
15 changes: 13 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"
"text/template"
"time"

termmd "github.com/MichaelMure/go-term-markdown"
"github.com/rs/zerolog/log"
Expand All @@ -16,7 +17,7 @@ import (
)

// Handler is the main response handler for requests
func Handler(rw http.ResponseWriter, req *http.Request) {
var Handler = http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {

if viper.GetBool("general.debug") {
utils.DebugReq(req)
Expand All @@ -38,6 +39,9 @@ func Handler(rw http.ResponseWriter, req *http.Request) {
defer sourcefile.Close()

source, err := ioutil.ReadAll(sourcefile)
if err != nil {
log.Error().Err(err).Msg("Failed to read file")
}

if viper.GetBool("general.fancycurl") && !utils.IsInArr("true", req.Header.Values("RawPlease")) {
if strings.HasPrefix(req.UserAgent(), "curl") {
Expand Down Expand Up @@ -69,8 +73,15 @@ func Handler(rw http.ResponseWriter, req *http.Request) {
page.SidebarContents = fmt.Sprintf("<h3><a class='home' href='%s/'><i class='fas fa-home'></i><span>%s</span></a></h3><ul>", viper.GetString("general.prefix"), viper.GetString("website.sitename")) + sidebarContent + "</ul>"
}

cacheSince := time.Now().Format(http.TimeFormat)
cacheUntil := time.Now().AddDate(60, 0, 0).Format(http.TimeFormat)

rw.Header().Set("Cache-Control", "max-age:290304000, public")
rw.Header().Set("Last-Modified", cacheSince)
rw.Header().Set("Expires", cacheUntil)

page.Raw = path

tmpl := template.Must(template.ParseFiles(viper.GetString("workdir") + "assets/page.html"))
tmpl.Execute(rw, page)
}
})
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"gitcat.ca/endigma/holden/handler"
"gitcat.ca/endigma/holden/utils"
"github.com/NYTimes/gziphandler"
)

func init() {
Expand Down Expand Up @@ -105,8 +106,7 @@ func main() {
fs2 := http.FileServer(http.Dir(viper.GetString("general.workdir") + "assets/public"))
http.Handle(viper.GetString("general.prefix")+"/public/", http.StripPrefix(viper.GetString("general.prefix")+"/public/", fs2))
http.Handle(viper.GetString("general.prefix")+"/favicon.ico", http.StripPrefix(viper.GetString("general.prefix")+"/public/", fs2))

http.HandleFunc(viper.GetString("general.prefix")+"/", handler.Handler)
http.Handle(viper.GetString("general.prefix")+"/", gziphandler.GzipHandler(handler.Handler))

log.Info().Msgf("Starting http server on :%s", viper.GetString("general.port"))
log.Fatal().Err(http.ListenAndServe(":"+viper.GetString("general.port"), nil)).Msg("Fatal")
Expand Down

0 comments on commit 85a7042

Please sign in to comment.