-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
127 lines (103 loc) · 2.79 KB
/
server_test.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package rmsgo_test
import (
"bytes"
"log"
"net/http"
"time"
"github.com/cvanloo/rmsgo"
)
// ExampleRegister demonstrates how to register the remote storage endpoints
// to a serve mux.
func ExampleRegister() {
const (
remoteRoot = "/storage/"
storageRoot = "/var/rms/storage/"
)
// [!] TODO: Use a real file
persistFile := &bytes.Buffer{}
// Restore server state at startup
err := rmsgo.Load(persistFile)
if err != nil {
log.Fatal(err)
}
err = rmsgo.Configure(remoteRoot, storageRoot)
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
// TODO: Other mux.Handle setup
rmsgo.Register(mux)
http.ListenAndServe(":8080", mux) // [!] TODO: Use TLS
// Persist server state at shutdown
err = rmsgo.Persist(persistFile)
if err != nil {
log.Fatal(err)
}
}
// Alternatively, the endpoints can be registered to the http.DefaultServeMux
// by passing nil to Register.
func ExampleRegister_usingDefaultServeMux() {
const (
remoteRoot = "/storage/"
storageRoot = "/var/rms/storage/"
)
// [!] TODO: Use a real file
persistFile := &bytes.Buffer{}
// Restore server state at startup
err := rmsgo.Load(persistFile)
if err != nil {
log.Fatal(err)
}
err = rmsgo.Configure(remoteRoot, storageRoot)
if err != nil {
log.Fatal(err)
}
rmsgo.Register(nil)
http.ListenAndServe(":8080", nil) // [!] TODO: Use TLS
// Persist server state at shutdown
err = rmsgo.Persist(persistFile)
if err != nil {
log.Fatal(err)
}
}
// Configure accepts a variable number of Option arguments.
// These can be used to overwrite the default configuration, e.g., to configure
// CORS, setup authentication, additional middleware, and more.
func ExampleOptions() {
const (
remoteRoot = "/storage/"
storageRoot = "/var/rms/storage/"
)
err := rmsgo.Configure(remoteRoot, storageRoot,
rmsgo.WithErrorHandler(func(err error) {
log.Panicf("remote storage: unhandled error: %v", err)
}),
rmsgo.WithMiddleware(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
lrw := rmsgo.NewLoggingResponseWriter(w)
// [!] Pass request on to remote storage server
next.ServeHTTP(lrw, r)
duration := time.Since(start)
// maybe use an actual library for structured logging
log.Printf("%v", map[string]any{
"method": r.Method,
"uri": r.RequestURI,
"duration": duration,
"status": lrw.Status,
"size": lrw.Size,
})
})
}),
rmsgo.WithAuthentication(func(r *http.Request, bearer string) (rmsgo.User, bool) {
// [!] TODO: Your authentication logic here...
// Return one of your own users.
return rmsgo.UserReadWrite{}, true
}),
)
if err != nil {
log.Fatal(err)
}
rmsgo.Register(nil)
http.ListenAndServe(":8080", nil) // [!] TODO: Use TLS
}