2020-01-03 03:21:57 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/elyby/chrly/api/mojang"
|
|
|
|
"github.com/elyby/chrly/mojangtextures"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UuidsProvider interface {
|
|
|
|
GetUuid(username string) (*mojang.ProfileInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type UUIDsWorker struct {
|
2020-01-29 04:04:15 +05:30
|
|
|
Emitter
|
2020-01-03 03:21:57 +05:30
|
|
|
UUIDsProvider mojangtextures.UUIDsProvider
|
|
|
|
}
|
|
|
|
|
2020-01-29 04:04:15 +05:30
|
|
|
func (ctx *UUIDsWorker) CreateHandler() *mux.Router {
|
2020-01-03 03:21:57 +05:30
|
|
|
router := mux.NewRouter().StrictSlash(true)
|
|
|
|
router.NotFoundHandler = http.HandlerFunc(NotFound)
|
|
|
|
|
|
|
|
router.Handle("/api/worker/mojang-uuid/{username}", http.HandlerFunc(ctx.GetUUID)).Methods("GET")
|
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *UUIDsWorker) GetUUID(response http.ResponseWriter, request *http.Request) {
|
|
|
|
username := parseUsername(mux.Vars(request)["username"])
|
|
|
|
profile, err := ctx.UUIDsProvider.GetUuid(username)
|
|
|
|
if err != nil {
|
2020-01-29 04:04:15 +05:30
|
|
|
ctx.Emitter.Emit("uuids_provider:error", err) // TODO: do I need emitter here?
|
2020-01-03 03:21:57 +05:30
|
|
|
if _, ok := err.(*mojang.TooManyRequestsError); ok {
|
|
|
|
response.WriteHeader(http.StatusTooManyRequests)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Header().Set("Content-Type", "application/json")
|
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
|
|
|
result, _ := json.Marshal(map[string]interface{}{
|
|
|
|
"provider": err.Error(),
|
|
|
|
})
|
|
|
|
_, _ = response.Write(result)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if profile == nil {
|
|
|
|
response.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Header().Set("Content-Type", "application/json")
|
|
|
|
responseData, _ := json.Marshal(profile)
|
|
|
|
_, _ = response.Write(responseData)
|
|
|
|
}
|