-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
138 lines (100 loc) · 2.71 KB
/
Copy pathmain.go
File metadata and controls
138 lines (100 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
type Task struct {
ID int `json:"id"`
Input string `json:"input"`
Status string `json:"status"`
Result string `json:"result"`
}
var tasks = make(map[int]Task)
func main() {
taskQueueChan := make(chan *Task)
resultQueueChan := make(chan *Task)
wg := sync.WaitGroup{}
wg.Add(4)
go processTask(&wg, taskQueueChan, resultQueueChan)
go processTask(&wg, taskQueueChan, resultQueueChan)
go processTask(&wg, taskQueueChan, resultQueueChan)
go resultListener(&wg, resultQueueChan)
mux := http.NewServeMux()
mux.HandleFunc("GET /health", health)
mux.HandleFunc("POST /tasks", createTask(taskQueueChan))
mux.HandleFunc("GET /tasks/{id}", getTask)
http.ListenAndServe(":8080", mux)
}
func health(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Level 2 running"))
}
func createTask(taskQueueChan chan<- *Task) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Input string
}
dec := json.NewDecoder(r.Body)
err := dec.Decode(&req)
if err != nil {
http.Error(w, "Invalid input format", http.StatusBadRequest)
return
}
id := len(tasks) + 1
status := "Pending"
result := ""
task := Task{
ID: id,
Input: req.Input,
Status: status,
Result: result,
}
taskQueueChan <- &task
tasks[id] = task
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode("Task" + strconv.Itoa(task.ID) + "created")
}
}
func getTask(w http.ResponseWriter, r *http.Request) {
id, nerr := strconv.Atoi(r.PathValue("id"))
if nerr != nil {
http.Error(w, nerr.Error(), http.StatusInternalServerError)
return
}
task, ok := tasks[id]
if !ok {
http.Error(w, "Task not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
r_err := json.NewEncoder(w).Encode(task)
if r_err != nil {
http.Error(w, "Task not found", http.StatusInternalServerError)
return
}
}
func processTask(wg *sync.WaitGroup, taskQueueChan <-chan *Task, resultQueueChan chan<- *Task) {
defer wg.Done()
for task := range taskQueueChan {
task.Result = strings.ToUpper(task.Input)
task.Status = "completed"
resultQueueChan <- task
time.Sleep(time.Duration(rand.Intn(400)) * time.Millisecond)
fmt.Println("Task " + strconv.Itoa(task.ID) + " processed")
}
}
func resultListener(wg *sync.WaitGroup, resultQueueChan <-chan *Task) {
defer wg.Done()
for task := range resultQueueChan {
tasks[task.ID] = *task
fmt.Println("Task " + strconv.Itoa(task.ID) + " completed. result: " + task.Result)
}
}