Skip to content

Commit

Permalink
Stay dead this time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dokotela committed Nov 21, 2024
1 parent 5822cfb commit 105c390
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 146 deletions.
4 changes: 2 additions & 2 deletions build/build_ios.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ gomobile init

# Build PocketFHIR .framework for iOS
echo "Building PocketFHIR .framework for iOS..."
gomobile bind -target=ios -o pocketfhir.framework ./pocketfhir
gomobile bind -target=ios -o pocketfhir.xcframework ./pocketfhir
if [ $? -ne 0 ]; then
echo "iOS build failed!"
exit 1
fi

# Move the framework to the appropriate iOS directory
mv pocketfhir.framework ../fhir_ant/ios/libs/pocketfhir.framework
mv pocketfhir.xcframework ../fhir_ant/ios/libs/pocketfhir.xcframework

echo "PocketFHIR iOS build completed successfully."
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ require (
golang.org/x/crypto/x509roots/fallback v0.0.0-20240507223354-67b13616a595 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/image v0.22.0 // indirect
golang.org/x/mobile v0.0.0-20241108191957-fa514ef75a0f // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.9.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,8 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20241108191957-fa514ef75a0f h1:23H/YlmTHfmmvpZ+ajKZL0qLz0+IwFOIqQA0mQbmLeM=
golang.org/x/mobile v0.0.0-20241108191957-fa514ef75a0f/go.mod h1:UbSUP4uu/C9hw9R2CkojhXlAxvayHjBdU9aRvE+c1To=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func main() {
dataDir := "./assets" // The directory to use for PocketBase data
pbIpAddress := "127.0.0.1" // PocketBase IP address for local development
caddyIpAddress := "127.0.0.1" // Caddy server loopback IP address to prevent conflict
pbPort := "8090" // PocketBase port set to 8080
pbPort := "8090" // PocketBase port set to 8090
httpPort := "8081" // Caddy HTTP traffic port
httpsPort := "8443" // Caddy HTTPS traffic port
enableApiLogs := true // Enable API logs for detailed local debugging
Expand Down
16 changes: 8 additions & 8 deletions pocketfhir/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type CaddyConfig struct {
IpAddress string
}

// StartCaddy starts the Caddy server with the provided configuration.
func StartCaddy(config CaddyConfig) {
// startCaddyInstance initializes and starts the Caddy server instance.
func startCaddyInstance(config CaddyConfig) {
// Change working directory
if err := os.Chdir(config.StoragePath); err != nil {
log.Fatalf("Failed to change working directory to %s: %v", config.StoragePath, err)
Expand All @@ -30,7 +30,7 @@ func StartCaddy(config CaddyConfig) {
// Log consolidated configuration details
log.Printf("Starting Caddy server with configuration: %+v", config)

// Generate Caddy config
// Generate the Caddy config
caddyCfg := createConfig(config)

// Serialize for debugging
Expand All @@ -40,12 +40,12 @@ func StartCaddy(config CaddyConfig) {
}
log.Printf("Generated Caddy config: %s", string(configJSON))

// Initialize and run Caddy
log.Println("Initializing Caddy...")
if err := caddy.Run(caddyCfg); err != nil {
log.Fatalf("Error running Caddy: %v", err)
// Load Caddy with the generated config JSON
log.Println("Loading Caddy configuration...")
err = caddy.Load(configJSON, true)
if err != nil {
log.Fatalf("[ERROR] Failed to load Caddy configuration: %v", err)
}

log.Println("Caddy server started successfully.")
}

Expand Down
31 changes: 31 additions & 0 deletions pocketfhir/native_bridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pocketfhir

import (
"log"
)

// Java Callbacks, make sure to register them before starting pocketbase
// to expose any method to java, add that with FirstLetterCapital
var nativeBridge NativeBridge

// RegisterNativeBridgeCallback allows setting the NativeBridge interface for callbacks
func RegisterNativeBridgeCallback(c NativeBridge) {
nativeBridge = c
}

// Helper methods

// NativeBridge interface to define the methods used for native callbacks
type NativeBridge interface {
handleCallback(string, string) string
}

// sendCommand sends command to native and returns the response
func sendCommand(command string, data string) string {
if nativeBridge != nil {
log.Printf("[DEBUG] Sending command '%s' with data: %s", command, data)
return nativeBridge.handleCallback(command, data)
}
log.Printf("[DEBUG] No NativeBridge defined. Command '%s' not sent.", command)
return ""
}
80 changes: 7 additions & 73 deletions pocketfhir/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,100 +13,34 @@ import (
"github.com/pocketbase/pocketbase/core"
)

// Java Callbacks, make sure to register them before starting pocketbase
// to expose any method to java, add that with FirstLetterCapital
var nativeBridge NativeBridge

// RegisterNativeBridgeCallback allows setting the NativeBridge interface for callbacks
func RegisterNativeBridgeCallback(c NativeBridge) {
nativeBridge = c
}

// RunServer starts the PocketFHIR server
func RunServer(dataDir string, ipAddress string, pbPort string, enableApiLogs bool) {
// runServerInstance handles the setup and running of the PocketBase server instance.
func runServerInstance(app *pocketbase.PocketBase, ipAddress, pbPort string, enableApiLogs bool) {
// Set CLI-like arguments for PocketBase to specify server address and port
log.Printf("[DEBUG] Setting CLI arguments for server address and port: %s:%s\n", ipAddress, pbPort)
os.Args = []string{os.Args[0], "serve", "--http", fmt.Sprintf("%s:%s", ipAddress, pbPort)}

// Create a configuration object with custom settings
log.Println("[DEBUG] Creating PocketBase configuration object...")
config := pocketbase.Config{
DefaultDataDir: dataDir,
DefaultDev: enableApiLogs, // Enabling dev mode for detailed logging
HideStartBanner: false,
}

// Initialize PocketBase with the default configuration
log.Println("[DEBUG] Initializing PocketBase app...")
app := pocketbase.NewWithConfig(config)
log.Println("[DEBUG] PocketBase app initialized.")

// Standard setup for the app
log.Println("[DEBUG] Running standard setup for PocketBase...")
standard(app)
log.Println("[DEBUG] Standard setup for PocketBase app completed.")

// Register hooks from hooks.go
log.Println("[DEBUG] Registering hooks...")
// Register hooks, FHIR routes, and management routes
registerHooks(app)
log.Println("[DEBUG] Hooks registered.")

// Register FHIR routes
log.Println("[DEBUG] Registering FHIR routes...")
registerFHIRRoutes(app)
log.Println("[DEBUG] FHIR routes registered.")

// Register server management routes
log.Println("[DEBUG] Registering server management routes...")
registerManagementRoutes(app)
log.Println("[DEBUG] Server management routes registered.")

// Setup additional native callbacks and routes
log.Println("[DEBUG] Setting up PocketBase callbacks and routes...")
setupPocketbaseCallbacks(app, enableApiLogs)
log.Println("[DEBUG] PocketBase callbacks and routes set up.")

// Initialize collections if necessary
log.Println("[DEBUG] Initializing collections...")
if err := initializeCollections(app); err != nil {
log.Printf("[ERROR] Failed to initialize collections: %v", err)
return
}
log.Println("[DEBUG] Collections initialized successfully.")

// Start the server in a separate goroutine
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
log.Println("[DEBUG] Calling app.Start() to start the server...")
if err := app.Start(); err != nil {
sendCommand("error", fmt.Sprintf("Error: Failed to start PocketBase server: %v", err))
log.Printf("[ERROR] Failed to start the app: %v", err)
} else {
log.Println("[DEBUG] PocketFHIR server started successfully.")
}
}()

// Wait for server to complete
wg.Wait()
}

// Helper methods

// NativeBridge interface to define the methods used for native callbacks
type NativeBridge interface {
HandleCallback(string, string) string
}

// sendCommand sends command to native and returns the response
func sendCommand(command string, data string) string {
if nativeBridge != nil {
log.Printf("[DEBUG] Sending command '%s' with data: %s", command, data)
return nativeBridge.HandleCallback(command, data)
// Start the server
log.Println("[DEBUG] Starting PocketBase server instance...")
if err := app.Start(); err != nil {
log.Fatalf("[ERROR] Failed to start PocketBase server: %v", err)
}
log.Printf("[DEBUG] No NativeBridge defined. Command '%s' not sent.", command)
return ""
}

// setupPocketbaseCallbacks sets up additional callbacks and native routes for PocketBase
Expand Down
62 changes: 0 additions & 62 deletions pocketfhir/start.go

This file was deleted.

Loading

0 comments on commit 105c390

Please sign in to comment.