webhooks can be vpntor:///outdir

main
bel 2025-05-05 21:23:59 -06:00
parent dbfd33f55e
commit 352eff2691
5 changed files with 43 additions and 16 deletions

View File

@ -70,17 +70,19 @@ func oneItem(ctx context.Context, feed feeds.Feed, item feeds.Item) error {
Item: item,
}
method, err := render(feed.Version.WebhookMethod, arg)
wmethod, wurl, wbody := feed.Webhook()
method, err := render(wmethod, arg)
if err != nil {
return err
}
wurl, err := render(feed.Version.WebhookURL, arg)
wurl, err = render(wurl, arg)
if err != nil {
return err
}
body, err := render(feed.Version.WebhookBody, arg)
body, err := render(wbody, arg)
if err != nil {
return err
}

View File

@ -24,12 +24,12 @@ func (h Handler) feeds(w http.ResponseWriter, r *http.Request) error {
func (h Handler) feedsPost(ctx context.Context, form url.Values) error {
var req feeds.Version
for k, v := range map[string]*string{
"URL": &req.URL,
"Cron": &req.Cron,
"Pattern": &req.Pattern,
"URL": &req.URL,
"WebhookBody": &req.WebhookBody,
"WebhookMethod": &req.WebhookMethod,
"WebhookURL": &req.WebhookURL,
"WebhookBody": &req.WebhookBody,
} {
if *v = form.Get(k); *v == "" {
return fmt.Errorf("no ?%s in %s", k, form.Encode())

View File

@ -5,18 +5,12 @@ import (
"fmt"
"net"
"net/http"
"os"
"show-rss/src/cmd/server/handler"
"strconv"
"show-rss/src/server"
)
func Main(ctx context.Context) error {
port, _ := strconv.Atoi(os.Getenv("PORT"))
if port == 0 {
port = 10000
}
return Run(ctx, fmt.Sprintf(":%d", port))
return Run(ctx, fmt.Sprintf(":%d", server.Port))
}
func Run(ctx context.Context, listen string) error {

View File

@ -2,8 +2,11 @@ package feeds
import (
"context"
"fmt"
"io"
"net/url"
"show-rss/src/db"
"show-rss/src/server"
"time"
"github.com/google/uuid"
@ -163,9 +166,9 @@ func Insert(ctx context.Context, url, cron, pattern, webhookMethod, webhookURL,
url,
cron,
pattern,
webhook_method,
webhook_url,
webhook_body
webhook_method,
webhook_url,
webhook_body
) VALUES ($4, $5, $6, $7, $8, $9, $10, $11);
COMMIT;
`,
@ -230,3 +233,17 @@ func initDB(ctx context.Context) error {
)`,
})
}
func (feed Feed) Webhook() (string, string, string) {
u, _ := url.Parse(feed.Version.WebhookURL)
switch u.Scheme {
case "vpntor":
return "POST", fmt.Sprintf("http://localhost:%d/v1/vpntor", server.Port), fmt.Sprintf(`{
"Magnet": "{{ .Item.Link }}",
"Dir": %q,
"URL": "https://vpntor.int.bel.blue/transmission/rpc"
}`, u.Path)
default:
return feed.Version.WebhookMethod, feed.Version.WebhookURL, feed.Version.WebhookBody
}
}

14
src/server/config.go Normal file
View File

@ -0,0 +1,14 @@
package server
import (
"os"
"strconv"
)
var Port = func() int {
port, _ := strconv.Atoi(os.Getenv("PORT"))
if port == 0 {
port = 10000
}
return port
}()