Добавлен обработчик NotFound

Исправлена опечатка в тесте
Разделена логика метода BuildNonElyTexturesHash
This commit is contained in:
ErickSkrauch 2016-07-06 14:45:14 +03:00
parent c4cd95cddc
commit 3d65529d2e
4 changed files with 26 additions and 3 deletions

18
lib/routes/NotFound.go Normal file
View File

@ -0,0 +1,18 @@
package routes
import (
"net/http"
"encoding/json"
)
func NotFound(w http.ResponseWriter, r *http.Request) {
json, _ := json.Marshal(map[string]string{
"status": "404",
"message": "Not Found",
"link": "http://docs.ely.by",
})
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
w.Write(json)
}

View File

@ -18,8 +18,7 @@ func ParseUsername(username string) string {
} }
func BuildNonElyTexturesHash(username string) string { func BuildNonElyTexturesHash(username string) string {
n := time.Now() hour := getCurrentHour()
hour := time.Date(n.Year(), n.Month(), n.Day(), n.Hour(), 0, 0, 0, time.UTC).Unix()
hasher := md5.New() hasher := md5.New()
hasher.Write([]byte("non-ely-" + strconv.FormatInt(hour, 10) + "-" + username)) hasher.Write([]byte("non-ely-" + strconv.FormatInt(hour, 10) + "-" + username))
@ -29,3 +28,8 @@ func BuildNonElyTexturesHash(username string) string {
func BuildKey(username string) string { func BuildKey(username string) string {
return "username:" + strings.ToLower(username) return "username:" + strings.ToLower(username)
} }
func getCurrentHour() int64 {
n := time.Now()
return time.Date(n.Year(), n.Month(), n.Day(), n.Hour(), 0, 0, 0, time.UTC).Unix()
}

View File

@ -17,6 +17,6 @@ func TestParseUsername(t *testing.T) {
func TestBuildKey(t *testing.T) { func TestBuildKey(t *testing.T) {
if BuildKey("Test") != "username:test" { if BuildKey("Test") != "username:test" {
t.Error("Function shound convert string to lover case and concatenate it with usernmae:") t.Error("Function shound convert string to lower case and concatenate it with usernmae:")
} }
} }

View File

@ -21,6 +21,7 @@ func main() {
services.Redis = client services.Redis = client
router := mux.NewRouter().StrictSlash(true) router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", routes.NotFound)
router.HandleFunc("/skins/{username}", routes.Skin).Methods("GET") router.HandleFunc("/skins/{username}", routes.Skin).Methods("GET")
router.HandleFunc("/textures/{username}", routes.Textures).Methods("GET") router.HandleFunc("/textures/{username}", routes.Textures).Methods("GET")
router.HandleFunc("/system/setSkin", routes.SetSkin).Methods("POST") // TODO: убрать этого, т.к. он стар router.HandleFunc("/system/setSkin", routes.SetSkin).Methods("POST") // TODO: убрать этого, т.к. он стар