From 6c842d4793ead4b83da8a71048df6bc70869494d Mon Sep 17 00:00:00 2001
From: DerGrumpf
Date: Sat, 21 Feb 2026 19:08:28 +0100
Subject: [PATCH] Added: Postgrest Endpoint for Learnlytics Database; WIP:
Swagger for Learnlytics endpoint
---
hosts/cyper-pi-1/configuration.nix | 2 +-
hosts/cyper-pi-1/postgrest.nix | 96 ++++++++++++++------
hosts/cyper-pi-1/swagger.nix | 138 ++++++++++++++++++++---------
3 files changed, 166 insertions(+), 70 deletions(-)
diff --git a/hosts/cyper-pi-1/configuration.nix b/hosts/cyper-pi-1/configuration.nix
index 6121b41..e56adc2 100644
--- a/hosts/cyper-pi-1/configuration.nix
+++ b/hosts/cyper-pi-1/configuration.nix
@@ -8,7 +8,7 @@
./postgres.nix
./postgrest.nix
./swagger.nix
- ./k3s-master.nix
+ # ./k3s-master.nix
];
# Any RPi 4 specific customizations go here
diff --git a/hosts/cyper-pi-1/postgrest.nix b/hosts/cyper-pi-1/postgrest.nix
index bc86a05..a49aafd 100644
--- a/hosts/cyper-pi-1/postgrest.nix
+++ b/hosts/cyper-pi-1/postgrest.nix
@@ -5,43 +5,85 @@
{
# PostgREST service
- systemd.services.postgrest = {
- description = "PostgREST - PostgreSQL REST API";
- after = [
- "postgresql.service"
- "network.target"
- ];
- wantedBy = [ "multi-user.target" ];
+ systemd.services = {
+ postgrest = {
+ description = "PostgREST - PostgreSQL REST API";
+ after = [
+ "postgresql.service"
+ "network.target"
+ ];
+ wantedBy = [ "multi-user.target" ];
- serviceConfig = {
- Type = "simple";
- ExecStart = "${pkgs.postgrest}/bin/postgrest /etc/postgrest/postgrest.conf";
- User = "postgrest";
- Restart = "on-failure";
- RestartSec = 5;
+ serviceConfig = {
+ Type = "simple";
+ ExecStart = "${pkgs.postgrest}/bin/postgrest /etc/postgrest/postgrest.conf";
+ User = "postgrest";
+ Restart = "on-failure";
+ 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
- users.users.postgrest = {
- isSystemUser = true;
- group = "postgrest";
+ users = {
+ users.postgrest = {
+ isSystemUser = true;
+ group = "postgrest";
+ };
+
+ groups.postgrest = { };
};
- users.groups.postgrest = { };
-
# PostgREST configuration file
- environment.etc."postgrest/postgrest.conf".text = ''
- db-uri = "postgres://postgres:postgres@127.0.0.1:5432/postgres"
- db-schema = "public"
- db-anon-role = "web_anon"
- server-host = "0.0.0.0"
- server-port = 3000
- jwt-secret = "no7WwM0xJa/Yzn5o4IZHG4oBNSShl4JRPZOcmhvnqFw="
- '';
+ environment.etc = {
+ "postgrest/postgrest.conf".text = ''
+ db-uri = "postgres://postgres:1P2h3i4lon@127.0.0.1:5432/postgres"
+ db-schema = "public"
+ db-anon-role = "web_anon"
+ server-host = "0.0.0.0"
+ 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
- networking.firewall.allowedTCPPorts = [ 3000 ];
+ networking.firewall.allowedTCPPorts = [
+ 3000
+ 3001
+ ];
# Include postgrest in system packages
environment.systemPackages = with pkgs; [
diff --git a/hosts/cyper-pi-1/swagger.nix b/hosts/cyper-pi-1/swagger.nix
index 7c53f95..82fba61 100644
--- a/hosts/cyper-pi-1/swagger.nix
+++ b/hosts/cyper-pi-1/swagger.nix
@@ -2,14 +2,15 @@
pkgs,
...
}:
-
{
# Swagger UI service
systemd.services.swagger-ui = {
- description = "Swagger UI for PostgREST API";
- after = [ "postgrest.service" "network.target" ];
+ description = "Swagger UI for PostgREST APIs";
+ after = [
+ "postgrest.service"
+ "network.target"
+ ];
wantedBy = [ "multi-user.target" ];
-
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.nodePackages.http-server}/bin/http-server /var/lib/swagger-ui -p 8080 -c-1 --cors";
@@ -18,54 +19,108 @@
User = "swagger";
};
};
-
# Create swagger user
- users.users.swagger = {
- isSystemUser = true;
- group = "swagger";
+ users = {
+ users.swagger = {
+ isSystemUser = true;
+ group = "swagger";
+ };
+ groups.swagger = { };
};
-
- users.groups.swagger = {};
-
# Setup Swagger UI directory
systemd.tmpfiles.rules = [
"d /var/lib/swagger-ui 0755 swagger swagger"
];
- # Create Swagger UI index.html that points to PostgREST OpenAPI
- environment.etc."swagger-ui-init/index.html".text = ''
-
-
-
- Swagger UI - PostgREST API
-
-
-
-
-
-
-
-
-
-
-
- '';
+ # Index page to select which API
+ environment.etc = {
+ "swagger-ui-init/index.html".text = ''
+
+
+
+ PostgREST APIs
+
+
+
+
+
+ PostgREST APIs
+ Default API (port 3000)
+ Learnlytics API (port 3001)
+
+
+ '';
+ # Swagger UI for default API
+ "swagger-ui-init/default/index.html".text = ''
+
+
+
+ Swagger UI - Default API
+
+
+
+
+
+
+
+
+
+
+
+ '';
+
+ # Swagger UI for learnlytics API
+ "swagger-ui-init/learnlytics/index.html".text = ''
+
+
+
+ Swagger UI - Learnlytics API
+
+
+
+
+
+
+
+
+
+
+
+ '';
+ };
# Copy Swagger UI files on startup
system.activationScripts.swagger-ui-setup = ''
- mkdir -p /var/lib/swagger-ui
- cp ${pkgs.swagger-ui}/share/swagger-ui/* /var/lib/swagger-ui/ 2>/dev/null || true
+ mkdir -p /var/lib/swagger-ui/default /var/lib/swagger-ui/learnlytics
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
'';
@@ -74,7 +129,6 @@
# Include packages
environment.systemPackages = with pkgs; [
- swagger-ui
nodePackages.http-server
];
}