Added files.

This commit is contained in:
Andrew Lalis 2024-03-22 16:17:57 -04:00
commit 5bed031dbe
6 changed files with 110 additions and 0 deletions

16
.gitignore vendored Normal file
View File

@ -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

21
deploy.sh Executable file
View File

@ -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"

12
dub.json Normal file
View File

@ -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"
}

10
dub.selections.json Normal file
View File

@ -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"
}
}

13
service-ping.service Normal file
View File

@ -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

38
source/app.d Normal file
View File

@ -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;
}
}