Skip to content

Support network config #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions aiscript-runtime/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ pub struct Config {
pub auth: AuthConfig,
#[serde(default)]
pub sso: SsoConfig,
#[serde(default)]
pub network: NetworkConfig,
}

#[derive(Debug, Deserialize, Default)]
pub struct NetworkConfig {
#[serde(default = "default_host")]
pub host: String,
#[serde(default = "default_port")]
pub port: u16,
}

fn default_host() -> String {
"127.0.0.1".to_string()
}

fn default_port() -> u16 {
8080
}

#[derive(Debug, Deserialize, Default)]
Expand Down
6 changes: 5 additions & 1 deletion aiscript-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ async fn run_server(
// Add the fallback handler to the router
router = router.fallback(handle_404);

let addr = SocketAddr::from(([0, 0, 0, 0], port));
let addr = format!("{}:{}", config.network.host, port)
.parse::<SocketAddr>()
.unwrap();
println!("Server listening on http://{}", addr);

let listener = TcpListener::bind(addr).await.unwrap();

match reload_rx {
Expand Down
6 changes: 3 additions & 3 deletions aiscript/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ enum Commands {
#[arg(value_name = "FILE")]
file: Option<PathBuf>,
/// The web server listening port.
#[arg(short, long, default_value_t = 8080)]
port: u16,
#[arg(short, long)]
port: Option<u16>,
/// Reload the file on change
#[arg(short, long, default_value_t = false)]
reload: bool,
Expand All @@ -53,7 +53,7 @@ async fn main() {
let cli = AIScriptCli::parse();
match cli.command {
Some(Commands::Serve { file, port, reload }) => {
println!("Server listening on port http://localhost:{}", port);
let port = port.unwrap_or(config.network.port);
aiscript_runtime::run(file, port, reload).await;
}
Some(Commands::New { name }) => {
Expand Down
4 changes: 4 additions & 0 deletions examples/project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[network]
host = "0.0.0.0"
port = 5042

[apidoc]
enabled = true
type = "swagger"
Expand Down
14 changes: 10 additions & 4 deletions examples/routes/nested/gist.ai
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ get /gists {
return "gist";
}

get /gists/<id:int> {
get /gists/:id {
"""Get gist by id"""
return "gist: " + id;
path {
id: str,
}
return "gist: " + path.id;
}

delete /gists/<id:int> {
delete /gists/:id {
"""Delete gist by id"""
return "delete gist: " + id;
path {
id: str,
}
return "delete gist: " + path.id;
}
16 changes: 8 additions & 8 deletions examples/routes/poc.ai
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ route /poc {
query {
"""hello content"""
@string(min_len=3, max_len=30)
content: str
content: str,
"""hello age"""
age: int = 18
age: int = 18,
}

print(query);
Expand All @@ -25,24 +25,24 @@ route /poc {
"""Post and put hello API"""
query {
@string(min_len=3, max_len=30)
content: str
content: str,
"""age"""
age: int = 18
age: int = 18,
@in(["ios", "android"])
platform: str = "ios"
platform: str = "ios",
}

@json
body {
"""test flag"""
test: bool = true
test: bool = true,
}

ai fn ask(question) {
return prompt content;
return prompt question;
}

let answer = ask(content);
let answer = ask(query.content);
return "AI answer: " + answer;
}
}
5 changes: 4 additions & 1 deletion examples/routes/repo.ai
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ post /repo {
return "create repo: " + name;
}

put /repo/<id:int> {
put /repo/:id {
"""Update a repository API"""
path {
id: str
}
body {
"""The repository name"""
@string(min_len=3, max_len=30)
Expand Down
Loading