diff --git a/source/cartastraccia/actor.d b/source/cartastraccia/actor.d index 379b9f0..a08f218 100644 --- a/source/cartastraccia/actor.d +++ b/source/cartastraccia/actor.d @@ -110,7 +110,7 @@ foreach(ref conf; pt.children) { foreach(ref feed; conf.children) { - feeds ~= RSSActor(feed.matches + feeds ~= RSSActor( feed.matches .filter!((immutable s) => s != "\n" && s != " ") .array ); @@ -184,26 +184,22 @@ }, (RSSActor[] fl) { - // start tasks in charge of updating feeds - feeds.match!( - (InvalidFeeds i) => logFatal(i.msg), - (RSSActor[] fl) { - fl.each!( - (RSSActor feed) { - logInfo("Starting task: "~feed.name); + // start tasks in charge of updating feeds + fl.each!( + (RSSActor feed) { + logInfo("Starting task: "~feed.name); - // ensure all previous tasks are destroyed - if(oldTasks[feed.name].running()) { - logWarn("["~feed.name~"] Force stop."); - oldTasks[feed.name].interrupt(); - } + // ensure all previous tasks are destroyed + if(oldTasks[feed.name].running()) { + logWarn("["~feed.name~"] Force stop."); + oldTasks[feed.name].interrupt(); + } - // start new workers - tasks[feed.name] = runWorkerTaskH( - &feedActor, feed.name, feed.path, 0); - }); - }); - }); + // start new workers + tasks[feed.name] = runTask( + &feedActor, feed.name, feed.path, 0); + }); + }); return tasks; } diff --git a/source/cartastraccia/asciiart.d b/source/cartastraccia/asciiart.d index fb20c46..6dc8822 100644 --- a/source/cartastraccia/asciiart.d +++ b/source/cartastraccia/asciiart.d @@ -39,3 +39,21 @@ ░ ░ ░ "; + +static immutable string BANNER = "========================================================================== +| Carta Straccia is a RSS feed aggregator | +=========================================================================="; + +static immutable string QUICKSTART = " +Quickstart +-------------------------------------------------------------------------- +0. Write a feeds.conf file [feed_name refresh_timeout feed_url] +> echo \"Stallman 3h https://stallman.org/rss/rss.xml\" > feeds.conf +-------------------------------------------------------------------------- +1. Start the daemon: +> cartastraccia --daemon --feeds=feeds.conf +-------------------------------------------------------------------------- +2. Connect to daemon using HTML browser +> elinks 'https://localhost:8080' +========================================================================== +"; diff --git a/source/cartastraccia/endpoint.d b/source/cartastraccia/endpoint.d index 7aa1bce..9d80ed7 100644 --- a/source/cartastraccia/endpoint.d +++ b/source/cartastraccia/endpoint.d @@ -40,11 +40,6 @@ import std.datetime; import core.time; -enum EndpointType { - cli, - html -} - /** * Implementing methods for a vibe Web Interface. * Functions are mapped to a URL path via an attribute. @@ -90,7 +85,7 @@ lastUpdate[feed.name] = cast(DateTime)Clock.currTime(); tasks[feed.name].send(FeedActorRequest.QUIT); - tasks[feed.name] = runWorkerTaskH(&feedActor, feed.name, feed.path, 0); + tasks[feed.name] = runTask(&feedActor, feed.name, feed.path, 0); logInfo("["~feed.name~"] Finished updating."); @@ -112,15 +107,13 @@ (RSSActor[] fl) { fl.each!( (RSSActor f) { - actorHandshake(f.name); - tasks[f.name].send(FeedActorRequest.QUIT); }); }); - loadFeedsConfig(configFile).match!( - + loadFeedsConfig(configFile) + .match!( (InvalidFeeds i) { logWarn("Not reloading"); }, diff --git a/source/main.d b/source/main.d index 8ac7f29..d37321d 100644 --- a/source/main.d +++ b/source/main.d @@ -52,25 +52,15 @@ import std.conv : to; import std.process; -immutable string info = "========================================================================== -| Carta Straccia is a RSS feed aggregator | -========================================================================== -0. Write a feeds.conf file [feed_name refresh_timeout feed_url] -> echo \"Stallman 3h https://stallman.org/rss/rss.xml\" > feeds.conf --------------------------------------------------------------------------- -1. Start the daemon: -> cartastraccia --daemon --feeds=feeds.conf --------------------------------------------------------------------------- -2. Connect to daemon using HTML endpoint -> cartastraccia --browser=/path/to/browser -=========================================================================="; /** * Start a vibe.d webserver * using an already initialied router * Loops on the eventloop until stopped. */ -void runWebServer(ref URLRouter router, immutable string bindAddress, immutable ushort bindPort) +void runWebServer(ref URLRouter router, + immutable string bindAddress, + immutable ushort bindPort) { auto settings = new HTTPServerSettings; settings.port = bindPort; @@ -87,8 +77,9 @@ * - registering a vibe.d router with an handle for each endpoint [html, cli, json, ...] * - starting a webserver */ -void runDaemon(immutable string feedsFile, immutable - string bindAddress, immutable ushort bindPort) +void runDaemon(immutable string feedsFile, + immutable string bindAddress, + immutable ushort bindPort) { auto feeds = loadFeedsConfig(feedsFile); @@ -112,7 +103,7 @@ (RSSActor feed) { logInfo("Starting task: "~feed.name); // start workers to serve RSS data - tasks[feed.name] = runWorkerTaskH( + tasks[feed.name] = runTask( &feedActor, feed.name, feed.path, 0); }); }); @@ -128,8 +119,9 @@ }); } -void runClient(EndpointType endpoint, immutable string browser, immutable string - bindAddress, immutable ushort bindPort, immutable bool reloadFeeds) +void runClient(immutable string bindAddress, + immutable ushort bindPort, + immutable bool reloadFeeds) { import std.stdio; @@ -147,63 +139,53 @@ } } - if(endpoint == EndpointType.cli) { - try { - string url = "http://"~bindAddress~":"~bindPort.to!string~"/cli"; - auto req = Request(); - req.keepAlive = false; - req.timeout = ACTOR_REQ_TIMEOUT; - req.get(url); + // try { + // string url = "http://"~bindAddress~":"~bindPort.to!string~"/cli"; + // auto req = Request(); + // req.keepAlive = false; + // req.timeout = ACTOR_REQ_TIMEOUT; + // req.get(url); - } catch (Exception e) { - logWarn("ERROR from daemon: "~e.msg~"\nCheck daemon logs for details (is it running?)"); - } - - } else if(endpoint == EndpointType.html) { - - if(!existsFile(browser)) { - logWarn("Could not find browser: "~browser); - logWarn("Try running: cartastraccia --browser=[/path/to/browser]"); - return; - } - - immutable address = "http://"~bindAddress~":"~bindPort.to!string; - auto pid = spawnShell(browser ~" "~address); - wait(pid); - } + // } catch (Exception e) { + // logWarn("ERROR from daemon: "~e.msg~"\nCheck daemon logs for details (is it running?)"); + // } } void main(string[] args) { // CLI arguments bool daemon = false; - EndpointType endpoint = EndpointType.html; string feedsFile = "feeds.conf"; string bindAddress = "localhost"; ushort bindPort = 8080; string browser = "/usr/bin/elinks"; bool reloadFeeds = false; + bool quickstart = false; auto helpInformation = getopt( - args, - "daemon|d", "Start daemon", &daemon, - "endpoint|e", "Endpoints to register [cli]", &endpoint, - "feeds|f", "File containing feeds to pull [feeds.conf]", &feedsFile, - "host|l", "Bind to this address [localhost]", &bindAddress, - "port|p", "Bind to this port [8080]", &bindPort, - "browser|b", "Absolute path to browser for HTML rendering [/usr/bin/elinks]", &browser, - "reload|r", "Reload feeds file", &reloadFeeds - ); + args, + "daemon|d", "Start daemon", &daemon, + "feeds|f", "File containing feeds to pull [feeds.conf]", &feedsFile, + "host|l", "Bind to this address [localhost]", &bindAddress, + "port|p", "Bind to this port [8080]", &bindPort, + "reload|r", "Reload feeds file", &reloadFeeds, + "quickstart|q", "Show quickstart template", &quickstart + ); if(helpInformation.helpWanted) { - defaultGetoptPrinter(info, helpInformation.options); + defaultGetoptPrinter(BANNER, helpInformation.options); + return; + } + + if(quickstart) { + defaultGetoptPrinter(BANNER ~ QUICKSTART, helpInformation.options); return; } if(daemon && reloadFeeds) { - logWarn("Starting daemon. Feeds file loading is the default behavior. Ignoring reload request."); + logWarn("Starting daemon. Ignoring reload request (not effective)."); } if(daemon) runDaemon(feedsFile, bindAddress, bindPort); - else runClient(endpoint, browser, bindAddress, bindPort, reloadFeeds); + else runClient(bindAddress, bindPort, reloadFeeds); }