diff --git a/http/face.go b/http/face.go new file mode 100644 index 0000000..9578737 --- /dev/null +++ b/http/face.go @@ -0,0 +1,27 @@ +package http + +import ( + "net/http" + + "github.com/gorilla/mux" +) + +const defaultHash = "default" + +func (cfg *Config) Face(response http.ResponseWriter, request *http.Request) { + cfg.Logger.IncCounter("faces.request", 1) + username := parseUsername(mux.Vars(request)["username"]) + rec, err := cfg.SkinsRepo.FindByUsername(username) + var hash string + if err != nil || rec.SkinId == 0 { + hash = defaultHash + } else { + hash = rec.Hash + } + + http.Redirect(response, request, buildFaceUrl(hash), 301) +} + +func buildFaceUrl(hash string) string { + return "http://ely.by/minecraft/skin_buffer/faces/" + hash + ".png" +} diff --git a/http/face_test.go b/http/face_test.go new file mode 100644 index 0000000..e80844a --- /dev/null +++ b/http/face_test.go @@ -0,0 +1,53 @@ +package http + +import ( + "net/http/httptest" + "testing" + + "github.com/golang/mock/gomock" + testify "github.com/stretchr/testify/assert" + + "github.com/elyby/chrly/db" +) + +func TestConfig_Face(t *testing.T) { + assert := testify.New(t) + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + config, mocks := setupMocks(ctrl) + + mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil) + mocks.Log.EXPECT().IncCounter("faces.request", int64(1)) + + req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user/face.png", nil) + w := httptest.NewRecorder() + + config.CreateHandler().ServeHTTP(w, req) + + resp := w.Result() + assert.Equal(301, resp.StatusCode) + assert.Equal("http://ely.by/minecraft/skin_buffer/faces/55d2a8848764f5ff04012cdb093458bd.png", resp.Header.Get("Location")) +} + +func TestConfig_Face2(t *testing.T) { + assert := testify.New(t) + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + config, mocks := setupMocks(ctrl) + + mocks.Skins.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{"mock_user"}) + mocks.Log.EXPECT().IncCounter("faces.request", int64(1)) + + req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user/face.png", nil) + w := httptest.NewRecorder() + + config.CreateHandler().ServeHTTP(w, req) + + resp := w.Result() + assert.Equal(301, resp.StatusCode) + assert.Equal("http://ely.by/minecraft/skin_buffer/faces/default.png", resp.Header.Get("Location")) +} diff --git a/http/http.go b/http/http.go index a539a15..293d5b1 100644 --- a/http/http.go +++ b/http/http.go @@ -55,6 +55,8 @@ func (cfg *Config) CreateHandler() http.Handler { router.HandleFunc("/cloaks/{username}", cfg.Cape).Methods("GET").Name("cloaks") router.HandleFunc("/textures/{username}", cfg.Textures).Methods("GET") router.HandleFunc("/textures/signed/{username}", cfg.SignedTextures).Methods("GET") + router.HandleFunc("/skins/{username}/face", cfg.Face).Methods("GET") + router.HandleFunc("/skins/{username}/face.png", cfg.Face).Methods("GET") // Legacy router.HandleFunc("/skins", cfg.SkinGET).Methods("GET") router.HandleFunc("/cloaks", cfg.CapeGET).Methods("GET")