-
Notifications
You must be signed in to change notification settings - Fork 1
/
torresmo.go
62 lines (50 loc) · 1.23 KB
/
torresmo.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
package torresmo
import (
"context"
"io/fs"
gohttp "net/http"
"time"
"github.com/mvrilo/torresmo/event"
"github.com/mvrilo/torresmo/log"
"github.com/mvrilo/torresmo/static"
"github.com/mvrilo/torresmo/torrent"
)
type Torresmo struct {
HTTPServer *gohttp.Server
EventHandler event.Handler
TorrentClient torrent.Client
Logger log.Logger
StaticFiles fs.FS
}
func (t *Torresmo) Shutdown(ctx context.Context, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
if t.HTTPServer != nil {
log.Info("Shutting down http server")
if err := t.HTTPServer.Shutdown(ctx); err != nil && err != gohttp.ErrServerClosed {
return err
}
}
log.Info("Shutting down torrent client")
t.TorrentClient.Stop()
return nil
}
// New initializes a Torresmo with some defaults
func New() (*Torresmo, error) {
logger := log.NewLogger()
wshandler := event.NewWebsocket(logger)
staticFiles, err := fs.Sub(static.Files, "dist")
if err != nil {
return nil, err
}
torrentClient, err := torrent.NewClient(logger)
if err != nil {
return nil, err
}
return &Torresmo{
Logger: logger,
EventHandler: wshandler,
TorrentClient: torrentClient,
StaticFiles: staticFiles,
}, nil
}