Skip to content

Commit

Permalink
api 区分为 admin 和 api (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgisky1980 committed Jun 12, 2024
1 parent b0e5160 commit a71e045
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ It only supports Safetensors models with the `.st` extension now. Models saved w
## 📙Currently Available APIs

The API service starts at port 65530, and the data input and output format follow the Openai API specification.
Note that some APIs like `chat` and `completions` have additional optional fields for advanced functionalities. Visit http://localhost:65530/swagger-ui for API schema.
Note that some APIs like `chat` and `completions` have additional optional fields for advanced functionalities. Visit http://localhost:65530/api-docs for API schema.

* `/api/oai/v1/models`
* `/api/oai/models`
Expand Down
2 changes: 1 addition & 1 deletion README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
## 📙目前可用的API

API 服务开启于 65530 端口, 数据输入输出格式遵循 Openai API 规范。
有一些 API,比如`chat``completions`有一些可选的额外字段,这些额外字段是为高级功能准备的。可以访问 http://localhost:65530/swagger-ui 查看具体的 API 参数。
有一些 API,比如`chat``completions`有一些可选的额外字段,这些额外字段是为高级功能准备的。可以访问 http://localhost:65530/api-docs 查看具体的 API 参数。

- `/api/oai/v1/models`
- `/api/oai/models`
Expand Down
4 changes: 2 additions & 2 deletions assets/configs/Config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ slot = "permisionkey"
tls = false

[[listen.app_keys]] # Allow mutiple app keys.
app_id = "JUSTAISERVER"
secret_key = "JUSTSECRET_KEY"
app_id = "admin"
secret_key = "ai00_is_good"

[web] # Remove this to disable WebUI.
path = "assets/www/index.zip" # Path to the WebUI.
4 changes: 2 additions & 2 deletions assets/www/index.zip
Git LFS file not shown
34 changes: 20 additions & 14 deletions crates/ai00-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ async fn main() {

let serve_path = match config.web {
Some(web) => {
let path = tempfile::tempdir()
.expect("create temp dir failed")
.into_path();
if Path::new("assets/temp").exists() {
std::fs::remove_dir_all("assets/temp").expect("delete temp dir failed");
}

std::fs::create_dir("assets/temp").expect("create plugins dir failed");
let path = PathBuf::from("assets/temp");

load_web(web.path, &path)
.await
.expect("load frontend failed");
Expand Down Expand Up @@ -189,29 +193,30 @@ async fn main() {
.allow_headers(AllowHeaders::any())
.into_handler();

let auth_handler: JwtAuth<JwtClaims, _> =
let admin_auth: JwtAuth<JwtClaims, _> =
JwtAuth::new(ConstDecoder::from_secret(config.listen.slot.as_bytes()))
.finders(vec![
Box::new(HeaderFinder::new()),
Box::new(QueryFinder::new("_token")),
Box::new(QueryFinder::new("admin_token")),
// Box::new(CookieFinder::new("jwt_token")),
])
.force_passed(listen.force_pass.unwrap_or_default());

let api_router = Router::with_hoop(auth_handler)
.push(Router::with_path("/adapters").get(api::adapter::adapters))
.push(Router::with_path("/models/info").get(api::model::info))
let admin_router = Router::with_hoop(admin_auth)
.push(Router::with_path("/models/save").post(api::model::save))
.push(Router::with_path("/models/load").post(api::model::load))
.push(Router::with_path("/models/unload").get(api::model::unload))
.push(Router::with_path("/models/state/load").post(api::model::load_state))
.push(Router::with_path("/models/state").get(api::model::state))
.push(Router::with_path("/models/list").get(api::file::models))
.push(Router::with_path("/files/unzip").post(api::file::unzip))
.push(Router::with_path("/files/dir").post(api::file::dir))
.push(Router::with_path("/files/ls").post(api::file::dir))
.push(Router::with_path("/files/config/load").post(api::file::load_config))
.push(Router::with_path("/files/config/save").post(api::file::save_config))
.push(Router::with_path("/files/config/save").post(api::file::save_config));
let api_router = Router::new()
.push(Router::with_path("/adapters").get(api::adapter::adapters))
.push(Router::with_path("/models/info").get(api::model::info))
.push(Router::with_path("/models/list").get(api::file::models))
.push(Router::with_path("/models/state").get(api::model::state))
.push(Router::with_path("/oai/models").get(api::oai::models))
.push(Router::with_path("/oai/v1/models").get(api::oai::models))
.push(Router::with_path("/oai/completions").post(api::oai::completions))
Expand All @@ -237,13 +242,14 @@ async fn main() {
Router::with_path("/api")
.push(Router::with_path("/auth/exchange").post(api::auth::exchange))
.push(api_router),
);
)
.push(Router::with_path("/admin").push(admin_router));

let doc = OpenApi::new(bin_name, version).merge_router(&app);

let app = app
.push(doc.into_router("/api-doc/openapi.json"))
.push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger-ui"));
.push(doc.into_router("/api-docs/openapi.json"))
.push(SwaggerUi::new("/api-docs/openapi.json").into_router("api-docs"));
// this static serve should be after `swagger`
let app = match serve_path {
Some(path) => app
Expand Down

0 comments on commit a71e045

Please sign in to comment.