2023-12-31 12:26:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-31 23:38:01 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-12-31 12:26:56 +00:00
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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
|
2023-12-31 23:38:01 +00:00
|
|
|
RequestedAt time.Time
|
2023-12-31 12:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makePageContentCacheEntry(username, path string) string {
|
|
|
|
return username + ":" + path
|
|
|
|
}
|
|
|
|
|
2023-12-31 23:38:01 +00:00
|
|
|
func serveFile(username, reponame, path, giteaUrl string, w http.ResponseWriter) {
|
2023-12-31 12:26:56 +00:00
|
|
|
// Provide a default
|
|
|
|
if path == "" {
|
|
|
|
path = "/index.html"
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
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
|
2023-12-31 23:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have to do the raw request manually because the Gitea SDK does not allow
|
|
|
|
// passing the If-Modfied-Since header.
|
|
|
|
apiUrl := fmt.Sprintf(
|
|
|
|
"%s/api/v1/repos/%s/%s/raw/%s?ref=%s",
|
|
|
|
giteaUrl,
|
|
|
|
username,
|
|
|
|
reponame,
|
|
|
|
path,
|
|
|
|
PagesBranch,
|
|
|
|
)
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", apiUrl, nil)
|
|
|
|
if found {
|
|
|
|
since := entry.(PageContentCache).RequestedAt.Format(time.RFC1123)
|
|
|
|
log.Debugf("Found %s in cache. Adding '%s' as If-Modified-Since", key, since)
|
|
|
|
req.Header.Add("If-Modified-Since", since)
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
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)
|
2023-12-31 12:26:56 +00:00
|
|
|
w.WriteHeader(404)
|
2023-12-31 23:38:01 +00:00
|
|
|
} else {
|
|
|
|
log.Debugf("Request failed but page %s is cached in memory", path)
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
w.Write(content)
|
2023-12-31 12:26:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-31 23:38:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2023-12-31 12:26:56 +00:00
|
|
|
|
2023-12-31 23:38:01 +00:00
|
|
|
log.Debugf("Gitea API request returned %d", resp.StatusCode)
|
|
|
|
if found && resp.StatusCode == 302 {
|
|
|
|
log.Debugf("Page %s is unchanged and cached in memory", path)
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
w.Write(content)
|
|
|
|
return
|
|
|
|
}
|
2023-12-31 13:08:23 +00:00
|
|
|
|
2023-12-31 23:38:01 +00:00
|
|
|
content, err = io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to get file %s/%s/%s (%s)", username, reponame, path, err)
|
|
|
|
w.WriteHeader(404)
|
|
|
|
return
|
2023-12-31 12:26:56 +00:00
|
|
|
}
|
2023-12-31 13:08:23 +00:00
|
|
|
|
2023-12-31 23:38:01 +00:00
|
|
|
pathParts := strings.Split(path, ".")
|
2023-12-31 23:38:39 +00:00
|
|
|
ext := pathParts[len(pathParts)-1]
|
2023-12-31 23:38:01 +00:00
|
|
|
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)
|
2023-12-31 12:26:56 +00:00
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
w.Write(content)
|
|
|
|
}
|