Skip to content

Commit 6d23345

Browse files
authored
Support network config (#47)
1 parent e4b523c commit 6d23345

File tree

7 files changed

+52
-17
lines changed

7 files changed

+52
-17
lines changed

aiscript-runtime/src/config/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ pub struct Config {
2727
pub auth: AuthConfig,
2828
#[serde(default)]
2929
pub sso: SsoConfig,
30+
#[serde(default)]
31+
pub network: NetworkConfig,
32+
}
33+
34+
#[derive(Debug, Deserialize, Default)]
35+
pub struct NetworkConfig {
36+
#[serde(default = "default_host")]
37+
pub host: String,
38+
#[serde(default = "default_port")]
39+
pub port: u16,
40+
}
41+
42+
fn default_host() -> String {
43+
"127.0.0.1".to_string()
44+
}
45+
46+
fn default_port() -> u16 {
47+
8080
3048
}
3149

3250
#[derive(Debug, Deserialize, Default)]

aiscript-runtime/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ async fn run_server(
269269
// Add the fallback handler to the router
270270
router = router.fallback(handle_404);
271271

272-
let addr = SocketAddr::from(([0, 0, 0, 0], port));
272+
let addr = format!("{}:{}", config.network.host, port)
273+
.parse::<SocketAddr>()
274+
.unwrap();
275+
println!("Server listening on http://{}", addr);
276+
273277
let listener = TcpListener::bind(addr).await.unwrap();
274278

275279
match reload_rx {

aiscript/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ enum Commands {
3131
#[arg(value_name = "FILE")]
3232
file: Option<PathBuf>,
3333
/// The web server listening port.
34-
#[arg(short, long, default_value_t = 8080)]
35-
port: u16,
34+
#[arg(short, long)]
35+
port: Option<u16>,
3636
/// Reload the file on change
3737
#[arg(short, long, default_value_t = false)]
3838
reload: bool,
@@ -53,7 +53,7 @@ async fn main() {
5353
let cli = AIScriptCli::parse();
5454
match cli.command {
5555
Some(Commands::Serve { file, port, reload }) => {
56-
println!("Server listening on port http://localhost:{}", port);
56+
let port = port.unwrap_or(config.network.port);
5757
aiscript_runtime::run(file, port, reload).await;
5858
}
5959
Some(Commands::New { name }) => {

examples/project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[network]
2+
host = "0.0.0.0"
3+
port = 5042
4+
15
[apidoc]
26
enabled = true
37
type = "swagger"

examples/routes/nested/gist.ai

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ get /gists {
33
return "gist";
44
}
55

6-
get /gists/<id:int> {
6+
get /gists/:id {
77
"""Get gist by id"""
8-
return "gist: " + id;
8+
path {
9+
id: str,
10+
}
11+
return "gist: " + path.id;
912
}
1013

11-
delete /gists/<id:int> {
14+
delete /gists/:id {
1215
"""Delete gist by id"""
13-
return "delete gist: " + id;
16+
path {
17+
id: str,
18+
}
19+
return "delete gist: " + path.id;
1420
}

examples/routes/poc.ai

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ route /poc {
55
query {
66
"""hello content"""
77
@string(min_len=3, max_len=30)
8-
content: str
8+
content: str,
99
"""hello age"""
10-
age: int = 18
10+
age: int = 18,
1111
}
1212

1313
print(query);
@@ -25,24 +25,24 @@ route /poc {
2525
"""Post and put hello API"""
2626
query {
2727
@string(min_len=3, max_len=30)
28-
content: str
28+
content: str,
2929
"""age"""
30-
age: int = 18
30+
age: int = 18,
3131
@in(["ios", "android"])
32-
platform: str = "ios"
32+
platform: str = "ios",
3333
}
3434

3535
@json
3636
body {
3737
"""test flag"""
38-
test: bool = true
38+
test: bool = true,
3939
}
4040

4141
ai fn ask(question) {
42-
return prompt content;
42+
return prompt question;
4343
}
4444

45-
let answer = ask(content);
45+
let answer = ask(query.content);
4646
return "AI answer: " + answer;
4747
}
4848
}

examples/routes/repo.ai

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ post /repo {
2020
return "create repo: " + name;
2121
}
2222

23-
put /repo/<id:int> {
23+
put /repo/:id {
2424
"""Update a repository API"""
25+
path {
26+
id: str
27+
}
2528
body {
2629
"""The repository name"""
2730
@string(min_len=3, max_len=30)

0 commit comments

Comments
 (0)