-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttpserver.go
More file actions
57 lines (45 loc) · 1.25 KB
/
Copy pathhttpserver.go
File metadata and controls
57 lines (45 loc) · 1.25 KB
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
package shortlinks
import (
"fmt"
"net/http"
"os"
)
type Server struct {
DB DB
Auth Auth
}
func (s Server) ListenAndServe(listen string) error {
mux := http.NewServeMux()
mux.Handle("/", indexHandler(s.DB))
mux.Handle("/_delete/", deleteHandler(s.DB, s.Auth))
mux.Handle("/_edit/", editHandler(s.DB, s.Auth))
mux.Handle("/_favicon", http.HandlerFunc(faviconHandler))
if dbd, ok := s.DB.(DBDeleted); ok {
mux.Handle("/_deleted/", deletedHandler(dbd))
}
var h http.Handler = mux
if auth := s.Auth; auth != nil {
h = auth.Wrap(h)
}
fmt.Fprintln(os.Stderr, "rw serving at", listen)
return http.ListenAndServe(listen, h)
}
func (s Server) PublicListenAndServe(listen string) error {
mux := http.NewServeMux()
mux.Handle("/", publicIndexHandler(s.DB))
mux.Handle("/_favicon", http.HandlerFunc(faviconHandler))
fmt.Fprintln(os.Stderr, "public serving at", listen)
return http.ListenAndServe(listen, mux)
}
func _500(w http.ResponseWriter, err error) {
fmt.Fprintln(os.Stderr, err)
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(500)
fmt.Fprintln(w, err)
}
func _403(w http.ResponseWriter, err error) {
fmt.Fprintln(os.Stderr, err)
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(403)
fmt.Fprintln(w, "forbidden")
}