summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go34
1 files changed, 28 insertions, 6 deletions
diff --git a/main.go b/main.go
index 4f8501a..fd0b9b6 100644
--- a/main.go
+++ b/main.go
@@ -13,7 +13,7 @@ import (
)
// Example:
-// curl 'http://localhost:8080/v1/auth/login' -X POST -H 'Content-Type: application/json' --data-raw $'{}}\n{"king": 5'
+// curl 'http://localhost:8080/v1/auth/login' -X POST -H 'Content-Type: application/json' --data-raw $'{}}\n{"king": "key"'
const log_length = 100
@@ -38,6 +38,11 @@ func main() {
var logs [log_length]string
n := 0
+ king := "NOKING"
+ logChan := make(chan string)
+
+ go parser(logChan, &king)
+
// Define a handler function for the root path
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
jsonData, _ := json.Marshal(accessLog{
@@ -46,7 +51,7 @@ func main() {
RequestTime: time.Now().UTC(),
HttpMethod: r.Method,
})
- addRotLog(&logs, &n, string(jsonData))
+ addRotLog(&logs, &n, logChan, string(jsonData))
// Serve the index.html file from the static directory
http.StripPrefix("/", fs).ServeHTTP(w, r)
})
@@ -63,19 +68,23 @@ func main() {
var data map[string]any
if json.Unmarshal(body, &data) != nil {
- addRotLog(&logs, &n, fmt.Sprintf(`{"authRequest": %s}`, string(body)))
+ addRotLog(&logs, &n, logChan, fmt.Sprintf(`{"authRequest": %s}`, string(body)))
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
if email, ok := data["email"].(string); ok {
if rememberMe, ok := data["rememberMe"].(bool); ok {
- addRotLog(&logs, &n, fmt.Sprintf(`{"authRequest": {"email": "%s", "password": "XXXXXXXX", "loginTime": "%s", "success": false, "rememberMe": %t}}`, email, time.Now().UTC(), rememberMe))
+ addRotLog(&logs, &n, logChan, fmt.Sprintf(`{"authRequest": {"email": "%s", "password": "XXXXXXXX", "loginTime": "%s", "success": false, "rememberMe": %t}}`, email, time.Now().UTC(), rememberMe))
}
}
http.Error(w, "Forbidden", http.StatusForbidden)
})
+ http.HandleFunc("/v1/king", func(w http.ResponseWriter, r *http.Request) {
+ fmt.Fprintf(w, king)
+ })
+
// Start the server on port 8080
fmt.Println("Server is listening on port 8080...")
err := http.ListenAndServe(":8080", nil)
@@ -92,10 +101,10 @@ func createGetLogs(logs *[log_length]string) func(http.ResponseWriter, *http.Req
}
}
-func addRotLog(logs *[log_length]string, last *int, value string) {
+func addRotLog(logs *[log_length]string, last *int, parser chan string, value string) {
if strings.Contains(value, "\n") {
for _, v := range strings.Split(value, "\n") {
- addRotLog(logs, last, v)
+ addRotLog(logs, last, parser, v)
}
} else {
if *last == log_length {
@@ -103,9 +112,22 @@ func addRotLog(logs *[log_length]string, last *int, value string) {
logs[i] = logs[i+1]
}
logs[log_length-1] = value
+ parser <- value
} else {
logs[*last] = value
*last++
+ parser <- value
+ }
+ }
+}
+
+func parser(input chan string, king *string) {
+ for value := range input {
+ var data map[string]any
+ if json.Unmarshal([]byte(value), &data) == nil {
+ if k, ok := data["king"].(string); ok {
+ *king = k
+ }
}
}
}