Skip to content

Commit 01283f0

Browse files
committed
Merge branch 'develop'
2 parents d27ef1b + ecef5c6 commit 01283f0

7 files changed

+64
-46
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ RUN apk --no-cache add ca-certificates
1212
COPY --from=builder /go/bin/imgd /imgd
1313
COPY config.example.gcfg /config.gcfg
1414
ENTRYPOINT ./imgd
15-
LABEL Name=imgd Version=2.10.0
15+
LABEL Name=imgd Version=2.11.0
1616
EXPOSE 8000

config.example.gcfg

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ url = https://minotar.net/
1111
# The duration, in seconds we should store item in our cache. Default: 48 hrs
1212
ttl = 172800
1313

14+
[minecraft]
15+
# User Agent to use with each HTTP request
16+
useragent = "minotar/imgd (https://github.com/minotar/imgd)"
17+
# SessionServerURL is the address where we can append a UUID and get back a SessionProfileResponse (UUID, Username and Properties/Textures)
18+
sessionserverurl = https://sessionserver.mojang.com/session/minecraft/profile/
19+
# ProfileURL is the address where we can append a Username and get back a APIProfileResponse (UUID and Username)
20+
profileurl = https://api.mojang.com/users/profiles/minecraft/
21+
1422
[redis]
1523
# If you're using Redis caching, you should fill this section out.
1624
# Otherwise, don't worry about it

configuration.go

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ type Configuration struct {
2424
Ttl int
2525
}
2626

27+
Minecraft struct {
28+
UserAgent string
29+
SessionServerURL string
30+
ProfileURL string
31+
}
32+
2733
Redis struct {
2834
Address string
2935
Auth string

http.go

+30-42
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
"strconv"
77
"strings"
88

9-
"github.com/prometheus/client_golang/prometheus"
10-
"github.com/prometheus/client_golang/prometheus/promhttp"
9+
"github.com/minotar/minecraft"
1110

1211
"github.com/gorilla/mux"
13-
"github.com/minotar/minecraft"
12+
"github.com/prometheus/client_golang/prometheus"
13+
"github.com/prometheus/client_golang/prometheus/promhttp"
1414
)
1515

