-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrss2full.go
128 lines (108 loc) · 2.66 KB
/
rss2full.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"strconv"
"syscall"
"github.com/judwhite/go-svc/svc"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
)
var (
Version = "0.2.1"
aAddr = flag.String("a", "", "Bind address")
aPort = flag.Int("p", 8088, "Port to listen")
aVers = flag.Bool("v", false, "Show version")
aVersl = flag.Bool("version", false, "Show version")
aHelp = flag.Bool("h", false, "Show help")
aHelpl = flag.Bool("help", false, "Show help")
aItemCount = flag.Int("item-count", 10, "Define number of items in feed")
aConnectionPerFeed = flag.Int("connection-per-feed", 2, "Define number of parallel connections per feed")
)
const usage = `rss2full %s
Usage:
rss2full -p 80
rss2full -h | -help
rss2full -v | -version
Options:
-a <addr> Bind address [default: *]
-p <port> Bind port [default: 8088]
-h, -help Show help
-v, -version Show version
-item-count <num> Define number of items in feed
-connection-per-feed <num> Define number of parallel connections(workers) per feed
`
type program struct {
quit chan struct{}
}
func (p *program) Init(env svc.Environment) error {
return nil
}
func (p *program) Start() error {
flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(usage, Version))
}
flag.Parse()
if *aHelp || *aHelpl {
showUsage()
}
if *aVers || *aVersl {
showVersion()
}
port := getPort(*aPort)
addr := *aAddr + ":" + strconv.Itoa(port)
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
go func() {
wwwroot := http.Dir("./wwwroot")
fs := http.FileServer(wwwroot)
router := httprouter.New()
router.GET("/feed/*feed", FullRss)
router.Handler("GET", "/assets/*filepath", fs)
router.Handler("GET", "/", fs)
if err := http.Serve(listener, router); err != nil {
logrus.Fatalf("http.Serve got error: %v", err)
}
<-p.quit
listener.Close()
logrus.Info("exit")
}()
logrus.Infof("version: %s", Version)
logrus.Infof("listen on %s \n", addr)
return nil
}
func (p *program) Stop() error {
close(p.quit)
return nil
}
func main() {
logrus.SetOutput(os.Stdout)
prg := &program{
quit: make(chan struct{}),
}
if err := svc.Run(prg, syscall.SIGINT, syscall.SIGTERM); err != nil {
logrus.Fatal(err)
}
}
func getPort(port int) int {
if portEnv := os.Getenv("PORT"); portEnv != "" {
newPort, _ := strconv.Atoi(portEnv)
if newPort > 0 {
port = newPort
}
}
return port
}
func showVersion() {
fmt.Println(Version)
os.Exit(1)
}
func showUsage() {
flag.Usage()
os.Exit(1)
}