1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: non-ascii project badges (resolve #357)

chore: locally generated badges (resolve #348)
This commit is contained in:
Ferdinand Mütsch
2022-04-18 11:39:26 +02:00
parent 1a47243f70
commit 40067d252e
18 changed files with 511 additions and 280 deletions

View File

@ -4,8 +4,24 @@ import (
"encoding/json"
"github.com/muety/wakapi/config"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
const (
cacheMaxAgePattern = `max-age=(\d+)`
)
var (
cacheMaxAgeRe *regexp.Regexp
)
func init() {
cacheMaxAgeRe = regexp.MustCompile(cacheMaxAgePattern)
}
func RespondJSON(w http.ResponseWriter, r *http.Request, status int, object interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
@ -13,3 +29,16 @@ func RespondJSON(w http.ResponseWriter, r *http.Request, status int, object inte
config.Log().Request(r).Error("error while writing json response: %v", err)
}
}
func IsNoCache(r *http.Request, cacheTtl time.Duration) bool {
cacheControl := r.Header.Get("cache-control")
if strings.Contains(cacheControl, "no-cache") {
return true
}
if match := cacheMaxAgeRe.FindStringSubmatch(cacheControl); match != nil && len(match) > 1 {
if maxAge, _ := strconv.Atoi(match[1]); time.Duration(maxAge)*time.Second <= cacheTtl {
return true
}
}
return false
}