forked from crbrox/gridas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replyer.go
executable file
·48 lines (43 loc) · 1.26 KB
/
replyer.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
package gridas
import (
"encoding/json"
"net/http"
"path/filepath"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"github.com/crbrox/gridas/config"
"github.com/crbrox/gridas/mylog"
)
//Replyer provides the replies from the destination hosts through HTTP as a JSON document.
//The response body is encoded in base64
type Replyer struct {
Cfg *config.Config
SessionSeed *mgo.Session
}
func (r *Replyer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
base := filepath.Base(req.URL.Path)
mylog.Debug("request for response ", base)
session := r.SessionSeed.New()
defer session.Close()
session.SetMode(mgo.Eventual, true)
db := session.DB(r.Cfg.Database)
respColl := db.C(r.Cfg.ResponsesColl)
rpl := &Reply{}
mylog.Debug("searching response", base)
e := respColl.Find(bson.M{"id": base}).One(&rpl)
if e != nil {
mylog.Debug("reply not found ", base)
http.Error(w, e.Error(), http.StatusNotFound)
return
}
mylog.Debug("json marshaling response", base)
text, e := json.MarshalIndent(rpl, "", " ")
if e != nil {
mylog.Debug("error marshaling JSON ", base, e)
http.Error(w, e.Error(), http.StatusInternalServerError)
return
}
mylog.Debugf("returning JSON %q for %v", text, base)
w.Header().Set("Content-Type", "application/json")
w.Write(text)
}