-
Notifications
You must be signed in to change notification settings - Fork 8
/
WebService.go
70 lines (60 loc) · 14.9 KB
/
WebService.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
package main
import "fmt"
import "io/ioutil"
import "net/http"
import "strconv"
import "encoding/json"
import "encoding/base64"
const indexPageD = "PCFkb2N0eXBlIGh0bWw+DQo8aHRtbD4NCg0KPGhlYWQ+DQoJPHRpdGxlPldlYlRvcDwvdGl0bGU+DQoJPG1ldGEgbmFtZT0idmlld3BvcnQiIGNvbnRlbnQ9IndpZHRoPWRldmljZS13aWR0aCI+DQoJPGxpbmsgcmVsPSJzdHlsZXNoZWV0IiBocmVmPSJodHRwczovL25ldGRuYS5ib290c3RyYXBjZG4uY29tL2Jvb3Rzd2F0Y2gvMy4wLjAvc2xhdGUvYm9vdHN0cmFwLm1pbi5jc3MiPg0KCTxzY3JpcHQgdHlwZT0idGV4dC9qYXZhc2NyaXB0IiBzcmM9Imh0dHBzOi8vYWpheC5nb29nbGVhcGlzLmNvbS9hamF4L2xpYnMvanF1ZXJ5LzIuMC4zL2pxdWVyeS5taW4uanMiPjwvc2NyaXB0Pg0KCTxzY3JpcHQgdHlwZT0idGV4dC9qYXZhc2NyaXB0IiBzcmM9Imh0dHBzOi8vbmV0ZG5hLmJvb3RzdHJhcGNkbi5jb20vYm9vdHN0cmFwLzMuMS4xL2pzL2Jvb3RzdHJhcC5taW4uanMiPjwvc2NyaXB0Pg0KCTxzY3JpcHQgdHlwZT0idGV4dC9qYXZhc2NyaXB0IiBzcmM9Imh0dHA6Ly9zbW9vdGhpZWNoYXJ0cy5vcmcvc21vb3RoaWUuanMiPjwvc2NyaXB0Pg0KCTxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyI+DQoJCS8qIFNwYWNlIG91dCBjb250Z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sLWZvb3RlciI+UHJvY2VzcyBsaXN0IDwvZGl2Pg0KCQk8dGFibGUgY2xhc3M9InRhYmxlIHRhYmxlLWJvcmRlcmVkIHRhYmxlLWNvbmRlbnNlZCB0YWJsZS1ob3ZlciB0YWJsZS1zdHJpcGVkIj4NCgkJCTx0aGVhZD4NCgkJCQk8dHI+DQoJCQkJCTx0aD5QaWQ8L3RoPg0KCQkJCQk8dGg+UHJvY2VzcyBuYW1lPC90aD4NCgkJCQkJPHRoPlVzZXIgaWQ8L3RoPg0KCQkJCQk8dGg+Q3B1IDxzcGFuIG9uY2xpY2s9InN3aXRjaFVzYWdlKCkiIGlkPSJjcHUtbWludW0iPj49MDwvc3Bhbj4lPC90aD4NCgkJCQkJPHRoPk1lbSBNYi48L3RoPg0KCQkJCTwvdHI+DQoJCQk8L3RoZWFkPg0KCQkJPHRib2R5IGlkPSJwcm9jZXNzLWxpc3QtdGFibGUiPiA8L3Rib2R5Pg0KCQk8L3RhYmxlPg0KCQk8ZGl2IGNsYXNzPSJwYW5lbC1ib2R5IiBkYXRhLWRzLWVkaXRtYXJrPSJ0cnVlIj48L2Rpdj4NCgk8L2Rpdj4NCjwvYm9keT4="
//main service class
type TopJsonService struct {
requestSelector RequestSelector
jsonFabric JsonFabric
}
//start and init service
func (serviceState *TopJsonService) Start(listenPort int) error {
serviceState.requestSelector = RequestSelector{}
serviceState.requestSelector.Init()
fmt.Println(":" + strconv.Itoa(listenPort))
http.HandleFunc("/", serviceState.Redirect)
http.HandleFunc("/webtop", serviceState.ServeHTTP)
http.HandleFunc("/index.html", serviceState.ServePage)
http.HandleFunc("/samplejson", serviceState.ReturnDummyReq)
retVal := http.ListenAndServe(":"+strconv.Itoa(listenPort), nil)
return retVal
}
//redirect all the wrong unplanned queries to index
func (service *TopJsonService) Redirect(responseWriter http.ResponseWriter, request *http.Request) {
http.Redirect(responseWriter, request, "/index.html", 301)
}
//serve http responce in different thread
func (service *TopJsonService) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
if reqData, err := service.jsonFabric.ProduceJsonRequest(request); err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
} else {
service.requestSelector.Dispatch(reqData, responseWriter, request)
}
}
//serve main page request
func (service *TopJsonService) ServePage(responseWriter http.ResponseWriter, request *http.Request) {
responseWriter.Header().Set("Content-Type: text/html", "*")
content, err := ioutil.ReadFile("index.html")
if err != nil {
val, _ := base64.StdEncoding.DecodeString(indexPageD)
responseWriter.Write(val)
return
}
responseWriter.Write(content)
}
func (service *TopJsonService) ReturnDummyReq(responseWriter http.ResponseWriter, request *http.Request) {
//this is service is not need lock
serviceState := KillRequest{BasicRequest{6}, 87}
js, err := json.Marshal(serviceState)
if err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}
responseWriter.Header().Set("Content-Type", "application/json")
responseWriter.Write(js)
return
}