Skip to content

Commit

Permalink
feat: hasJoined and hasPaid routes
Browse files Browse the repository at this point in the history
  • Loading branch information
net32 committed Apr 26, 2022
1 parent cad72a0 commit afec7fe
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 24 deletions.
12 changes: 12 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ func FetchProfileByName(userName string) (model.Profile, MojangResponse) {
}
return profile, data
}

func HasPaid(userName string) (string, MojangResponse) {
result := "failed"
data := UsernameToUUID(userName)
if data.Code == 200 {
result = "true"
}
if data.Code == 204 {
result = "false"
}
return result, data
}
49 changes: 36 additions & 13 deletions server/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"encoding/json"
"log"
"time"

"github.com/go-redis/redis/v8"
Expand All @@ -21,26 +23,47 @@ func redisConn() *redis.Client {
}

type CacheResponse struct {
hasCache bool
response MojangResponse
HasCache bool `json:"hascache"`
Response MojangResponse `json:"response"`
}

func HasCache(cacheKey string) CacheResponse {
func ExistsKey(cacheKey string) int64 {
rdb := redisConn()
return rdb.Exists(rdb.Context(), cacheKey).Val()
}

func SaveValue(cacheKey string, value string) bool {
rdb := redisConn()
hasCache := rdb.Exists(rdb.Context(), cacheKey).Val()
return CacheResponse{
hasCache == 1,
MojangResponse{
200,
rdb.Get(rdb.Context(), cacheKey).Val()}}
rdb.Set(rdb.Context(), cacheKey, value, 24*time.Hour).Val()
return true
}

func GetValue(cacheKey string) string {
rdb := redisConn()
return rdb.Get(rdb.Context(), cacheKey).Val()
}

func HasCache(cacheKey string) CacheResponse {
hasCache := ExistsKey(cacheKey)
value := GetValue(cacheKey)
var response MojangResponse
if hasCache == 1 {
err := json.Unmarshal([]byte(value), &response)
if err != nil {
log.Println(err, "HasCache:", cacheKey)
}
}
return CacheResponse{hasCache == 1, response}
}

func SaveCache(cacheKey string, response MojangResponse) CacheResponse {
saved := false
if response.Code == 200 {
rdb := redisConn()
rdb.Set(rdb.Context(), cacheKey, response.Json, time.Hour).Val()
saved = true
if response.Code < 500 {
data, err := json.Marshal(response)
if err != nil {
log.Println(err, "SaveCache:", cacheKey, response)
}
saved = SaveValue(cacheKey, string(data))
}
return CacheResponse{saved, response}
}
21 changes: 13 additions & 8 deletions server/mojang.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,40 @@ func UuidToProfile(uuid string, unsigned string) MojangResponse {
return mojangGet(URL)
}

func HasJoined(userName string, serverId string) MojangResponse {
URL := SESSION_URL + fmt.Sprintf("session/minecraft/hasJoined?username=%s&serverId=%s", userName, serverId)
return mojangGet(URL)
}

func BlockedServers() MojangResponse {
URL := SESSION_URL + "blockedservers"
return mojangGet(URL)
}

type MojangResponse struct {
Code int
Json string
Code int `json:"code"`
Json string `json:"json"`
}

func mojangGet(URL string) MojangResponse {
key := URL
cache := HasCache(key)
if cache.hasCache {
return cache.response
if cache.HasCache {
return cache.Response
}
resp, err := http.Get(URL)
return SaveCache(key, mojangResponse(resp, err)).response
return SaveCache(key, mojangResponse(resp, err)).Response
}

func mojangPost(URL string, jsonData []byte) MojangResponse {
hashData := hex.EncodeToString(md5.New().Sum(jsonData))
key := URL + hashData
cache := HasCache(key)
if cache.hasCache {
return cache.response
if cache.HasCache {
return cache.Response
}
resp, err := http.Post(URL, "application/json", bytes.NewBuffer(jsonData))
return SaveCache(key, mojangResponse(resp, err)).response
return SaveCache(key, mojangResponse(resp, err)).Response
}

func mojangResponse(resp *http.Response, err error) MojangResponse {
Expand Down
16 changes: 13 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const VERSION = "1.0.2"
var name, author, url = "mojang-redis", "NeT32", "https://github.com/net32/mojang-redis"

func InitServer() {
log.Println("Starting", name, "developed by", author)
log.Println("API Version", VERSION)
log.Println("GitHub >", url)
log.Printf("Starting %s developed by %s.", name, author)
log.Printf("API Version: %s", VERSION)
log.Printf("GitHub: %s", url)
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.GET("/", func(c *gin.Context) {
Expand Down Expand Up @@ -54,10 +54,20 @@ func InitServer() {
}
writeJsonResponse(c, UuidToProfile(uuid, unsigned))
})
r.GET("/session/minecraft/hasJoined", func(c *gin.Context) {
userName, _ := c.GetQuery("username")
serverId, _ := c.GetQuery("serverId")
writeJsonResponse(c, HasJoined(userName, serverId))
})
r.GET("/blockedservers", func(c *gin.Context) {
response := BlockedServers()
c.String(response.Code, response.Json)
})
r.GET("/auth/haspaid/:name", func(c *gin.Context) {
userName := c.Params.ByName("name")
response, _ := HasPaid(userName)
c.String(200, response)
})
addr := ":" + GetEnv("PORT", "8080")
log.Println("Listen: " + addr)
r.Run(addr)
Expand Down

0 comments on commit afec7fe

Please sign in to comment.