Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.
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
2 changes: 1 addition & 1 deletion server/bleep/src/webserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub async fn start(app: Application) -> anyhow::Result<()> {
.route("/hoverable", get(hoverable::handle))
.route("/token-info", get(intelligence::handle))
// misc
.route("/file/*ref", get(file::handle))
.route("/file", get(file::handle))
.route("/search", get(semantic::complex_search))
.route(
"/answer",
Expand Down
43 changes: 25 additions & 18 deletions server/bleep/src/webserver/file.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};

use axum::{
extract::{Path, Query},
Extension, Json,
};
use anyhow::Context;
use axum::{extract::Query, Extension, Json};

use crate::repo::RepoRef;

use super::prelude::*;

#[derive(Debug, serde::Deserialize, Default)]
#[derive(Debug, serde::Deserialize)]
pub(super) struct Params {
pub repo_ref: RepoRef,
pub path: PathBuf,

/// 1-indexed line number at which to start the snippet
pub line_start: Option<isize>,

Expand All @@ -19,33 +22,27 @@ pub(super) struct Params {
#[derive(serde::Serialize)]
pub(super) struct FileResponse {
contents: String,
lang: Option<String>,
}

impl super::ApiResponse for FileResponse {}

pub(super) async fn handle<'a>(
Path(path): Path<String>,
Query(params): Query<Params>,
Extension(indexes): Extension<Arc<Indexes>>,
) -> Result<Json<super::Response<'a>>, Error> {
// Strip leading slash, always present.
let Some((rr, file_path)) = path.split_once(':') else {
return Err(Error::user("invalid path, use repo_ref:path"));
};

let Ok(repo_ref) = rr.parse() else {
println!("{rr}");
return Err(Error::user("invalid repo_ref"));
};

let doc = indexes
.file
.by_path(&repo_ref, file_path)
.by_path(
&params.repo_ref,
params.path.to_str().context("invalid file path")?,
)
.await
.map_err(Error::internal)?;

Ok(json(FileResponse {
contents: split_by_lines(&doc.content, &doc.line_end_indices, &params)?.to_string(),
lang: doc.lang,
}))
}

Expand Down Expand Up @@ -93,6 +90,8 @@ cccccc
text,
&indices,
&Params {
repo_ref: "local//repo".into(),
path: "file".into(),
line_start: None,
line_end: None
}
Expand All @@ -106,6 +105,8 @@ cccccc
text,
&indices,
&Params {
repo_ref: "local//repo".into(),
path: "file".into(),
line_start: Some(1),
line_end: None
}
Expand All @@ -119,6 +120,8 @@ cccccc
text,
&indices,
&Params {
repo_ref: "local//repo".into(),
path: "file".into(),
line_start: Some(2),
line_end: None
}
Expand All @@ -132,6 +135,8 @@ cccccc
text,
&indices,
&Params {
repo_ref: "local//repo".into(),
path: "file".into(),
line_start: Some(3),
line_end: Some(3),
}
Expand All @@ -145,6 +150,8 @@ cccccc
text,
&indices,
&Params {
repo_ref: "local//repo".into(),
path: "file".into(),
line_start: Some(2),
line_end: Some(3),
}
Expand Down