cli can pass -p 10_000
parent
49064f1ea2
commit
c7375949c2
|
|
@ -6,10 +6,12 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"show-rss/src/cleanup"
|
"show-rss/src/cleanup"
|
||||||
"show-rss/src/db"
|
"show-rss/src/db"
|
||||||
|
"show-rss/src/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
DB string
|
DB string
|
||||||
|
Port int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFlags(args []string) (Flags, error) {
|
func NewFlags(args []string) (Flags, error) {
|
||||||
|
|
@ -17,6 +19,7 @@ func NewFlags(args []string) (Flags, error) {
|
||||||
|
|
||||||
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
||||||
fs.StringVar(&result.DB, "db", "/tmp/f.db", "path to sqlite.db")
|
fs.StringVar(&result.DB, "db", "/tmp/f.db", "path to sqlite.db")
|
||||||
|
fs.IntVar(&result.Port, "p", 10_000, "port for http")
|
||||||
err := fs.Parse(args)
|
err := fs.Parse(args)
|
||||||
|
|
||||||
return result, err
|
return result, err
|
||||||
|
|
@ -33,6 +36,8 @@ func Config(ctx context.Context, args []string) (context.Context, func(), error)
|
||||||
return ctx, nil, err
|
return ctx, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx = server.Inject(ctx, flags.Port)
|
||||||
|
|
||||||
return ctx, func() {
|
return ctx, func() {
|
||||||
cleanup.Extract(ctx)()
|
cleanup.Extract(ctx)()
|
||||||
}, nil
|
}, nil
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ func oneItem(ctx context.Context, feed feeds.Feed, item feeds.Item) error {
|
||||||
Item: item,
|
Item: item,
|
||||||
}
|
}
|
||||||
|
|
||||||
wmethod, wurl, wbody := feed.Webhook()
|
wmethod, wurl, wbody := feed.Webhook(ctx)
|
||||||
|
|
||||||
method, err := render(wmethod, arg)
|
method, err := render(wmethod, arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Main(ctx context.Context) error {
|
func Main(ctx context.Context) error {
|
||||||
return Run(ctx, fmt.Sprintf(":%d", server.Port))
|
return Run(ctx, fmt.Sprintf(":%d", server.Extract(ctx)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(ctx context.Context, listen string) error {
|
func Run(ctx context.Context, listen string) error {
|
||||||
|
|
|
||||||
|
|
@ -263,11 +263,11 @@ func initDB(ctx context.Context) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (feed Feed) Webhook() (string, string, string) {
|
func (feed Feed) Webhook(ctx context.Context) (string, string, string) {
|
||||||
u, _ := url.Parse(feed.Version.WebhookURL)
|
u, _ := url.Parse(feed.Version.WebhookURL)
|
||||||
switch u.Scheme {
|
switch u.Scheme {
|
||||||
case "vpntor":
|
case "vpntor":
|
||||||
return "POST", fmt.Sprintf("http://localhost:%d/v1/vpntor", server.Port), fmt.Sprintf(`{
|
return "POST", fmt.Sprintf("http://localhost:%d/v1/vpntor", server.Extract(ctx)), fmt.Sprintf(`{
|
||||||
"Magnet": "{{ .Item.Link }}",
|
"Magnet": "{{ .Item.Link }}",
|
||||||
"Dir": %q,
|
"Dir": %q,
|
||||||
"URL": "https://vpntor.int.bel.blue/transmission/rpc"
|
"URL": "https://vpntor.int.bel.blue/transmission/rpc"
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,18 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"context"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var Port = func() int {
|
func Inject(ctx context.Context, port int) context.Context {
|
||||||
port, _ := strconv.Atoi(os.Getenv("PORT"))
|
return context.WithValue(ctx, "server.port", port)
|
||||||
if port == 0 {
|
}
|
||||||
port = 10000
|
|
||||||
|
func Extract(ctx context.Context) int {
|
||||||
|
v := ctx.Value("server.port")
|
||||||
|
port, ok := v.(int)
|
||||||
|
if !ok {
|
||||||
|
return 10_000
|
||||||
}
|
}
|
||||||
return port
|
return port
|
||||||
}()
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue