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
46 lines (36 loc) · 963 Bytes
/
server.go
File metadata and controls
46 lines (36 loc) · 963 Bytes
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
package dmsghttp
import (
"context"
"log"
"net/http"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/disc"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/skycoin/dmsg"
)
type Server struct {
PubKey cipher.PubKey
SecKey cipher.SecKey
Port uint16
DiscoveryURL string
hs *http.Server
}
func (s *Server) Serve(handler http.Handler) error {
s.hs = &http.Server{Handler: handler}
if len(s.DiscoveryURL) == 0 {
s.DiscoveryURL = DefaultDiscoveryURL
}
dc := disc.NewHTTP(s.DiscoveryURL)
client := dmsg.NewClient(s.PubKey, s.SecKey, dc, 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)
}
func (s *Server) Close() error {
return s.hs.Close()
}