-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
executable file
·49 lines (42 loc) · 1.11 KB
/
server.go
File metadata and controls
executable file
·49 lines (42 loc) · 1.11 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
package main
import (
"context"
"fmt"
"net"
"net/http"
"time"
)
type server struct {
srv *http.Server
listen net.Listener
}
func newServer(handler http.Handler, port int, timeout time.Duration) (*server, error) {
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return nil, fmt.Errorf("Could not listen: %v\n", err)
}
s := &http.Server{
Handler: handler,
ReadTimeout: timeout,
WriteTimeout: timeout,
}
return &server{s, l}, nil
}
// Serve calls the underlying HTTP server's Serve() function, passing
// it the listener
func (s server) Serve() error {
return s.srv.Serve(s.listen)
}
// ServeTLS calls the underlying HTTP server's ServeTLS() function, passing
// it the listener
func (s server) ServeTLS(certFile, keyFile string) error {
return s.srv.ServeTLS(s.listen, certFile, keyFile)
}
// Shutdown calls the underlying HTTP server's Shutdown() function
// with a given timeout
func (s server) Shutdown(timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
defer s.listen.Close()
return s.srv.Shutdown(ctx)
}