forked from jrupac/goliath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goliath.go
90 lines (77 loc) · 2.01 KB
/
goliath.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
package main
import (
"context"
"flag"
"fmt"
log "github.com/golang/glog"
"github.com/jrupac/goliath/fetch"
"github.com/jrupac/goliath/opml"
"github.com/jrupac/goliath/storage"
"github.com/jrupac/goliath/utils"
"net/http"
"os"
"os/signal"
"syscall"
)
const VERSION = "0.01"
var (
dbPath = flag.String("dbPath", "", "The address of the database.")
opmlPath = flag.String("opmlPath", "", "Path of OPML file to import.")
portFlag = flag.Int("port", 9999, "Port of HTTP server.")
)
func main() {
flag.Parse()
defer log.Flush()
ctx := context.Background()
log.Infof("Goliath %s.", VERSION)
if *dbPath == "" {
log.Fatalf("Path to database must be set.")
}
d, err := storage.Open(*dbPath)
if err != nil {
log.Fatalf("Unable to open DB: %s", err)
}
defer d.Close()
if *opmlPath != "" {
p, err := opml.ParseOpml(*opmlPath)
if err != nil {
log.Warningf("Error while parsing OPML: %s", err)
}
utils.DebugPrint("Parsed OPML file", *p)
err = d.ImportOpml(p)
if err != nil {
log.Warningf("Error while importing OPML: %s", err)
}
}
allFeeds, err := d.GetAllFeeds()
if err != nil {
log.Infof("Failed to fetch all feeds: %s", err)
}
utils.DebugPrint("Feed list", allFeeds)
ctx, cancel := context.WithCancel(ctx)
go fetch.Start(ctx, d, allFeeds)
srv := &http.Server{Addr: fmt.Sprintf(":%d", *portFlag)}
installSignalHandler(cancel, srv)
if err = Serve(srv); err != nil {
log.Infof("%s", err)
}
}
func installSignalHandler(cancel context.CancelFunc, srv *http.Server) {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
s := <-sc
log.Infof("Received signal and shutting down: %s", s)
close(sc)
cancel()
log.Infof("Shutting down HTTP server.")
if err := srv.Shutdown(nil); err != nil {
log.Infof("Failed to cleanly shutdown HTTP server: %s", err)
}
}()
}
func Serve(srv *http.Server) error {
http.HandleFunc("/fever", HandleFever)
log.Infof("Starting HTTP server on port %d", *portFlag)
return srv.ListenAndServe()
}