forked from SkycoinProject/dmsg-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
49 lines (40 loc) · 1.36 KB
/
server.go
File metadata and controls
49 lines (40 loc) · 1.36 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 dmsghttp
import (
"context"
"log"
"net/http"
"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/dmsg/disc"
"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/SkycoinProject/dmsg"
)
// Server holds relevant data for server to run properly
// Data includes Public / Secret key pair that identifies the server.
// There is also port on which server will listen.
// Optional parameter is Discovery, if none is provided default one will be used.
// Default dicovery URL is stored as dmsghttp.DefaultDiscoveryURL
type Server struct {
PubKey cipher.PubKey
SecKey cipher.SecKey
Port uint16
Discovery disc.APIClient
hs *http.Server
}
// Serve handles request to dmsg server
// Accepts handler holding routes for the current instance
func (s *Server) Serve(handler http.Handler) error {
s.hs = &http.Server{Handler: handler}
client := dmsg.NewClient(s.PubKey, s.SecKey, s.Discovery, dmsg.SetLogger(logging.MustGetLogger("dmsgC_httpS")))
if err := client.InitiateServerConnections(context.Background(), 1); err != nil {
log.Fatalf("Error initiating server connections by initiator: %v", err)
}
list, err := client.Listen(s.Port)
if err != nil {
return err
}
return s.hs.Serve(list)
}
// Close closes active Listeners and Connections by invoking http's Close func
func (s *Server) Close() error {
return s.hs.Close()
}