Skip to content

Commit

Permalink
so close
Browse files Browse the repository at this point in the history
  • Loading branch information
Dokotela committed Nov 19, 2024
1 parent 962f947 commit f86bd15
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 30 deletions.
117 changes: 90 additions & 27 deletions pocketfhir/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,114 @@ import (
_ "github.com/caddyserver/caddy/v2/modules/standard"
)

// CreateConfig generates a basic Caddy configuration to run a reverse proxy.
func CreateConfig(port, upstreamURL, storagePath string) *caddy.Config {
return &caddy.Config{
Admin: &caddy.AdminConfig{
Listen: fmt.Sprintf(":%s", port),
// CreateConfig generates a basic Caddy configuration to run a reverse proxy with HTTPS.
func CreateConfig(port, upstreamURL, storagePath, certFile, keyFile 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)

// JSON Configuration with dynamically inserted paths and certificates
jsonConfig := fmt.Sprintf(`{
"logging": {
"logs": {
"default": {
"level": "DEBUG",
"writer": {
"output": "file",
"filename": "%s"
}
},
"http.access": {
"level": "DEBUG",
"writer": {
"output": "file",
"filename": "%s"
},
"encoder": {
"format": "json"
}
}
}
},
StorageRaw: json.RawMessage(fmt.Sprintf(`{
"module": "file_system",
"root": "%s"
}`, storagePath)),
AppsRaw: map[string]json.RawMessage{
"http": json.RawMessage(fmt.Sprintf(`{
"apps": {
"http": {
"servers": {
"simple_reverse_proxy": {
"listen": [":%s"],
"routes": [{
"handle": [{
"handler": "reverse_proxy",
"upstreams": [{
"dial": "%s"
}]
}]
}]
"srv0": {
"listen": [
":%s"
],
"routes": [
{
"handle": [
{
"handler": "request_body",
"max_size": 10000000
},
{
"handler": "reverse_proxy",
"transport": {
"protocol": "http",
"read_timeout": 360000000000
},
"upstreams": [
{
"dial": "127.0.0.1:8090"
}
]
}
]
}
]
}
}
}`, port, upstreamURL)), // Ensure upstreamURL is used correctly
}
},
"tls": {
"certificates": {
"automate": ["*"],
"load_files": [
{
"certificate": "%s",
"key": "%s"
}
]
}
}
}`, caddyDebugLogPath, accessLogPath, port, certFile, keyFile)

// 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
}

func StartCaddy(port, upstreamURL, storagePath string) {
// StartCaddy starts the Caddy server with the provided configuration.
func StartCaddy(port, upstreamURL, storagePath, certFile, keyFile string) {
// Change working directory
if err := os.Chdir(storagePath); err != nil {
log.Fatalf("Failed to change working directory to %s: %v", storagePath, err)
}

log.Printf("Starting Caddy with configuration:\nPort: %s\nUpstreamURL: %s\nStoragePath: %s\n", port, upstreamURL, storagePath)
// Log configuration for transparency
log.Printf("Starting Caddy with configuration:\nPort: %s\nUpstreamURL: %s\nStoragePath: %s\nCertFile: %s\nKeyFile: %s\n", port, upstreamURL, storagePath, certFile, keyFile)

// Generate Caddy config
cfg := CreateConfig(port, upstreamURL, storagePath, certFile, keyFile)

cfg := CreateConfig(port, upstreamURL, storagePath)
configJSON, err := json.Marshal(cfg)
// 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))

// Start the Caddy server (no blocking)
// Initialize Caddy
log.Println("Initializing Caddy...")
if err := caddy.Run(cfg); err != nil {
log.Fatalf("Error running Caddy: %v", err)
Expand Down
8 changes: 5 additions & 3 deletions pocketfhir/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"os"
"os/signal"
"syscall"

_ "github.com/caddyserver/caddy/v2/modules/standard"
)

func StartPocketFHIR(dataDir string, hostname string, port string, getApiLogs bool, caddyPort string, caddyStoragePath string) {
func StartPocketFHIR(dataDir string, hostname string, port string, getApiLogs bool, caddyPort string, caddyStoragePath string, certFile string, keyFile string) {
// Set environment variables for PocketBase configuration
log.Println("[DEBUG] Setting environment variables...")
if err := os.Setenv("POCKETBASE_DATA_DIR", dataDir); err != nil {
Expand All @@ -28,8 +30,8 @@ func StartPocketFHIR(dataDir string, hostname string, port string, getApiLogs bo

// Start the Caddy server in a separate goroutine
go func() {
log.Println("[DEBUG] Starting Caddy server...")
StartCaddy(caddyPort, fmt.Sprintf("http://%s:%s", hostname, port), caddyStoragePath)
log.Println("[DEBUG] Starting Caddy server with HTTPS...")
StartCaddy(caddyPort, fmt.Sprintf("http://%s:%s", hostname, port), caddyStoragePath, certFile, keyFile)
}()

// Wait for interrupt signal to gracefully shut down the server
Expand Down

0 comments on commit f86bd15

Please sign in to comment.