diff --git a/build/build_fhir_ant.sh b/build/build_fhir_ant.sh new file mode 100755 index 0000000..8fd5898 --- /dev/null +++ b/build/build_fhir_ant.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +# Ensure Go modules are initialized +if [ ! -f go.mod ]; then + go mod init pocketfhir +fi + +# Install necessary tools +go get -u golang.org/x/mobile/bind +go install golang.org/x/mobile/cmd/gomobile@latest +gomobile init + +# Build PocketFHIR .aar for Android +echo "Building PocketFHIR .aar file for Android..." +gomobile bind -target=android -androidapi=21 -o pocketfhir.aar ./pocketfhir +if [ $? -ne 0 ]; then + echo "Android build failed!" + exit 1 +fi +mv pocketfhir.aar ../fhir_ant/android/app/libs/pocketfhir.aar +rm pocketfhir-sources.jar + +#!/bin/bash + +cd ../fhir_ant + +# Define the file path +FILE_PATH="./android/app/build.gradle" + +# Use sed to replace the specified line +sed -i.bak "s/implementation(name: 'pocketfhir', ext: 'aar') \/\/ Include PocketFHIR \.aar/implementation(name: 'pocketfhir-2', ext: 'aar') \/\/ Include PocketFHIR \.aar/" "$FILE_PATH" + +flutter clean ; flutter pub get ; flutter build apk + +sed -i.bak "s/implementation(name: 'pocketfhir-2', ext: 'aar') \/\/ Include PocketFHIR \.aar/implementation(name: 'pocketfhir', ext: 'aar') \/\/ Include PocketFHIR \.aar/" "$FILE_PATH" + +flutter clean ; flutter pub get ; flutter build apk + +cd ../pocketfhir \ No newline at end of file diff --git a/config/caddy/Caddyfile.fhirant b/config/caddy/Caddyfile.fhirant new file mode 100644 index 0000000..02653c6 --- /dev/null +++ b/config/caddy/Caddyfile.fhirant @@ -0,0 +1,52 @@ +# Listen on port 8081 for HTTP requests +10.0.0.16:8081 { + # Set the root directory for serving files, such as certificates + root * /absolute/path/to/storage/pki/authorities/local + + # Enable file server to serve static files like root.crt + handle_path /certs/* { + file_server browse + } + + # Reverse proxy configuration for PocketBase + handle { + reverse_proxy 127.0.0.1:8090 { + transport http { + read_timeout 360s + } + } + } + + # Logging setup + log { + output file /absolute/path/to/storage/caddy_debug.log { + roll_size 5MiB + roll_keep 10 + roll_keep_for 720h + } + format json + } +} + +# HTTPS server block for serving PocketBase via HTTPS +10.0.2.16:8443 { + # Enable TLS with automatic certificates (use internal certificates for testing) + tls internal + + # Set up reverse proxy for HTTPS + reverse_proxy 127.0.0.1:8090 { + transport http { + read_timeout 360s + } + } + + # Logging setup for HTTPS server + log { + output file /absolute/path/to/storage/caddy_https_debug.log { + roll_size 5MiB + roll_keep 10 + roll_keep_for 720h + } + format json + } +} diff --git a/config/caddy/temp.json b/config/caddy/temp.json new file mode 100644 index 0000000..bf8e7ca --- /dev/null +++ b/config/caddy/temp.json @@ -0,0 +1,212 @@ +{ + "logging": { + "logs": { + "default": { + "exclude": [ + "http.log.access.log0", + "http.log.access.log1" + ] + }, + "log0": { + "writer": { + "filename": "/absolute/path/to/storage/caddy_debug.log", + "output": "file", + "roll_keep": 10, + "roll_keep_days": 30, + "roll_size_mb": 5 + }, + "encoder": { + "format": "json" + }, + "include": [ + "http.log.access.log0" + ] + }, + "log1": { + "writer": { + "filename": "/absolute/path/to/storage/caddy_https_debug.log", + "output": "file", + "roll_keep": 10, + "roll_keep_days": 30, + "roll_size_mb": 5 + }, + "encoder": { + "format": "json" + }, + "include": [ + "http.log.access.log1" + ] + } + } + }, + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":8081" + ], + "routes": [ + { + "match": [ + { + "host": [ + "10.0.0.16" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "handler": "vars", + "root": "/absolute/path/to/storage/pki/authorities/local" + } + ] + }, + { + "group": "group2", + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "handler": "rewrite", + "strip_path_prefix": "/certs" + } + ] + }, + { + "handle": [ + { + "browse": {}, + "handler": "file_server", + "hide": [ + "./Caddyfile.fhirant" + ] + } + ] + } + ] + } + ], + "match": [ + { + "path": [ + "/certs/*" + ] + } + ] + }, + { + "group": "group2", + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "handler": "reverse_proxy", + "transport": { + "protocol": "http", + "read_timeout": 360000000000 + }, + "upstreams": [ + { + "dial": "127.0.0.1:8090" + } + ] + } + ] + } + ] + } + ] + } + ] + } + ], + "terminal": true + } + ], + "logs": { + "logger_names": { + "10.0.0.16": [ + "log0" + ] + } + } + }, + "srv1": { + "listen": [ + ":8443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "10.0.2.16" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "handler": "reverse_proxy", + "transport": { + "protocol": "http", + "read_timeout": 360000000000 + }, + "upstreams": [ + { + "dial": "127.0.0.1:8090" + } + ] + } + ] + } + ] + } + ], + "terminal": true + } + ], + "logs": { + "logger_names": { + "10.0.2.16": [ + "log1" + ] + } + } + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "subjects": [ + "10.0.2.16" + ], + "issuers": [ + { + "module": "internal" + } + ] + } + ] + } + } + } +} \ No newline at end of file diff --git a/pocketfhir/caddy.go b/pocketfhir/caddy.go index b280f18..e1b9c66 100644 --- a/pocketfhir/caddy.go +++ b/pocketfhir/caddy.go @@ -10,16 +10,66 @@ import ( _ "github.com/caddyserver/caddy/v2/modules/standard" ) -// CreateConfig generates a basic Caddy configuration to run a reverse proxy with HTTPS. -func CreateConfig(pbPort, httpPort, httpsPort, pbUrl, storagePath string) *caddy.Config { +// StartCaddy starts the Caddy server with the provided configuration. +func StartCaddy(pbPort, httpPort, httpsPort, pbUrl, storagePath, ipAddress string) { + // Change working directory + if err := os.Chdir(storagePath); err != nil { + log.Fatalf("Failed to change working directory to %s: %v", storagePath, err) + } + + // Log configuration for transparency + log.Printf("Starting Caddy server with the following configuration:") + log.Printf("PocketBase Port: %s", pbPort) + log.Printf("HTTP Port: %s", httpPort) + log.Printf("HTTPS Port: %s", httpsPort) + log.Printf("Upstream URL: %s", pbUrl) + log.Printf("Storage Path: %s", storagePath) + + // Generate Caddy config + cfg := createConfig(pbPort, httpPort, httpsPort, pbUrl, storagePath, ipAddress) + + // Serialize for debugging + configJSON, err := json.MarshalIndent(cfg, "", " ") + if err != nil { + log.Fatalf("Failed to serialize Caddy config: %v", err) + } + log.Printf("Generated Caddy config: %s", string(configJSON)) + + // Initialize Caddy + log.Println("Initializing Caddy...") + if err := caddy.Run(cfg); err != nil { + log.Fatalf("Error running Caddy: %v", err) + } + + log.Println("Caddy server started successfully.") +} + +// CreateConfig generates a basic Caddy configuration to run a reverse proxy with HTTP and a static file server. +func createConfig(pbPort, httpPort, httpsPort, pbUrl, storagePath, ipAddress string) *caddy.Config { // Use storagePath for the log file paths caddyDebugLogPath := fmt.Sprintf("%s/caddy_debug.log", storagePath) - accessLogPath := fmt.Sprintf("%s/access_log.log", storagePath) - // Define where certificates should be stored - certificateStoragePath := fmt.Sprintf("%s/caddy_certs", storagePath) + caddyHTTPSDebugLogPath := fmt.Sprintf("%s/caddy_https_debug.log", storagePath) + rootCertPath := fmt.Sprintf("%s/pki/authorities/local", storagePath) + + // Generate the JSON configuration + jsonConfig := jsonConfig(pbPort, httpPort, httpsPort, pbUrl, storagePath, ipAddress, caddyDebugLogPath, caddyHTTPSDebugLogPath, rootCertPath) + log.Printf("Generated JSON Configuration: %s", jsonConfig) + + // Parse the JSON into a caddy.Config struct + var caddyConfig caddy.Config + err := json.Unmarshal([]byte(jsonConfig), &caddyConfig) + if err != nil { + log.Fatalf("Failed to parse JSON configuration: %v", err) + } + + log.Printf("Generated Caddy Configuration: %s", jsonConfig) + + return &caddyConfig +} - // JSON Configuration with dynamically inserted paths - jsonConfig := fmt.Sprintf(`{ +func jsonConfig(pbPort, httpPort, httpsPort, pbUrl, storagePath, ipAddress, caddyDebugLogPath, caddyHTTPSDebugLogPath, rootCertPath string) string { + // JSON Configuration for Caddy with a basic file server + return fmt.Sprintf(`{ "logging": { "logs": { "default": { @@ -41,26 +91,56 @@ func CreateConfig(pbPort, httpPort, httpsPort, pbUrl, storagePath string) *caddy } } }, - "storage": { + "storage": { "module": "file_system", "root": "%s" }, "apps": { "http": { "http_port": %s, - "https_port": %s, "servers": { - "srv0": { - "listen": [ - ":%s" - ], + "srv_http": { + "listen": [":%s"], "routes": [ { + "match": [{"host": ["%s"]}], "handle": [ { - "handler": "request_body", - "max_size": 10000000 - }, + "handler": "reverse_proxy", + "transport": { + "protocol": "http", + "read_timeout": 360000000000 + }, + "upstreams": [ + { + "dial": "%s:%s" + } + ] + } + ] + }, + { + "handle": [ + { + "handler": "file_server", + "root": "%s", + "browse": true + } + ] + } + ], + "logs": { + "logger_names": { + "%s": ["log0"] + } + } + }, + "srv_https": { + "listen": [":%s"], + "routes": [ + { + "match": [{"host": ["%s"]}], + "handle": [ { "handler": "reverse_proxy", "transport": { @@ -75,7 +155,12 @@ func CreateConfig(pbPort, httpPort, httpsPort, pbUrl, storagePath string) *caddy } ] } - ] + ], + "logs": { + "logger_names": { + "%s": ["log1"] + } + } } } }, @@ -93,50 +178,5 @@ func CreateConfig(pbPort, httpPort, httpsPort, pbUrl, storagePath string) *caddy } } } - }`, caddyDebugLogPath, accessLogPath, certificateStoragePath, httpPort, httpsPort, httpsPort, pbUrl, pbPort) - - // Parse the JSON into a caddy.Config struct - var caddyConfig caddy.Config - err := json.Unmarshal([]byte(jsonConfig), &caddyConfig) - if err != nil { - log.Fatalf("Failed to parse JSON configuration: %v", err) - } - - log.Printf("Generated Caddy Configuration: %s", jsonConfig) - - return &caddyConfig -} - -// StartCaddy starts the Caddy server with the provided configuration. -func StartCaddy(pbPort, httpPort, httpsPort, pbUrl, storagePath string) { - // Change working directory - if err := os.Chdir(storagePath); err != nil { - log.Fatalf("Failed to change working directory to %s: %v", storagePath, err) - } - - // Log configuration for transparency - log.Printf("Starting Caddy server with the following configuration:") - log.Printf("PocketBase Port: %s", pbPort) - log.Printf("HTTP Port: %s", httpPort) - log.Printf("HTTPS Port: %s", httpsPort) - log.Printf("Upstream URL: %s", pbUrl) - log.Printf("Storage Path: %s", storagePath) - - // Generate Caddy config - cfg := CreateConfig(pbPort, httpPort, httpsPort, pbUrl, storagePath) - - // Serialize for debugging - configJSON, err := json.MarshalIndent(cfg, "", " ") - if err != nil { - log.Fatalf("Failed to serialize Caddy config: %v", err) - } - log.Printf("Generated Caddy config: %s", string(configJSON)) - - // Initialize Caddy - log.Println("Initializing Caddy...") - if err := caddy.Run(cfg); err != nil { - log.Fatalf("Error running Caddy: %v", err) - } - - log.Println("Caddy server started successfully.") + }`, caddyDebugLogPath, caddyHTTPSDebugLogPath, storagePath, httpPort, httpPort, ipAddress, pbUrl, pbPort, storagePath, ipAddress, httpsPort, ipAddress, pbUrl, pbPort, ipAddress) } diff --git a/pocketfhir/start.go b/pocketfhir/start.go index 1d7b3d9..02c7552 100644 --- a/pocketfhir/start.go +++ b/pocketfhir/start.go @@ -11,6 +11,7 @@ import ( func StartPocketFHIR( pbPort string, httpPort string, httpsPort string, pbUrl string, ipAddress string, dataDir string, enableApiLogs bool, storagePath string) { + // Set environment variables for PocketBase configuration log.Println("[DEBUG] Setting environment variables...") if err := os.Setenv("POCKETBASE_DATA_DIR", dataDir); err != nil { @@ -31,7 +32,7 @@ func StartPocketFHIR( // Start the Caddy server in a separate goroutine go func() { log.Println("[DEBUG] Starting Caddy server with HTTPS...") - StartCaddy(pbPort, httpPort, httpsPort, pbUrl, storagePath) + StartCaddy(pbPort, httpPort, httpsPort, pbUrl, storagePath, ipAddress) }() // Wait for interrupt signal to gracefully shut down the server