-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_server.go
76 lines (59 loc) · 1.62 KB
/
rest_server.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
package main
// HTTP REST server for all client interactions
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
)
var shutdown chan bool = make(chan bool, 1)
var restServer *RestServer
type RestServer struct {
PrettyPrint bool
}
// Authenticate
func (this *RestServer) auth(r *http.Request) bool {
// @todo Implement
return true
}
// Not authorized
func (this *RestServer) notAuthorized(w http.ResponseWriter) {
w.WriteHeader(http.StatusUnauthorized)
}
// Not found
func (this *RestServer) notFound(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
}
// Start
func (this *RestServer) start() {
go func() {
// New router
router := httprouter.New()
// Debug handlers
if conf.HttpDebug {
log.Warn("HTTP debug endpoints are ON")
// File locator
router.GET("/v1/debug/file-locator/shards", GetDebugFileLocatorShards)
router.GET("/v1/debug/file-locator/shard-locations", GetDebugFileLocatorShardLocations)
// Gossip
router.GET("/v1/debug/gossip/nodes", GetDebugGossipNodes)
// Block
router.POST("/v1/debug/block/allocate", PostDebugBlockAllocate)
router.PUT("/v1/debug/block/persist", PutDebugBlockPersist)
}
// File
router.POST("/v1/file", PostFile)
router.GET("/v1/file", GetFile)
// Local calls
router.GET("/v1/local/file", GetLocalFile) // Local file will attempt to load file from this server
// Start server
log.Infof("Starting REST HTTP server on port TCP/%d", conf.HttpPort)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", conf.HttpPort), router))
}()
}
func newRestServer() *RestServer {
o := &RestServer{
PrettyPrint: true,
}
o.start()
return o
}