Allow CORS in ranking API

This commit is contained in:
Eduard Urbach 2018-10-27 11:06:36 +09:00
parent 252d021d90
commit 5a592c505b

View File

@ -10,6 +10,7 @@ import (
// Ranking returns the ranking information for the character via the API.
func Ranking(ctx *aero.Context) string {
// Check character ID
id := ctx.Get("id")
_, err := arn.GetCharacter(id)
@ -17,17 +18,24 @@ func Ranking(ctx *aero.Context) string {
return ctx.Error(http.StatusNotFound, "Character not found", err)
}
// Sort characters
characters := arn.FilterCharacters(func(character *arn.Character) bool {
return !character.IsDraft
})
arn.SortCharactersByLikes(characters)
// Allow CORS
ctx.Response().Header().Set("Access-Control-Allow-Origin", "*")
// Return ranking
for index, character := range characters {
if character.ID == id {
return strconv.Itoa(index + 1)
}
}
// If the ID wasn't found for some reason,
// return an empty string.
return ""
}