Clean up code, remove Prometheus

This commit is contained in:
Jacob Gunther
2022-07-11 11:27:43 -05:00
parent a87b633ca3
commit 57b8b17e42
10 changed files with 98 additions and 381 deletions

View File

@@ -3,7 +3,6 @@ package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
@@ -14,26 +13,14 @@ import (
"github.com/mineatar-io/api-server/src/redis"
"github.com/mineatar-io/api-server/src/routes"
"github.com/mineatar-io/api-server/src/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/valyala/fasthttp"
)
var (
host string = "127.0.0.1"
port uint16 = 3000
prometheusHost string = "127.0.0.1"
prometheusPort uint16 = 3001
config *conf.Configuration = &conf.Configuration{}
r *redis.Redis = &redis.Redis{}
)
var (
requestCountMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "request_count",
Help: "The total number of requests received",
})
host string = "127.0.0.1"
port uint16 = 3000
config *conf.Configuration = &conf.Configuration{}
r *redis.Redis = &redis.Redis{}
)
func init() {
@@ -55,11 +42,6 @@ func init() {
if value, ok := os.LookupEnv("HOST"); ok {
host = value
prometheusHost = value
}
if value, ok := os.LookupEnv("PROM_HOST"); ok {
prometheusHost = value
}
if value, ok := os.LookupEnv("PORT"); ok {
@@ -70,17 +52,6 @@ func init() {
}
port = uint16(parsedValue)
prometheusPort = uint16(parsedValue + 1)
}
if value, ok := os.LookupEnv("PROM_PORT"); ok {
parsedValue, err := strconv.ParseUint(value, 10, 16)
if err != nil {
log.Fatal(err)
}
prometheusPort = uint16(parsedValue)
}
routes.Init(r, config)
@@ -98,19 +69,10 @@ func middleware(next fasthttp.RequestHandler) fasthttp.RequestHandler {
log.Printf("%s %s (%s) - %s\n", ctx.Method(), ctx.URI(), ctx.RemoteAddr(), ctx.UserAgent())
}
requestCountMetric.Inc()
next(ctx)
}
}
func metrics() {
log.Printf("Prometheus listening on %s:%d\n", prometheusHost, prometheusPort)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(fmt.Sprintf("%s:%d", prometheusHost, prometheusPort), nil)
}
func main() {
defer r.Close()
@@ -129,7 +91,5 @@ func main() {
log.Printf("Listening on %s:%d\n", host, port)
go metrics()
log.Fatal(fasthttp.ListenAndServe(fmt.Sprintf("%s:%d", host, port), middleware(router.Handler)))
}

View File

@@ -7,57 +7,10 @@ import (
"github.com/mineatar-io/api-server/src/util"
"github.com/mineatar-io/skin-render"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
requestFullBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "full_body_request_count",
Help: "The amount of full body requests",
})
renderFullBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "full_body_render_count",
Help: "The amount of full body renders",
})
requestFrontBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "front_body_request_count",
Help: "The amount of front body requests",
})
renderFrontBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "front_body_render_count",
Help: "The amount of front body renders",
})
requestBackBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "back_body_request_count",
Help: "The amount of back body requests",
})
renderBackBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "back_body_render_count",
Help: "The amount of back body renders",
})
requestLeftBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "left_body_request_count",
Help: "The amount of left body requests",
})
renderLeftBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "left_body_render_count",
Help: "The amount of left body renders",
})
requestRightBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "right_body_request_count",
Help: "The amount of right body requests",
})
renderRightBodyMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "right_body_render_count",
Help: "The amount of right body renders",
})
)
func FullBodyHandler(ctx *fasthttp.RequestCtx) {
requestFullBodyMetric.Inc()
user := ctx.UserValue("user").(string)
opts := util.ParseQueryParams(ctx, config.Routes.FullBody)
@@ -65,17 +18,13 @@ func FullBodyHandler(ctx *fasthttp.RequestCtx) {
uuid, ok, err := util.LookupUUID(user)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if !ok && !opts.Fallback {
ctx.SetStatusCode(http.StatusNotFound)
ctx.SetBodyString(http.StatusText(http.StatusNotFound))
util.WriteError(ctx, nil, http.StatusNotFound)
return
}
@@ -86,10 +35,7 @@ func FullBodyHandler(ctx *fasthttp.RequestCtx) {
cache, ok, err := r.GetBytes(cacheKey)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -114,10 +60,7 @@ func FullBodyHandler(ctx *fasthttp.RequestCtx) {
rawSkin, slim, err := util.GetPlayerSkin(uuid)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -132,24 +75,16 @@ func FullBodyHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered full body image for '%s'\n", uuid)
}
renderFullBodyMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -164,8 +99,6 @@ func FullBodyHandler(ctx *fasthttp.RequestCtx) {
}
func FrontBodyHandler(ctx *fasthttp.RequestCtx) {
requestFrontBodyMetric.Inc()
user := ctx.UserValue("user").(string)
opts := util.ParseQueryParams(ctx, config.Routes.FrontBody)
@@ -173,17 +106,13 @@ func FrontBodyHandler(ctx *fasthttp.RequestCtx) {
uuid, ok, err := util.LookupUUID(user)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if !ok && !opts.Fallback {
ctx.SetStatusCode(http.StatusNotFound)
ctx.SetBodyString(http.StatusText(http.StatusNotFound))
util.WriteError(ctx, nil, http.StatusNotFound)
return
}
@@ -194,10 +123,7 @@ func FrontBodyHandler(ctx *fasthttp.RequestCtx) {
cache, ok, err := r.GetBytes(cacheKey)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -222,10 +148,7 @@ func FrontBodyHandler(ctx *fasthttp.RequestCtx) {
rawSkin, slim, err := util.GetPlayerSkin(uuid)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -240,24 +163,16 @@ func FrontBodyHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered front body image for '%s'\n", uuid)
}
renderFrontBodyMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -272,8 +187,6 @@ func FrontBodyHandler(ctx *fasthttp.RequestCtx) {
}
func BackBodyHandler(ctx *fasthttp.RequestCtx) {
requestBackBodyMetric.Inc()
user := ctx.UserValue("user").(string)
opts := util.ParseQueryParams(ctx, config.Routes.BackBody)
@@ -281,17 +194,13 @@ func BackBodyHandler(ctx *fasthttp.RequestCtx) {
uuid, ok, err := util.LookupUUID(user)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if !ok && !opts.Fallback {
ctx.SetStatusCode(http.StatusNotFound)
ctx.SetBodyString(http.StatusText(http.StatusNotFound))
util.WriteError(ctx, nil, http.StatusNotFound)
return
}
@@ -302,10 +211,7 @@ func BackBodyHandler(ctx *fasthttp.RequestCtx) {
cache, ok, err := r.GetBytes(cacheKey)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -330,10 +236,7 @@ func BackBodyHandler(ctx *fasthttp.RequestCtx) {
rawSkin, slim, err := util.GetPlayerSkin(uuid)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -348,24 +251,16 @@ func BackBodyHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered back body image for '%s'\n", uuid)
}
renderBackBodyMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -380,8 +275,6 @@ func BackBodyHandler(ctx *fasthttp.RequestCtx) {
}
func LeftBodyHandler(ctx *fasthttp.RequestCtx) {
requestLeftBodyMetric.Inc()
user := ctx.UserValue("user").(string)
opts := util.ParseQueryParams(ctx, config.Routes.LeftBody)
@@ -389,17 +282,13 @@ func LeftBodyHandler(ctx *fasthttp.RequestCtx) {
uuid, ok, err := util.LookupUUID(user)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if !ok && !opts.Fallback {
ctx.SetStatusCode(http.StatusNotFound)
ctx.SetBodyString(http.StatusText(http.StatusNotFound))
util.WriteError(ctx, nil, http.StatusNotFound)
return
}
@@ -410,10 +299,7 @@ func LeftBodyHandler(ctx *fasthttp.RequestCtx) {
cache, ok, err := r.GetBytes(cacheKey)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -438,10 +324,7 @@ func LeftBodyHandler(ctx *fasthttp.RequestCtx) {
rawSkin, slim, err := util.GetPlayerSkin(uuid)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -456,24 +339,16 @@ func LeftBodyHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered left body image for '%s'\n", uuid)
}
renderLeftBodyMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -488,8 +363,6 @@ func LeftBodyHandler(ctx *fasthttp.RequestCtx) {
}
func RightBodyHandler(ctx *fasthttp.RequestCtx) {
requestRightBodyMetric.Inc()
user := ctx.UserValue("user").(string)
opts := util.ParseQueryParams(ctx, config.Routes.RightBody)
@@ -497,17 +370,13 @@ func RightBodyHandler(ctx *fasthttp.RequestCtx) {
uuid, ok, err := util.LookupUUID(user)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if !ok && !opts.Fallback {
ctx.SetStatusCode(http.StatusNotFound)
ctx.SetBodyString(http.StatusText(http.StatusNotFound))
util.WriteError(ctx, nil, http.StatusNotFound)
return
}
@@ -518,10 +387,7 @@ func RightBodyHandler(ctx *fasthttp.RequestCtx) {
cache, ok, err := r.GetBytes(cacheKey)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -546,10 +412,7 @@ func RightBodyHandler(ctx *fasthttp.RequestCtx) {
rawSkin, slim, err := util.GetPlayerSkin(uuid)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -564,24 +427,16 @@ func RightBodyHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered right body image for '%s'\n", uuid)
}
renderRightBodyMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}

View File

@@ -7,25 +7,10 @@ import (
"github.com/mineatar-io/api-server/src/util"
"github.com/mineatar-io/skin-render"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
requestFaceMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "face_request_count",
Help: "The amount of face requests",
})
renderFaceMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "face_render_count",
Help: "The amount of face renders",
})
)
func FaceHandler(ctx *fasthttp.RequestCtx) {
requestFaceMetric.Inc()
user := ctx.UserValue("user").(string)
opts := util.ParseQueryParams(ctx, config.Routes.Face)
@@ -33,17 +18,13 @@ func FaceHandler(ctx *fasthttp.RequestCtx) {
uuid, ok, err := util.LookupUUID(user)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if !ok && !opts.Fallback {
ctx.SetStatusCode(http.StatusNotFound)
ctx.SetBodyString(http.StatusText(http.StatusNotFound))
util.WriteError(ctx, nil, http.StatusNotFound)
return
}
@@ -54,10 +35,7 @@ func FaceHandler(ctx *fasthttp.RequestCtx) {
cache, ok, err := r.GetBytes(cacheKey)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -82,10 +60,7 @@ func FaceHandler(ctx *fasthttp.RequestCtx) {
rawSkin, slim, err := util.GetPlayerSkin(uuid)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -100,24 +75,16 @@ func FaceHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered face image for '%s'\n", uuid)
}
renderFaceMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}

View File

@@ -7,25 +7,10 @@ import (
"github.com/mineatar-io/api-server/src/util"
"github.com/mineatar-io/skin-render"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
requestHeadMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "head_request_count",
Help: "The amount of head requests",
})
renderHeadMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "head_render_count",
Help: "The amount of head renders",
})
)
func HeadHandler(ctx *fasthttp.RequestCtx) {
requestHeadMetric.Inc()
user := ctx.UserValue("user").(string)
opts := util.ParseQueryParams(ctx, config.Routes.Head)
@@ -33,17 +18,13 @@ func HeadHandler(ctx *fasthttp.RequestCtx) {
uuid, ok, err := util.LookupUUID(user)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if !ok && !opts.Fallback {
ctx.SetStatusCode(http.StatusNotFound)
ctx.SetBodyString(http.StatusText(http.StatusNotFound))
util.WriteError(ctx, nil, http.StatusNotFound)
return
}
@@ -54,10 +35,7 @@ func HeadHandler(ctx *fasthttp.RequestCtx) {
cache, ok, err := r.GetBytes(cacheKey)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -82,10 +60,7 @@ func HeadHandler(ctx *fasthttp.RequestCtx) {
rawSkin, slim, err := util.GetPlayerSkin(uuid)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -100,24 +75,16 @@ func HeadHandler(ctx *fasthttp.RequestCtx) {
log.Printf("Rendered head image for '%s'\n", uuid)
}
renderHeadMetric.Inc()
data, err := util.EncodePNG(render)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}

View File

@@ -1,20 +1,9 @@
package routes
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
pingRequestMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "ping_request_count",
Help: "The amount of ping requests",
})
)
func PingHandler(ctx *fasthttp.RequestCtx) {
pingRequestMetric.Inc()
ctx.SetBodyString("Pong!")
}

View File

@@ -2,22 +2,12 @@ package routes
import (
"fmt"
"log"
"net/http"
"github.com/mineatar-io/api-server/src/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
requestRawSkinMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "raw_skin_request_count",
Help: "The amount of raw skin requests",
})
)
func SkinHandler(ctx *fasthttp.RequestCtx) {
user := ctx.UserValue("user").(string)
@@ -26,17 +16,13 @@ func SkinHandler(ctx *fasthttp.RequestCtx) {
uuid, ok, err := util.LookupUUID(user)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
if !ok && !opts.Fallback {
ctx.SetStatusCode(http.StatusNotFound)
ctx.SetBodyString(http.StatusText(http.StatusNotFound))
util.WriteError(ctx, nil, http.StatusNotFound)
return
}
@@ -44,10 +30,7 @@ func SkinHandler(ctx *fasthttp.RequestCtx) {
rawSkin, _, err := util.GetPlayerSkin(uuid)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -55,10 +38,7 @@ func SkinHandler(ctx *fasthttp.RequestCtx) {
data, err := util.EncodePNG(rawSkin)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
@@ -67,8 +47,6 @@ func SkinHandler(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
}
requestRawSkinMetric.Inc()
ctx.SetContentType("image/png")
ctx.SetBody(data)
}

View File

@@ -1,41 +1,25 @@
package routes
import (
"log"
"net/http"
"github.com/mineatar-io/api-server/src/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
)
var (
uuidLookupMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "uuid_lookup_request_count",
Help: "The amount of UUID lookup requests",
})
)
func UUIDHandler(ctx *fasthttp.RequestCtx) {
user := ctx.UserValue("user").(string)
uuid, ok, err := util.LookupUUID(user)
if err != nil {
log.Println(err)
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
util.WriteError(ctx, err, http.StatusInternalServerError)
return
}
uuidLookupMetric.Inc()
if !ok {
ctx.SetStatusCode(404)
ctx.SetBodyString(http.StatusText(http.StatusNotFound))
util.WriteError(ctx, nil, http.StatusNotFound)
return
}

View File

@@ -285,3 +285,17 @@ func ParseQueryParams(ctx *fasthttp.RequestCtx, route conf.RouteConfig) *QueryPa
return response
}
func WriteError(ctx *fasthttp.RequestCtx, err error, statusCode int, body ...string) {
ctx.SetStatusCode(statusCode)
if len(body) > 0 {
ctx.SetBodyString(body[0])
} else {
ctx.SetBodyString(http.StatusText(statusCode))
}
if err != nil {
log.Println(err)
}
}