@@ -5,14 +5,17 @@ import (
5
5
"encoding/base64"
6
6
"encoding/json"
7
7
"fmt"
8
- "github.com/google/uuid"
8
+ uuid2 "github.com/google/uuid"
9
9
"github.com/gorilla/mux"
10
10
"github.com/joho/godotenv"
11
11
"image/png"
12
12
"mymcuu.id/api/mojang"
13
13
"net/http"
14
+ "os"
14
15
)
15
16
17
+ var CORS = "https://mymcuu.id"
18
+
16
19
type JsonError struct {
17
20
Error string `json:"error"`
18
21
}
@@ -23,7 +26,7 @@ type UUIDResponse struct {
23
26
Avatar string `json:"avatar"`
24
27
}
25
28
func setupResponse (w * http.ResponseWriter , req * http.Request ) {
26
- (* w ).Header ().Set ("Access-Control-Allow-Origin" , "https://mymcuu.id" )
29
+ (* w ).Header ().Set ("Access-Control-Allow-Origin" , CORS )
27
30
(* w ).Header ().Set ("Access-Control-Allow-Methods" , "GET, OPTIONS" )
28
31
(* w ).Header ().Set ("Access-Control-Allow-Headers" , "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization" )
29
32
}
@@ -41,8 +44,8 @@ func GetUUIDFromUsername(w http.ResponseWriter, r *http.Request) {
41
44
}
42
45
vars := mux .Vars (r )
43
46
username := vars ["username" ]
44
- if ok , _ := HasData (r .Context (), username ); ok {
45
- resp , err := GetData (r .Context (), username )
47
+ if ok , _ := HasDataFromUsername (r .Context (), username ); ok {
48
+ resp , err := GetDataFromUsername (r .Context (), username )
46
49
if err != nil {
47
50
fmt .Fprintf (w , ErrorJson (err .Error ()))
48
51
return
@@ -62,7 +65,7 @@ func GetUUIDFromUsername(w http.ResponseWriter, r *http.Request) {
62
65
headImage , err := mojang .GetHeadFromUUID (resp .UUID )
63
66
buf := new (bytes.Buffer )
64
67
png .Encode (buf , * headImage )
65
- withDashes , err := uuid .Parse (resp .UUID )
68
+ withDashes , err := uuid2 .Parse (resp .UUID )
66
69
if err != nil {
67
70
fmt .Fprintf (w , ErrorJson (err .Error ()))
68
71
return
@@ -77,7 +80,7 @@ func GetUUIDFromUsername(w http.ResponseWriter, r *http.Request) {
77
80
return
78
81
}
79
82
jsonResponse := string (bytes )
80
- err = StoreData (r .Context (), username , jsonResponse )
83
+ err = StoreData (r .Context (), username , withDashes . String (), jsonResponse )
81
84
if err != nil {
82
85
fmt .Printf ("failed to save %s to cache" , username )
83
86
return
@@ -101,14 +104,74 @@ func GetHeadFromUUID(w http.ResponseWriter, r *http.Request){
101
104
png .Encode (w , * resp )
102
105
}
103
106
107
+ func GetUsernameFromUUID (w http.ResponseWriter , r * http.Request ) {
108
+ setupResponse (& w , r )
109
+ if (* r ).Method == "OPTIONS" {
110
+ return
111
+ }
112
+ vars := mux .Vars (r )
113
+ uuid := vars ["uuid" ]
114
+ parsedUUID , err := uuid2 .Parse (uuid )
115
+ if err != nil {
116
+ fmt .Fprintf (w , ErrorJson ("invalid uuid" ))
117
+ return
118
+ }
119
+ if ok , _ := HasDataFromUUID (r .Context (), parsedUUID .String ()); ok {
120
+ resp , err := GetDataFromUUID (r .Context (), parsedUUID .String ())
121
+ if err != nil {
122
+ fmt .Fprintf (w , ErrorJson (err .Error ()))
123
+ return
124
+ }
125
+ if resp != nil {
126
+ w .WriteHeader (http .StatusOK )
127
+ fmt .Fprintf (w , * resp )
128
+ return
129
+ }
130
+ }
131
+ profile , err := mojang .GetProfileFromUUID (uuid )
132
+ w .Header ().Set ("Content-Type" , "application/json" )
133
+ if err != nil {
134
+ fmt .Fprintf (w , ErrorJson (err .Error ()))
135
+ return
136
+ }
137
+ headImage , err := mojang .GetHeadFromProfile (* profile )
138
+ buf := new (bytes.Buffer )
139
+ png .Encode (buf , * headImage )
140
+ if err != nil {
141
+ fmt .Fprintf (w , ErrorJson (err .Error ()))
142
+ return
143
+ }
144
+ bytes , err := json .Marshal (UUIDResponse {
145
+ UUID : parsedUUID .String (),
146
+ Username : profile .Name ,
147
+ Avatar : fmt .Sprintf ("data:image/png;base64,%s" , base64 .StdEncoding .EncodeToString (buf .Bytes ())),
148
+ })
149
+ if err != nil {
150
+ fmt .Fprintf (w , ErrorJson (err .Error ()))
151
+ return
152
+ }
153
+ jsonResponse := string (bytes )
154
+ err = StoreData (r .Context (), profile .Name , parsedUUID .String (), jsonResponse )
155
+ if err != nil {
156
+ fmt .Printf ("failed to save %s to cache" , profile .Name )
157
+ return
158
+ }
159
+ w .WriteHeader (http .StatusOK )
160
+ fmt .Fprintf (w , jsonResponse )
161
+ }
162
+
104
163
func main (){
105
164
err := godotenv .Load ()
106
165
if err != nil {
107
166
//log.Fatal("Error loading .env file")
108
167
}
168
+ if len (os .Getenv ("CORS" )) > 0 {
169
+ CORS = os .Getenv ("CORS" )
170
+ }
109
171
SetupRedis ()
110
172
r := mux .NewRouter ()
111
173
r .HandleFunc ("/username/{username}" , GetUUIDFromUsername )
174
+ r .HandleFunc ("/uuid/{uuid}" , GetUsernameFromUUID )
112
175
r .HandleFunc ("/head/{uuid}" , GetHeadFromUUID )
113
176
http .Handle ("/" , r )
114
177
http .ListenAndServe (":8080" , nil )
0 commit comments