Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/tauri-main' into zod-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
stephlow committed Oct 14, 2024
2 parents fb8effb + 381bc65 commit deada04
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
13 changes: 6 additions & 7 deletions fpx-app/src/api_manager/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,29 @@ pub struct ApiManager {
}

impl ApiManager {
/// Start a API server. If a API pid is already set, then that will first be
/// Start an API server. If an API pid is already set, then that will first be
/// shutdown.
pub fn start_api(&self, fpx_config: FpxConfig) {
// Get a lock for the duration of this function
let mut api_pid = self.api_pid.lock().expect("lock is poisoned");

// If there is a API pid already there, then first send the SIGTERM
// If there is an API pid already there, then first send the SIGTERM
// signal to that process group.
if let Some(api_pid) = api_pid.take() {
// shutdown any existing api server
send_sigterm_signal(api_pid);
}

let listen_port = fpx_config.listen_port();

// Create some environment variables overrides based on the fpx.toml
let mut envs: Vec<(&str, String)> = vec![];
if let Some(listen_port) = fpx_config.listen_port {
envs.push(("FPX_PORT", listen_port.to_string()));
}
let envs: Vec<(&str, String)> = vec![("FPX_PORT", listen_port.to_string())];

// Start the process using pnpm. The process_group=0 will ensure that
// the process group ID is the same as the root process ID.
let mut child_process = process::Command::new("pnpm")
.arg("dev:api")
.process_group(0) //
.process_group(0)
.envs(envs)
.spawn()
.expect("failed to execute pnpm dev:api");
Expand Down
7 changes: 5 additions & 2 deletions fpx-app/src/models/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ use serde::{Deserialize, Serialize};
#[derive(JsonSchema, Deserialize, Serialize, Clone)]
pub struct Workspace {
path: String,
config: FpxConfig,
api_port: u32,
}

impl Workspace {
pub fn new(path: String, config: FpxConfig) -> Self {
Self { path, config }
Self {
path,
api_port: config.listen_port(),
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions fpx/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use std::{env, ops::Range};
use thiserror::Error;
use tracing::error;

const DEFAULT_PORT: u32 = 8788;

#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq, Default, JsonSchema)]
pub struct FpxConfig {
/// The port on which the API server should listen.
Expand Down Expand Up @@ -48,6 +50,12 @@ impl FpxConfig {

Ok((config, config_file_path))
}

/// Unwraps the configured listen_port in the workspace' `fpx.toml` if it's set or return the
/// default port
pub fn listen_port(&self) -> u32 {
self.listen_port.unwrap_or(DEFAULT_PORT)
}
}

#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq, JsonSchema, Error)]
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const SpanAddedSchema = memoizeOne(() =>
export type SpanAdded = z.infer<ReturnType<typeof SpanAddedSchema>>;

export const WorkspaceSchema = memoizeOne(() =>
z.object({ config: z.lazy(FpxConfigSchema), path: z.string() }),
z.object({ api_port: z.number().int(), path: z.string() }),
);

export type Workspace = z.infer<ReturnType<typeof WorkspaceSchema>>;
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function TauriRuntime({ children }: RuntimeProviderProps) {

const handleGetApiBaseUrl = useHandler(() => {
if (workspace) {
return `http://localhost:${workspace.config.listen_port}`;
return `http://localhost:${workspace.api_port}`;
}

return "";
Expand Down

0 comments on commit deada04

Please sign in to comment.