-
Notifications
You must be signed in to change notification settings - Fork 0
/
flusher_http.go
113 lines (99 loc) · 2.82 KB
/
flusher_http.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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/mux"
"io/ioutil"
"net/http"
"net/url"
"os"
"runtime"
)
var (
router = mux.NewRouter()
http_configuration = new(HttpConfiguration)
)
type HttpConfiguration struct {
ServerPort int
ProccessCount int
}
func FlushRequest(w http.ResponseWriter, r *http.Request) (interface{}, *errorHandler) {
body, err_body := ioutil.ReadAll(r.Body)
if err_body != nil {
return nil, &errorHandler{errors.New(err_body.Error()), "Unable to parse the request", 200006}
}
var fl = new(Flusher)
err_marsh := json.Unmarshal(body, &fl)
if err_marsh != nil {
return nil, &errorHandler{err_marsh, "Unable to marshal the json request", 200005}
}
d, err_parse := parseJsonData(string(body))
if err_parse != nil {
return nil, &errorHandler{err_parse.Error, err_parse.Message, 200005}
}
objects := d["objects"].([]interface{})
var urls []string
for _, v := range objects {
urls = append(urls, v.(string))
}
purge_type := d["type"].(string)
domain := d["domain"].(string)
action := d["action"].(string)
res, err := fl.FlushRequest(urls, purge_type, domain, action)
return res, err
}
func FlushStatus(w http.ResponseWriter, r *http.Request) (interface{}, *errorHandler) {
params, parse_err := url.ParseQuery(r.URL.RawQuery)
if parse_err != nil {
return nil, &errorHandler{parse_err, parse_err.Error(), 200004}
}
stat_id := params.Get("stat_id")
if stat_id == "" {
return nil, &errorHandler{errors.New("Unable to get the purge stat url"), "Unable to get the purge stat url", 200003}
}
var fl = new(FlusherStatus)
res, err := fl.FlushStatus(stat_id)
if err != nil {
return nil, &errorHandler{err.Error, err.Message, 200002}
}
return res, nil
}
func FlushQueueStatus(w http.ResponseWriter, r *http.Request) (interface{}, *errorHandler) {
var fl = new(Flusher)
res, err := fl.FlushQueueStatus()
if err != nil {
return nil, &errorHandler{err.Error, err.Message, 200001}
}
return res, nil
}
/* run the server */
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("recover in action: ", r)
os.Exit(1)
}
}()
/* Set the configuration */
conf_err := getConfig("http", http_configuration)
if conf_err.Error != nil {
os.Exit(1)
}
if http_configuration.ProccessCount > 1 {
goMaxProcs := os.Getenv("GOMAXPROCS")
if goMaxProcs == "" {
runtime.GOMAXPROCS(runtime.NumCPU())
}
}
port := http_configuration.ServerPort
router.Handle("/flush", handler(FlushRequest)).Methods("POST")
router.Handle("/flush_status", handler(FlushStatus)).Methods("GET")
router.Handle("/flush_queue_status", handler(FlushQueueStatus)).Methods("GET")
http.Handle("/", router)
fmt.Println(fmt.Sprintf("Server is listening on port %d", port))
err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), router)
if err != nil {
panic(err)
}
}