1616
type Router struct {
@@ -221,50 +221,38 @@ func fetchSkin(username string) *mcSkin {
221221
hasTimer.ObserveDuration()
222222
stats.MissCache()
223223

224-
// Everyone loves nested if statements, right?
225224
var skin minecraft.Skin
226225
stats.APIRequested("GetUUID")
227-
uuid, err := minecraft.NormalizePlayerForUUID(username)
228-
if err != nil && err.Error() == "unable to GetAPIProfile: user not found" {
229-
log.Debugf("Failed UUID lookup: %s (%s)", username, err.Error())
230-
skin, _ = minecraft.FetchSkinForSteve()
231-
stats.Errored("UnknownUser")
232-
// Don't return yet to ensure we cache the failure
233-
} else {
234-
var catchErr error
235-
// Either no error, or there is one (eg. rate limit or network etc.), but they do possibly still exist
236-
if err != nil && err.Error() == "unable to GetAPIProfile: rate limited" {
237-
log.Noticef("Failed UUID lookup: %s (%s)", username, err.Error())
226+
uuid, err := mcClient.NormalizePlayerForUUID(username)
227+
if err != nil {
228+
switch errorMsg := err.Error(); errorMsg {
229+
230+
case "unable to GetAPIProfile: user not found":
231+
log.Debugf("Failed UUID lookup: %s (%s)", username, errorMsg)
232+
stats.Errored("UnknownUser")
233+
234+
case "unable to GetAPIProfile: rate limited":
235+
log.Noticef("Failed UUID lookup: %s (%s)", username, errorMsg)
238236
stats.Errored("LookupUUIDRateLimit")
239-
catchErr = err
240-
} else if err != nil {
241-
// Other generic issues with looking up UUID, but still worth trying S3
242-
log.Infof("Failed UUID lookup: %s (%s)", username, err.Error())
237+
238+
default:
239+
log.Infof("Failed UUID lookup: %s (%s)", username, errorMsg)
243240
stats.Errored("LookupUUID")
244-
catchErr = err
245-
} else {
246-
// We have a UUID, so let's get a skin!
247-
sPTimer := prometheus.NewTimer(getDuration.WithLabelValues("SessionProfile"))
248-
skin, catchErr = minecraft.FetchSkinUUID(uuid)
249-
sPTimer.ObserveDuration()
250-
if catchErr != nil {
251-
log.Noticef("Failed Skin SessionProfile: %s (%s)", username, catchErr.Error())
252-
stats.Errored("SkinSessionProfile")
253-
}
241+
254242
}
255-
if catchErr != nil {
256-
// Let's fallback to S3 and try and serve at least an old skin...
257-
s3Timer := prometheus.NewTimer(getDuration.WithLabelValues("S3"))
258-
skin, err = minecraft.FetchSkinUsernameS3(username)
259-
s3Timer.ObserveDuration()
260-
if err != nil {
261-
log.Debugf("Failed Skin S3: %s (%s)", username, err.Error())
262-
// Well, looks like they don't exist after all.
263-
skin, _ = minecraft.FetchSkinForSteve()
264-
stats.Errored("FallbackSteve")
265-
} else {
266-
stats.Errored("FallbackUsernameS3")
267-
}
243+
244+
skin, _ = minecraft.FetchSkinForSteve()
245+
stats.Errored("FallbackSteve")
246+
} else {
247+
// We have a UUID, so let's get a skin!
248+
sPTimer := prometheus.NewTimer(getDuration.WithLabelValues("SessionProfile"))
249+
skin, err = mcClient.FetchSkinUUID(uuid)
250+
sPTimer.ObserveDuration()
251+
if err != nil {
252+
log.Noticef("Failed Skin SessionProfile: %s (%s)", username, err.Error())
253+
stats.Errored("SkinSessionProfile")
254+
skin, _ = minecraft.FetchSkinForSteve()
255+
stats.Errored("FallbackSteve")
268256
}
269257
}
270258

main.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"runtime"
88

9+
"github.com/minotar/minecraft"
10+
911
"github.com/gorilla/mux"
1012
"github.com/op/go-logging"
1113
)
@@ -16,12 +18,13 @@ const (
1618
MinWidth = uint(8)
1719
MaxWidth = uint(300)
1820

19-
ImgdVersion = "2.10.0"
21+
ImgdVersion = "2.11.0"
2022
)
2123

2224
var (
2325
config = &Configuration{}
2426
cache Cache
27+
mcClient *minecraft.Minecraft
2528
stats *StatusCollector
2629
signalHandler *SignalHandler
2730
)
@@ -46,6 +49,17 @@ func setupCache() {
4649
}
4750
}
4851

52+
func setupMcClient() {
53+
mcClient = &minecraft.Minecraft{
54+
Client: minecraft.NewHTTPClient(),
55+
UserAgent: config.Minecraft.UserAgent,
56+
UUIDAPI: minecraft.UUIDAPI{
57+
SessionServerURL: config.Minecraft.SessionServerURL,
58+
ProfileURL: config.Minecraft.ProfileURL,
59+
},
60+
}
61+
}
62+
4963
func setupLog(logBackend *logging.LogBackend) {
5064
logging.SetBackend(logBackend)
5165
logging.SetFormatter(logging.MustStringFormatter(format))
@@ -81,5 +95,6 @@ func main() {
8195
setupConfig()
8296
setupLog(logBackend)
8397
setupCache()
98+
setupMcClient()
8499
startServer()
85100
}

main_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestSetup(t *testing.T) {
3838
setupConfig()
3939
setupLog(logBackend)
4040
setupCache()
41+
setupMcClient()
4142
}
4243

4344
func TestRenders(t *testing.T) {

wercker.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ release:
4040
# Should ideally use some intelligence to get version, eg $(./imgd -version) would be cool
4141
- github-create-release:
4242
token: $GITHUB_TOKEN
43-
tag: 2.10.0
44-
title: imgd 2.10.0
43+
tag: 2.11.0
44+
title: imgd 2.11.0
4545
draft: true
4646

4747
- github-upload-asset:

0 commit comments

Comments
 (0)