-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
executable file
·74 lines (61 loc) · 1.42 KB
/
admin.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
package admin
import (
"flag"
"fmt"
"net"
"net/http"
"net/url"
"os"
"time"
"labix.org/v2/mgo"
"github.com/goincremental/negroni-sessions"
"github.com/martini-contrib/encoder"
)
var (
admin = flag.String("admin", os.Getenv("ADMIN"), "")
)
type Time time.Time
func (t Time) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%d", time.Time(t).UnixNano())), nil
}
type Log struct {
OK bool
S interface{}
T Time
}
func NewLog(ok bool, s interface{}) (l Log) {
l.OK = ok
l.T = Time(time.Now())
l.S = s
return
}
type FBSubscriptionURL struct {
U string
V url.Values
}
type Status struct {
AppDomain string
Log map[string]Log
}
func Authorize(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
fmt.Printf("Invoked Auth: %v\n", r.URL)
host, _, err := net.SplitHostPort(r.RemoteAddr)
ip := net.ParseIP(host)
s := sessions.GetSession(r)
if err != nil || (s.Get("user_id") != *admin && !ip.IsLoopback()) {
http.Error(rw, "Please spare me :P", http.StatusUnauthorized)
return
}
next(rw, r)
}
func FetchStatus(s *Status, ms *mgo.Session, enc encoder.Encoder, r *http.Request) (int, []byte) {
s.Log["MongoDB"] = NewLog(true, "")
if err := ms.Ping(); err != nil {
s.Log["MongoDB"] = NewLog(false, err.Error())
}
return http.StatusOK, encoder.Must(enc.Encode(s))
}
func OK(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "text/plain")
rw.Write([]byte("OK"))
}