Added: Postgrest Endpoint for Learnlytics Database; WIP: Swagger for Learnlytics endpoint

This commit is contained in:
2026-02-21 19:08:28 +01:00
parent c66af9c4e1
commit 6c842d4793
3 changed files with 166 additions and 70 deletions

View File

@@ -8,7 +8,7 @@
./postgres.nix ./postgres.nix
./postgrest.nix ./postgrest.nix
./swagger.nix ./swagger.nix
./k3s-master.nix # ./k3s-master.nix
]; ];
# Any RPi 4 specific customizations go here # Any RPi 4 specific customizations go here

View File

@@ -5,43 +5,85 @@
{ {
# PostgREST service # PostgREST service
systemd.services.postgrest = { systemd.services = {
description = "PostgREST - PostgreSQL REST API"; postgrest = {
after = [ description = "PostgREST - PostgreSQL REST API";
"postgresql.service" after = [
"network.target" "postgresql.service"
]; "network.target"
wantedBy = [ "multi-user.target" ]; ];
wantedBy = [ "multi-user.target" ];
serviceConfig = { serviceConfig = {
Type = "simple"; Type = "simple";
ExecStart = "${pkgs.postgrest}/bin/postgrest /etc/postgrest/postgrest.conf"; ExecStart = "${pkgs.postgrest}/bin/postgrest /etc/postgrest/postgrest.conf";
User = "postgrest"; User = "postgrest";
Restart = "on-failure"; Restart = "on-failure";
RestartSec = 5; RestartSec = 5;
};
};
# PostgREST service for learnlytics database
postgrest-learnlytics = {
description = "PostgREST - Learnlytics API";
after = [
"postgresql.service"
"network.target"
];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.postgrest}/bin/postgrest /etc/postgrest/postgrest-learnlytics.conf";
User = "postgrest";
Restart = "on-failure";
RestartSec = 5;
};
}; };
}; };
# Create postgrest user # Create postgrest user
users.users.postgrest = { users = {
isSystemUser = true; users.postgrest = {
group = "postgrest"; isSystemUser = true;
group = "postgrest";
};
groups.postgrest = { };
}; };
users.groups.postgrest = { };
# PostgREST configuration file # PostgREST configuration file
environment.etc."postgrest/postgrest.conf".text = '' environment.etc = {
db-uri = "postgres://postgres:postgres@127.0.0.1:5432/postgres" "postgrest/postgrest.conf".text = ''
db-schema = "public" db-uri = "postgres://postgres:1P2h3i4lon@127.0.0.1:5432/postgres"
db-anon-role = "web_anon" db-schema = "public"
server-host = "0.0.0.0" db-anon-role = "web_anon"
server-port = 3000 server-host = "0.0.0.0"
jwt-secret = "no7WwM0xJa/Yzn5o4IZHG4oBNSShl4JRPZOcmhvnqFw=" server-port = 3000
''; server-proxy-uri = "http://192.168.2.200:3000"
jwt-secret = "no7WwM0xJa/Yzn5o4IZHG4oBNSShl4JRPZOcmhvnqFw="
server-cors-allowed-origins = "*"
openapi-server-proxy-uri = "http://192.168.2.200:3000"
'';
# PostgREST configuration for learnlytics database
"postgrest/postgrest-learnlytics.conf".text = ''
db-uri = "postgres://postgres:1P2h3i4lon@127.0.0.1:5432/learnlytics"
db-schema = "public"
db-anon-role = "web_anon"
server-host = "0.0.0.0"
server-port = 3001
server-proxy-uri = "http://192.168.2.200:3001"
jwt-secret = "dnjudqZC0Fby2DEo3Xt2nY98KlHxxqT7NWvLk5aKmew="
server-cors-allowed-origins = "*"
'';
};
# Open firewall port for PostgREST # Open firewall port for PostgREST
networking.firewall.allowedTCPPorts = [ 3000 ]; networking.firewall.allowedTCPPorts = [
3000
3001
];
# Include postgrest in system packages # Include postgrest in system packages
environment.systemPackages = with pkgs; [ environment.systemPackages = with pkgs; [

View File

@@ -2,14 +2,15 @@
pkgs, pkgs,
... ...
}: }:
{ {
# Swagger UI service # Swagger UI service
systemd.services.swagger-ui = { systemd.services.swagger-ui = {
description = "Swagger UI for PostgREST API"; description = "Swagger UI for PostgREST APIs";
after = [ "postgrest.service" "network.target" ]; after = [
"postgrest.service"
"network.target"
];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
serviceConfig = { serviceConfig = {
Type = "simple"; Type = "simple";
ExecStart = "${pkgs.nodePackages.http-server}/bin/http-server /var/lib/swagger-ui -p 8080 -c-1 --cors"; ExecStart = "${pkgs.nodePackages.http-server}/bin/http-server /var/lib/swagger-ui -p 8080 -c-1 --cors";
@@ -18,54 +19,108 @@
User = "swagger"; User = "swagger";
}; };
}; };
# Create swagger user # Create swagger user
users.users.swagger = { users = {
isSystemUser = true; users.swagger = {
group = "swagger"; isSystemUser = true;
group = "swagger";
};
groups.swagger = { };
}; };
users.groups.swagger = {};
# Setup Swagger UI directory # Setup Swagger UI directory
systemd.tmpfiles.rules = [ systemd.tmpfiles.rules = [
"d /var/lib/swagger-ui 0755 swagger swagger" "d /var/lib/swagger-ui 0755 swagger swagger"
]; ];
# Create Swagger UI index.html that points to PostgREST OpenAPI # Index page to select which API
environment.etc."swagger-ui-init/index.html".text = '' environment.etc = {
<!DOCTYPE html> "swagger-ui-init/index.html".text = ''
<html> <!DOCTYPE html>
<head> <html>
<title>Swagger UI - PostgREST API</title> <head>
<meta charset="utf-8"/> <title>PostgREST APIs</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="utf-8"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css"> <meta name="viewport" content="width=device-width, initial-scale=1">
</head> <style>
<body> body { font-family: Arial; margin: 40px; }
<div id="swagger-ui"></div> a { display: block; margin: 10px 0; padding: 10px; background: #007bff; color: white; text-decoration: none; border-radius: 5px; width: 200px; text-align: center; }
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js"></script> a:hover { background: #0056b3; }
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-standalone-preset.js"></script> </style>
<script> </head>
SwaggerUIBundle({ <body>
url: "http://localhost:3000/openapi.json", <h1>PostgREST APIs</h1>
dom_id: '#swagger-ui', <a href="/default">Default API (port 3000)</a>
presets: [ <a href="/learnlytics">Learnlytics API (port 3001)</a>
SwaggerUIBundle.presets.apis, </body>
SwaggerUIStandalonePreset </html>
], '';
layout: "StandaloneLayout"
})
</script>
</body>
</html>
'';
# Swagger UI for default API
"swagger-ui-init/default/index.html".text = ''
<!DOCTYPE html>
<html>
<head>
<title>Swagger UI - Default API</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css">
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-standalone-preset.js"></script>
<script>
const host = window.location.host.split(':')[0];
SwaggerUIBundle({
url: "http://" + host + ":3000",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout"
})
</script>
</body>
</html>
'';
# Swagger UI for learnlytics API
"swagger-ui-init/learnlytics/index.html".text = ''
<!DOCTYPE html>
<html>
<head>
<title>Swagger UI - Learnlytics API</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css">
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-standalone-preset.js"></script>
<script>
const host = window.location.host.split(':')[0];
SwaggerUIBundle({
url: "http://" + host + ":3001",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout"
})
</script>
</body>
</html>
'';
};
# Copy Swagger UI files on startup # Copy Swagger UI files on startup
system.activationScripts.swagger-ui-setup = '' system.activationScripts.swagger-ui-setup = ''
mkdir -p /var/lib/swagger-ui mkdir -p /var/lib/swagger-ui/default /var/lib/swagger-ui/learnlytics
cp ${pkgs.swagger-ui}/share/swagger-ui/* /var/lib/swagger-ui/ 2>/dev/null || true
cp /etc/swagger-ui-init/index.html /var/lib/swagger-ui/index.html cp /etc/swagger-ui-init/index.html /var/lib/swagger-ui/index.html
cp /etc/swagger-ui-init/default/index.html /var/lib/swagger-ui/default/index.html
cp /etc/swagger-ui-init/learnlytics/index.html /var/lib/swagger-ui/learnlytics/index.html
chown -R swagger:swagger /var/lib/swagger-ui chown -R swagger:swagger /var/lib/swagger-ui
''; '';
@@ -74,7 +129,6 @@
# Include packages # Include packages
environment.systemPackages = with pkgs; [ environment.systemPackages = with pkgs; [
swagger-ui
nodePackages.http-server nodePackages.http-server
]; ];
} }