Allow to submit new URL trough the API

Closes #4
pull/18/head
Aloïs Micard 4 years ago
parent 2313c242e8
commit 75fa6724c9
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE

@ -33,7 +33,7 @@ services:
- torproxy
#feeder:
# image: trandoshan.io/feeder:latest
# command: --log-level debug --nats-uri nats --url https://www.facebookcorewwwi.onion
# command: --log-level debug --api-uri http://api:8080 --url https://www.facebookcorewwwi.onion
scheduler:
image: trandoshan.io/scheduler:latest
command: --log-level debug --nats-uri nats --api-uri http://api:8080
@ -49,7 +49,9 @@ services:
- api
api:
image: trandoshan.io/api:latest
command: --log-level debug --elasticsearch-uri http://elasticsearch:9200
command: --log-level debug --nats-uri nats --elasticsearch-uri http://elasticsearch:9200
restart: always
depends_on:
- elasticsearch
- elasticsearch
ports:
- 15005:8080

@ -5,9 +5,12 @@ import (
"context"
"encoding/json"
"github.com/creekorful/trandoshan/internal/log"
"github.com/creekorful/trandoshan/internal/natsutil"
"github.com/creekorful/trandoshan/pkg/proto"
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/labstack/echo/v4"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"net/http"
@ -44,6 +47,11 @@ func GetApp() *cli.App {
Usage: "", // TODO
Flags: []cli.Flag{
log.GetLogFlag(),
&cli.StringFlag{
Name: "nats-uri",
Usage: "URI to the NATS server",
Required: true,
},
&cli.StringFlag{
Name: "elasticsearch-uri",
Usage: "URI to the Elasticsearch server",
@ -63,6 +71,15 @@ func execute(ctx *cli.Context) error {
logrus.Infof("Starting trandoshan-api v%s", ctx.App.Version)
logrus.Debugf("Using elasticsearch server at: %s", ctx.String("elasticsearch-uri"))
logrus.Debugf("Using NATS server at: %s", ctx.String("nats-uri"))
// Connect to the NATS server
nc, err := nats.Connect(ctx.String("nats-uri"))
if err != nil {
logrus.Errorf("Error while connecting to NATS server %s: %s", ctx.String("nats-uri"), err)
return err
}
defer nc.Close()
// Create Elasticsearch client
es, err := elasticsearch.NewClient(elasticsearch.Config{Addresses: []string{ctx.String("elasticsearch-uri")}})
@ -74,6 +91,7 @@ func execute(ctx *cli.Context) error {
// Add endpoints
e.GET("/v1/resources", searchResources(es))
e.POST("/v1/resources", addResource(es))
e.POST("/v1/urls", addURL(nc))
logrus.Info("Successfully initialized trandoshan-api. Waiting for requests")
@ -178,6 +196,26 @@ func addResource(es *elasticsearch.Client) echo.HandlerFunc {
}
}
func addURL(nc *nats.Conn) echo.HandlerFunc {
return func(c echo.Context) error {
var url string
if err := json.NewDecoder(c.Request().Body).Decode(&url); err != nil {
logrus.Errorf("Error while un-marshaling url: %s", err)
return c.NoContent(http.StatusUnprocessableEntity)
}
// Publish the URL
if err := natsutil.PublishJSON(nc, proto.URLTodoSubject, &proto.URLTodoMsg{URL: url}); err != nil {
logrus.Errorf("Unable to publish URL: %s", err)
return c.NoContent(http.StatusInternalServerError)
}
logrus.Debugf("Successfully published URL: %s", url)
return nil
}
}
// extract title from html body
func extractTitle(body string) string {
cleanBody := strings.ToLower(body)

@ -1,12 +1,12 @@
package feeder
import (
"bytes"
"fmt"
"github.com/creekorful/trandoshan/internal/log"
"github.com/creekorful/trandoshan/internal/natsutil"
"github.com/creekorful/trandoshan/pkg/proto"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"net/http"
)
// GetApp return the feeder app
@ -18,8 +18,8 @@ func GetApp() *cli.App {
Flags: []cli.Flag{
log.GetLogFlag(),
&cli.StringFlag{
Name: "nats-uri",
Usage: "URI to the NATS server",
Name: "api-uri",
Usage: "URI to the API server",
Required: true,
},
&cli.StringFlag{
@ -37,18 +37,10 @@ func execute(ctx *cli.Context) error {
logrus.Infof("Starting trandoshan-feeder v%s", ctx.App.Version)
logrus.Debugf("Using NATS server at: %s", ctx.String("nats-uri"))
logrus.Debugf("Using API server at: %s", ctx.String("api-uri"))
// Connect to the NATS server
nc, err := nats.Connect(ctx.String("nats-uri"))
if err != nil {
logrus.Errorf("Error while connecting to NATS server %s: %s", ctx.String("nats-uri"), err)
return err
}
defer nc.Close()
// Publish the message
if err := natsutil.PublishJSON(nc, proto.URLTodoSubject, &proto.URLTodoMsg{URL: ctx.String("url")}); err != nil {
apiURL := fmt.Sprintf("%s/v1/urls", ctx.String("api-uri"))
if _, err := http.Post(apiURL, "application/json", bytes.NewBufferString(ctx.String("api-uri"))); err != nil {
logrus.Errorf("Unable to publish URL: %s", err)
return err
}

Loading…
Cancel
Save