-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (59 loc) · 1.3 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
)
var req int
var messages = make(chan interface{}, 10)
func main() {
for i := 0; i < 4; i++ {
go worker(messages, i+1)
}
r := mux.NewRouter()
r.HandleFunc("/", rootHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
fmt.Println("starting golang server at port:", port)
err := http.ListenAndServe("0.0.0.0:"+port, r)
if err != nil {
log.Fatal("Something went wrong starting the server", err)
}
}
type Person struct {
Name string `json:"name"`
Purpose string `json:"purpose"`
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
x := Person{
Name: "tawsif",
Purpose: "testing do app platform",
}
bt, err := json.Marshal(x)
if err != nil {
fmt.Println("json marshal error", err.Error())
}
// req++
// go func(reqx int) {
// for i := 0; i < 500; i++ {
// messages <- fmt.Sprintf("Task: %d of req %d", i, reqx)
// }
// }(req)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(bt)
}
func worker(c chan interface{}, workerNo int) {
fmt.Println("worker:", workerNo, " Ready")
for {
time.Sleep(time.Millisecond * 20)
fmt.Println("worker:", workerNo, " received ", <-c)
}
}