This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.go
96 lines (81 loc) · 2.68 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
wwr "github.com/qbeon/webwire-go"
)
// EchoServer implements the webwire.ServerImplementation interface
type EchoServer struct{}
// OnOptions implements the webwire.ServerImplementation interface.
// Sets HTTP access control headers to satisfy CORS
func (srv *EchoServer) OnOptions(resp http.ResponseWriter) {
resp.Header().Set("Access-Control-Allow-Origin", "*")
resp.Header().Set("Access-Control-Allow-Methods", "WEBWIRE")
}
// OnSignal implements the webwire.ServerImplementation interface
// Does nothing, not needed in this example
func (srv *EchoServer) OnSignal(
_ context.Context,
_ *wwr.Client,
_ *wwr.Message,
) {
}
// OnClientConnected implements the webwire.ServerImplementation interface.
// Does nothing, not needed in this example
func (srv *EchoServer) OnClientConnected(client *wwr.Client) {}
// OnClientDisconnected implements the webwire.ServerImplementation interface
// Does nothing, not needed in this example
func (srv *EchoServer) OnClientDisconnected(client *wwr.Client) {}
// BeforeUpgrade implements the webwire.ServerImplementation interface.
// Must return true to ensure incoming connections are accepted
func (srv *EchoServer) BeforeUpgrade(resp http.ResponseWriter, req *http.Request) bool {
return true
}
// OnRequest implements the webwire.ServerImplementation interface.
// Returns the received message back to the client
func (srv *EchoServer) OnRequest(
_ context.Context,
client *wwr.Client,
message *wwr.Message,
) (response wwr.Payload, err error) {
log.Printf("Replied to client: %s", client.Info().RemoteAddr)
// Reply to the request using the same data and encoding
return message.Payload, nil
}
var serverAddr = flag.String("addr", ":8081", "server address")
func main() {
// Parse command line arguments
flag.Parse()
// Setup a new webwire server instance
server, err := wwr.NewServer(
&EchoServer{},
wwr.ServerOptions{
Address: *serverAddr,
},
)
if err != nil {
panic(fmt.Errorf("Failed setting up WebWire server: %s", err))
}
// Listen for OS signals and shutdown server in case of demanded termination
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-osSignals
log.Printf("Termination demanded by the OS (%s), shutting down...", sig)
if err := server.Shutdown(); err != nil {
log.Printf("Error during server shutdown: %s", err)
}
log.Println("Server gracefully terminated")
}()
// Launch echo server
log.Printf("Listening on %s", server.Addr().String())
if err := server.Run(); err != nil {
panic(fmt.Errorf("WebWire server failed: %s", err))
}
}