fix(web-proxy): actually working metrics, misc fixes

This commit is contained in:
spiral 2022-11-23 03:24:36 +00:00
parent b2bfe93013
commit 4ea21177ec
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
2 changed files with 15 additions and 15 deletions

View File

@ -64,7 +64,7 @@ func (p ProxyHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// CORS headers // CORS headers
rw.Header().Add("Access-Control-Allow-Origin", "*") rw.Header().Add("Access-Control-Allow-Origin", "*")
rw.Header().Add("Access-Control-Request-Method", r.Method) rw.Header().Add("Access-Control-Allow-Methods", "*")
rw.Header().Add("Access-Control-Allow-Credentials", "true") rw.Header().Add("Access-Control-Allow-Credentials", "true")
rw.Header().Add("Access-Control-Allow-Headers", "Content-Type, Authorization, sentry-trace") rw.Header().Add("Access-Control-Allow-Headers", "Content-Type, Authorization, sentry-trace")
rw.Header().Add("Access-Control-Max-Age", "86400") rw.Header().Add("Access-Control-Max-Age", "86400")
@ -98,7 +98,7 @@ func logTimeElapsed(resp *http.Response) error {
"route": cleanPath(r.Host, r.URL.Path), "route": cleanPath(r.Host, r.URL.Path),
}).Observe(elapsed.Seconds()) }).Observe(elapsed.Seconds())
log.Printf("[%s %s] \"%s %s%s\" %d - %vms\n", r.Header.Get("Fly-Client-IP"), r.Header.Get("User-Agent"), r.Method, r.Host, r.URL.Path, resp.StatusCode, elapsed.Milliseconds()) log.Printf("[%s] \"%s %s%s\" %d - %vms %s\n", r.Header.Get("Fly-Client-IP"), r.Method, r.Host, r.URL.Path, resp.StatusCode, elapsed.Milliseconds(), r.Header.Get("User-Agent"))
return nil return nil
} }

View File

@ -28,28 +28,28 @@ func proxyTo(host string) *httputil.ReverseProxy {
return rp return rp
} }
var systemsRegex = regexp.MustCompile("systems/[^/]+") var systemsRegex = regexp.MustCompile("systems/[^/{}]+")
var membersRegex = regexp.MustCompile("members/[^/]+") var membersRegex = regexp.MustCompile("members/[^/{}]+")
var groupsRegex = regexp.MustCompile("groups/[^/]+") var groupsRegex = regexp.MustCompile("groups/[^/{}]+")
var switchesRegex = regexp.MustCompile("switches/[^/]+") var switchesRegex = regexp.MustCompile("switches/[^/{}]+")
var guildsRegex = regexp.MustCompile("guilds/[^/]+") var guildsRegex = regexp.MustCompile("guilds/[^/{}]+")
var messagesRegex = regexp.MustCompile("messages/[^/]+") var messagesRegex = regexp.MustCompile("messages/[^/{}]+")
func cleanPath(host, path string) string { func cleanPath(host, path string) string {
if host != "api.pluralkit.me" { if host != "api.pluralkit.me" {
return "" return ""
} }
if !(strings.HasPrefix(host, "/v2") || strings.HasPrefix(host, "/private")) { if !(strings.HasPrefix(path, "/v2") || strings.HasPrefix(path, "/private")) {
return "" return ""
} }
path = systemsRegex.ReplaceAllString(path, "{systemRef}") path = systemsRegex.ReplaceAllString(path, "systems/{systemRef}")
path = membersRegex.ReplaceAllString(path, "{memberRef}") path = membersRegex.ReplaceAllString(path, "members/{memberRef}")
path = groupsRegex.ReplaceAllString(path, "{groupRef}") path = groupsRegex.ReplaceAllString(path, "groups/{groupRef}")
path = switchesRegex.ReplaceAllString(path, "{switchRef}") path = switchesRegex.ReplaceAllString(path, "switches/{switchRef}")
path = guildsRegex.ReplaceAllString(path, "{guild_id}") path = guildsRegex.ReplaceAllString(path, "guilds/{guild_id}")
path = messagesRegex.ReplaceAllString(path, "{message_id}") path = messagesRegex.ReplaceAllString(path, "messages/{message_id}")
return path return path
} }