commit 5bed031dbe932635b47898c24b656e3c6c942ced Author: andrewlalis Date: Fri Mar 22 16:17:57 2024 -0400 Added files. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..43c5cf6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +.dub +docs.json +__dummy.html +docs/ +/service-ping +service-ping.so +service-ping.dylib +service-ping.dll +service-ping.a +service-ping.lib +service-ping-test-* +*.exe +*.pdb +*.o +*.obj +*.lst diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..e561644 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -e +set -o pipefail + +USER=pi +HOST=192.168.0.82 +ADDR=$USER@$HOST + +echo "Stopping service-ping" +ssh $ADDR "sudo systemctl stop service-ping" +echo "Removing old source" +ssh $ADDR "rm -rf /opt/service-ping/source/*" +echo "Uploading latest source" +scp -r ${PWD}/* $ADDR:/opt/service-ping/source/ +echo "Building application" +ssh $ADDR "cd /opt/service-ping/source && /opt/ldc2-1.37.0/bin/dub build --compiler=/opt/ldc2-1.37.0/bin/ldc2 --build=release" +echo "Copying app to final location" +ssh $ADDR "sudo cp -f /opt/service-ping/source/service-ping /opt/service-ping/app" +echo "Starting service-ping" +ssh $ADDR "sudo systemctl start service-ping" diff --git a/dub.json b/dub.json new file mode 100644 index 0000000..814b7a3 --- /dev/null +++ b/dub.json @@ -0,0 +1,12 @@ +{ + "authors": [ + "Andrew Lalis" + ], + "copyright": "Copyright © 2024, Andrew Lalis", + "dependencies": { + "handy-httpd": "~>8.3.2" + }, + "description": "A minimal D application.", + "license": "proprietary", + "name": "service-ping" +} \ No newline at end of file diff --git a/dub.selections.json b/dub.selections.json new file mode 100644 index 0000000..1fdafb9 --- /dev/null +++ b/dub.selections.json @@ -0,0 +1,10 @@ +{ + "fileVersion": 1, + "versions": { + "handy-httpd": "8.3.2", + "httparsed": "1.2.1", + "path-matcher": "1.1.4", + "slf4d": "3.0.0", + "streams": "3.5.0" + } +} diff --git a/service-ping.service b/service-ping.service new file mode 100644 index 0000000..f3159e7 --- /dev/null +++ b/service-ping.service @@ -0,0 +1,13 @@ +[Unit] +Description=service-ping +After=network.target + +[Service] +Type=simple +User=root +WorkingDirectory=/opt/service-ping +ExecStart=/opt/service-ping/app 192.168.0.82 8100 +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/source/app.d b/source/app.d new file mode 100644 index 0000000..c09d7b1 --- /dev/null +++ b/source/app.d @@ -0,0 +1,38 @@ +import handy_httpd; + +import std.conv : to; + +void main(string[] args) { + ServerConfig cfg; + cfg.workerPoolSize = 5; + cfg.defaultHeaders["Access-Control-Allow-Origin"] = "*"; + + if (args.length >= 2) cfg.hostname = args[1]; + if (args.length >= 3) cfg.port = args[2].to!ushort; + + new HttpServer(new RequestHandler(), cfg).start(); +} + +class RequestHandler : HttpRequestHandler { + void handle(ref HttpRequestContext ctx) { + auto maybeParam = ctx.request.queryParams.getFirst("address"); + if (!maybeParam) { + ctx.response.status = HttpStatus.BAD_REQUEST; + ctx.response.writeBodyString("Required query parameter \"address\" is missing."); + return; + } + string address = maybeParam.value; + bool pingSuccessful = doPing(address); + import std.json; + JSONValue responseBody = JSONValue.emptyObject; + responseBody.object["success"] = JSONValue(pingSuccessful); + ctx.response.writeBodyString(responseBody.toJSON, "application/json"); + } + + bool doPing(string address) { + import std.process; + auto result = execute(["ping", address, "-c", "1", "-W", "1"]); + const int status = result.status; + return status == 0; + } +}