-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher.go
executable file
·78 lines (67 loc) · 1.84 KB
/
dispatcher.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 camcloud
import (
"encoding/json"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"fmt"
)
type config struct {
HttpPort int `json:"http_port"`
Suid string `json:"su_id"`
ClusterIpAddress string `json:"cluster_ip_address"`
DBInserterDataChannelDepth int `json:"db_inserter_data_channel_depth"`
DBQueryChannelDepth int `json:"db_query_channel_depth"`
}
var conf config
func commonWrapper(f func(http.ResponseWriter, *http.Request) interface{}) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
u := f(w, r)
b, err := json.Marshal(u)
check(err)
w.Write(b)
}
}
func Main() {
/* read configuration */
file, err := os.Open("config.json")
defer file.Close()
if err != nil {
/* configure not found */
log.Printf("Unable to read config.json. Setting default parameters.")
conf.HttpPort = 80
conf.ClusterIpAddress = "127.0.0.1"
conf.Suid = "15337"
conf.DBInserterDataChannelDepth = 512
} else {
decoder := json.NewDecoder(file)
err = decoder.Decode(&conf)
if err != nil {
log.Printf("Error reading config.json: %s", err)
return
}
}
initializeDBAccessor()
router := mux.NewRouter()
// Each of these handler funcs would be called inside a go routine
router.HandleFunc("/u", commonWrapper(recordUploader)).Methods("POST")
router.HandleFunc("/q/{cam}/{vc}/{vt}/{vp}/{tss}/{tse}",
commonWrapper(queryRecords)).Methods("GET")
http.Handle("/", router)
log.Println(fmt.Sprintf("Listening at port %d ...", conf.HttpPort))
http.ListenAndServe(fmt.Sprintf(":%d", conf.HttpPort), router)
log.Println("Done! Exiting...")
uninitializeDBAccessor()
}
/*
db_inserter_data_channel <- record
status := <- result_chan
if status != "received" {
log.Printf("%s", status)
}
if counter > 500 {
break
}
*/