-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
54 lines (49 loc) · 1009 Bytes
/
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
package attest
import (
"fmt"
"net"
"net/http"
)
// Basic HTTP server.
type HttpServer struct {
Handler *http.ServeMux
listener net.Listener
server http.Server
stopped chan bool
}
// Create a new HTTP server for testing purposes. To add a handler for a path,
// use the Handler field. The server will continue to run until the Close()
// method is invoked.
func NewHttpServer() (*HttpServer, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, err
}
var (
s = http.NewServeMux()
c = make(chan bool)
h = &HttpServer{
Handler: s,
listener: l,
server: http.Server{
Handler: s,
},
stopped: c,
}
)
go func() {
h.server.Serve(l)
close(c)
}()
return h, nil
}
// Retrieve the address of the server. The string will be in the form
// "http://host:port".
func (h *HttpServer) Addr() string {
return fmt.Sprintf("http://%s", h.listener.Addr())
}
// Close the server.
func (h *HttpServer) Close() {
h.listener.Close()
<-h.stopped
}