Spaces:
Running
Running
Update api/chat.go
Browse files- api/chat.go +41 -3
api/chat.go
CHANGED
@@ -76,11 +76,49 @@ func getEnvOrDefault(key, defaultValue string) string {
|
|
76 |
return defaultValue
|
77 |
}
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
func getToken() (string, error) {
|
80 |
tokenReq := struct {
|
81 |
UUID string `json:"uuid"`
|
82 |
}{
|
83 |
-
UUID:
|
84 |
}
|
85 |
|
86 |
tokenReqBody, _ := json.Marshal(tokenReq)
|
@@ -104,7 +142,7 @@ func getToken() (string, error) {
|
|
104 |
|
105 |
func Handler(w http.ResponseWriter, r *http.Request) {
|
106 |
authToken := r.Header.Get("Authorization")
|
107 |
-
envToken :=
|
108 |
|
109 |
if envToken != "" && authToken != "Bearer "+envToken {
|
110 |
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
@@ -396,4 +434,4 @@ func main() {
|
|
396 |
if err := http.ListenAndServe(":"+port, nil); err != nil {
|
397 |
fmt.Printf("Error starting server: %v\n", err)
|
398 |
}
|
399 |
-
}
|
|
|
76 |
return defaultValue
|
77 |
}
|
78 |
|
79 |
+
func parseApiKey() (uuid, cookie string) {
|
80 |
+
apiKey := os.Getenv("APIKEY")
|
81 |
+
if apiKey == "" {
|
82 |
+
return "", ""
|
83 |
+
}
|
84 |
+
pairs := strings.Split(apiKey, ";")
|
85 |
+
for _, pair := range pairs {
|
86 |
+
parts := strings.SplitN(pair, "=", 2)
|
87 |
+
if len(parts) != 2 {
|
88 |
+
continue
|
89 |
+
}
|
90 |
+
key := strings.TrimSpace(parts[0])
|
91 |
+
value := strings.TrimSpace(parts[1])
|
92 |
+
if key == "uuid" {
|
93 |
+
uuid = value
|
94 |
+
} else if key == "cookie" {
|
95 |
+
cookie = value
|
96 |
+
}
|
97 |
+
}
|
98 |
+
return
|
99 |
+
}
|
100 |
+
|
101 |
+
func getUUID() string {
|
102 |
+
uuidVal, _ := parseApiKey()
|
103 |
+
if uuidVal != "" {
|
104 |
+
return uuidVal
|
105 |
+
}
|
106 |
+
return getEnvOrDefault("UUID", "")
|
107 |
+
}
|
108 |
+
|
109 |
+
func getAuthToken() string {
|
110 |
+
_, cookieVal := parseApiKey()
|
111 |
+
if cookieVal != "" {
|
112 |
+
return cookieVal
|
113 |
+
}
|
114 |
+
return getEnvOrDefault("AUTH_TOKEN", "")
|
115 |
+
}
|
116 |
+
|
117 |
func getToken() (string, error) {
|
118 |
tokenReq := struct {
|
119 |
UUID string `json:"uuid"`
|
120 |
}{
|
121 |
+
UUID: getUUID(),
|
122 |
}
|
123 |
|
124 |
tokenReqBody, _ := json.Marshal(tokenReq)
|
|
|
142 |
|
143 |
func Handler(w http.ResponseWriter, r *http.Request) {
|
144 |
authToken := r.Header.Get("Authorization")
|
145 |
+
envToken := getAuthToken()
|
146 |
|
147 |
if envToken != "" && authToken != "Bearer "+envToken {
|
148 |
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
434 |
if err := http.ListenAndServe(":"+port, nil); err != nil {
|
435 |
fmt.Printf("Error starting server: %v\n", err)
|
436 |
}
|
437 |
+
}
|