-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
107 lines (90 loc) · 2.85 KB
/
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
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
package main
import (
"encoding/json"
"expvar"
"log"
"net/http"
"strings"
"golang.org/x/net/context"
"time"
)
// aptServerConfig holds some global defaults for the server
var cfg struct {
CookieName string // The session cookie name for uploads
TTL time.Duration // How long to keep session alive
PreGenHook HookRunner // A hook to run before we run the genrator
PostGenHook HookRunner // A hooke to run after successful regeneration
}
var state struct {
Archive Archiver // The generator for updating the repo
SessionManager *UploadSessionManager // The session manager
Lock *Governor // Locks to ensure the repo update is atomic
getCount *expvar.Int // Download count
}
// appError is a custom error type to
// encode the HTTP status and meesage we will
// send back to a client
type appError struct {
Code int
Message []byte
Error error
}
type appHandler func(context.Context, http.ResponseWriter, *http.Request) *appError
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if e := fn(ctx, w, r); e != nil { // e is *appError, not os.Error.
if e.Code == 0 {
e.Code = http.StatusInternalServerError
}
if e.Message == nil {
e.Message = []byte(http.StatusText(e.Code))
}
log.Printf("ERROR: %v", e.Error)
sendResponse(w, e.Code, e.Message)
}
}
// NewServerResponse contructs a new repsonse to a client and can take
// a string of JSON'able object
func sendResponse(w http.ResponseWriter, code int, obj interface{}) *appError {
var err error
var j []byte
if obj != nil {
j, err = json.Marshal(obj)
if err != nil {
code = http.StatusInternalServerError
j = []byte("{\"error\": \"Failed to marshal response, " + err.Error() + "\"}")
w.WriteHeader(code)
w.Write(j)
return nil
}
} else {
j, _ = json.Marshal(http.StatusText(code))
}
w.WriteHeader(code)
w.Write(j)
return nil
}
func sendOKResponse(w http.ResponseWriter, obj interface{}) *appError {
return sendResponse(w, http.StatusOK, obj)
}
func handleWithReadLock(f appHandler, ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
state.Lock.ReadLock()
defer state.Lock.ReadUnLock()
return f(ctx, w, r)
}
func handleWithWriteLock(f appHandler, ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
state.Lock.WriteLock()
defer state.Lock.WriteUnLock()
return f(ctx, w, r)
}
// AuthorisedAdmin returns true if the web request is sufficient
// for performing administration functions
func AuthorisedAdmin(ctx context.Context, w http.ResponseWriter, r *http.Request) bool {
h := r.RemoteAddr[:strings.LastIndex(r.RemoteAddr, ":")]
if !(h == "127.0.0.1" || h == "[::1]") {
log.Printf("UNAUTHORIZED: %v %v", r.RemoteAddr, r.RequestURI)
return false
}
return true
}