rio/internal/pages/pages.go

120 lines
2.6 KiB
Go
Raw Normal View History

2024-01-01 13:19:19 +00:00
package pages
2023-12-31 12:26:56 +00:00
import (
"mime"
"net/http"
"strings"
"time"
"git.polynom.me/rio/internal/constants"
"git.polynom.me/rio/internal/repo"
2023-12-31 12:26:56 +00:00
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
var (
2023-12-31 23:38:39 +00:00
pageCache = cache.New(6*time.Hour, 1*time.Hour)
2023-12-31 12:26:56 +00:00
)
type PageContentCache struct {
2023-12-31 23:38:39 +00:00
Content []byte
mimeType string
RequestedAt time.Time
2023-12-31 12:26:56 +00:00
}
func makePageContentCacheEntry(username, path string) string {
return username + ":" + path
}
2024-01-06 16:42:08 +00:00
func addHeaders(csp, contentType string, w http.ResponseWriter) {
w.Header().Set("Content-Type", contentType)
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Strict-Transport-Security", "max-age=31536000")
if csp != "" {
w.Header().Set("Content-Security-Policy", csp)
}
}
func ServeFile(username, reponame, path, defaultCsp string, giteaClient *repo.GiteaClient, w http.ResponseWriter) {
// Provide a default file.
switch {
case path == "":
2023-12-31 12:26:56 +00:00
path = "/index.html"
2024-01-06 16:42:08 +00:00
case path[len(path)-1] == '/':
path = path + "index.html"
2023-12-31 12:26:56 +00:00
}
// Strip away a starting / as it messes with Gitea
if path[:1] == "/" {
path = path[1:]
}
key := makePageContentCacheEntry(username, path)
entry, found := pageCache.Get(key)
var content []byte
var mimeType string
2023-12-31 13:08:23 +00:00
var err error
var since *time.Time = nil
2023-12-31 12:26:56 +00:00
if found {
log.Debugf("Returning %s from cache", path)
content = entry.(PageContentCache).Content
mimeType = entry.(PageContentCache).mimeType
sinceRaw := entry.(PageContentCache).RequestedAt
since = &sinceRaw
}
content, changed, err := giteaClient.GetFile(
username,
reponame,
constants.PagesBranch,
path,
since,
)
2024-01-06 16:42:08 +00:00
csp := repo.GetCSPForRepository(username, reponame, "", giteaClient)
if err != nil {
if !found {
2023-12-31 21:55:37 +00:00
log.Errorf("Failed to get file %s/%s/%s (%s)", username, reponame, path, err)
2024-01-06 16:42:08 +00:00
addHeaders(csp, "text/html", w)
2023-12-31 12:26:56 +00:00
w.WriteHeader(404)
} else {
log.Debugf("Request failed but page %s is cached in memory", path)
2024-01-06 16:42:08 +00:00
addHeaders(csp, mimeType, w)
w.WriteHeader(200)
w.Write(content)
2023-12-31 12:26:56 +00:00
}
return
}
2023-12-31 12:26:56 +00:00
if found && !changed {
log.Debugf("Page %s is unchanged and cached in memory", path)
2024-01-06 16:42:08 +00:00
addHeaders(csp, mimeType, w)
w.WriteHeader(200)
w.Write(content)
return
}
2023-12-31 13:08:23 +00:00
pathParts := strings.Split(path, ".")
2023-12-31 23:38:39 +00:00
ext := pathParts[len(pathParts)-1]
mimeType = mime.TypeByExtension("." + ext)
now := time.Now()
pageCache.Set(
key,
PageContentCache{
content,
mimeType,
now,
},
cache.DefaultExpiration,
)
log.Debugf("Page %s requested from Gitea and cached in memory at %v", path, now)
2024-01-06 16:42:08 +00:00
addHeaders(csp, mimeType, w)
w.WriteHeader(200)
2023-12-31 12:26:56 +00:00
w.Write(content)
}