feat: add models list endpoint
This commit is contained in:
+13
-42
@@ -1,34 +1,14 @@
|
||||
mod auth;
|
||||
mod providers;
|
||||
mod routes;
|
||||
mod state;
|
||||
|
||||
use crate::auth::jwt::Claims;
|
||||
use crate::auth::middleware::auth_middleware;
|
||||
use crate::providers::ollama::OllamaProvider;
|
||||
use crate::state::app_state::AppState;
|
||||
|
||||
use axum::extract::Extension;
|
||||
use axum::{Router, middleware, routing::get};
|
||||
use axum::Router;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
pub async fn protected_route(Extension(claims): Extension<Claims>) -> String {
|
||||
format!(
|
||||
"Hello {}, your user id is {}",
|
||||
claims.preferred_username.unwrap_or("unknown".to_string()),
|
||||
claims.sub
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn public_route() -> &'static str {
|
||||
println!("Public route hit");
|
||||
"Public endpoint: no authentication required"
|
||||
}
|
||||
|
||||
pub fn app() -> Router {
|
||||
let public_routes = Router::new().route("/", get(public_route));
|
||||
|
||||
let protected_routes = Router::new()
|
||||
.route("/protected", get(protected_route))
|
||||
.layer(middleware::from_fn(auth_middleware));
|
||||
|
||||
Router::new().merge(public_routes).merge(protected_routes)
|
||||
}
|
||||
use std::sync::Arc;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -37,23 +17,14 @@ async fn main() {
|
||||
dotenvy::dotenv().ok();
|
||||
}
|
||||
|
||||
// let jwks = keycloak::get_jwks()
|
||||
// .await
|
||||
// .expect("Failed to fetch JWKS");
|
||||
let state = AppState {
|
||||
ollama: Arc::new(OllamaProvider::new("http://localhost:11434")),
|
||||
};
|
||||
|
||||
// // println!("{:?}", jwks);
|
||||
let app = Router::new()
|
||||
.nest("/v1", routes::v1::router())
|
||||
.with_state(state);
|
||||
|
||||
// let token = "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJublpLek04TkZHVmpWbGFPRXZpMUtFSTVHQWRwaGlsYjh3RHRLeG5JOENZIn0.eyJleHAiOjE3NzU3Mzg2MDUsImlhdCI6MTc3NTczODMwNSwianRpIjoiNjk2OTY4NzQtZWMwNi00NGFkLTg0MDYtYmY3YWM4MjI5MjkxIiwiaXNzIjoiaHR0cHM6Ly9hdXRoLmljZWJlcmcuYmxhY2svcmVhbG1zL2ljZWJlcmciLCJzdWIiOiJmZGRiN2FjZC1kMmE5LTRmMTctOWIxNi1kZjVlN2EzNDI4YjciLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJjaGF0LWFwaSIsInNjb3BlIjoiIiwiY2xpZW50SG9zdCI6Ijg2LjIxMi44NC4xOTEiLCJjbGllbnRBZGRyZXNzIjoiODYuMjEyLjg0LjE5MSIsImNsaWVudF9pZCI6ImNoYXQtYXBpIn0.BS7ohLWiMDxAUz_Q-Qi2UoLYbNn8AUrYeSWeO-602SQ-AYBW3gfYxXOSeRgWyn4VfObpVfK7QfqQBUxorXxi1JVld-4fGXL8NXQNyq5Ip_JHNG1p02Z39Pe9MmC9MXOwA_GQF2PIkLIdOJ_W_guXVhl2ptEWPPSiXM5Z5CNg8lyOiKPI0g2JWV6FBRG-HMXzqnxAb1j8wGUpC9JzGwAU3sjWBGhT1AAovs-XLmm5hZEPxI-Ia3SmUnF-QjFMmebPVxLdxL7OszzVEhKipsZRiwQxjY6eJhJFFa8uycBigHPSzu_HqqkK6AjNlyExvR0EGvl9zUWdOfMPDiVX2Sg92g";
|
||||
// match jwt::validate_token(token, &jwks) {
|
||||
// Ok(claims) => {
|
||||
// println!("Valid token for user: {:?}", claims);
|
||||
// }
|
||||
// Err(err) => {
|
||||
// println!("Invalid token: {}", err);
|
||||
// }
|
||||
// }
|
||||
|
||||
let app = app();
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
||||
println!("Server running on {}", addr);
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
pub mod ollama;
|
||||
@@ -0,0 +1,25 @@
|
||||
use reqwest::Client;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OllamaProvider {
|
||||
pub client: Client,
|
||||
pub base_url: String,
|
||||
}
|
||||
|
||||
impl OllamaProvider {
|
||||
pub fn new(base_url: impl Into<String>) -> Self {
|
||||
Self {
|
||||
client: Client::new(),
|
||||
base_url: base_url.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list_models(&self) -> Result<Value, reqwest::Error> {
|
||||
let url = format!("{}/api/tags", self.base_url);
|
||||
|
||||
let res = self.client.get(url).send().await?.json::<Value>().await?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod v1;
|
||||
@@ -0,0 +1,12 @@
|
||||
pub mod models;
|
||||
|
||||
use crate::auth::middleware::auth_middleware;
|
||||
use crate::routes::v1::models::list_models;
|
||||
use crate::state::app_state::AppState;
|
||||
use axum::{Router, middleware, routing::get};
|
||||
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/models", get(list_models))
|
||||
.layer(middleware::from_fn(auth_middleware))
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use axum::{Json, extract::State};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::state::app_state::AppState;
|
||||
|
||||
pub async fn list_models(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<Value>, (axum::http::StatusCode, String)> {
|
||||
match state.ollama.list_models().await {
|
||||
Ok(models) => Ok(Json(models)),
|
||||
Err(err) => Err((
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
err.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
use crate::providers::ollama::OllamaProvider;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub ollama: Arc<OllamaProvider>,
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod app_state;
|
||||
Reference in New Issue
Block a user