Skip to content

Commit

Permalink
Add AppState to store OpenSearch client
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed Apr 21, 2024
1 parent 3cced06 commit f45be4b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition = "2021"

[dependencies]
axum = "0.7"
opensearch = "2.2"
tokio = { version = "1.37", features = ["full"] }
17 changes: 15 additions & 2 deletions backend-rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
use std::sync::Arc;

use axum::{
extract::State,
routing::{get, post},
Router,
};
use opensearch::OpenSearch;

struct AppState {
opensearch: OpenSearch,
}

#[tokio::main]
async fn main() {
// build our application with a single route
let state = Arc::new(AppState {
opensearch: OpenSearch::default(),
});
let api = Router::new()
.route("/flake", get(get_flake))
.route("/publish", post(post_publish));
.route("/publish", post(post_publish))
.with_state(state);
let app = Router::new().nest("/api", api);

// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}

async fn get_flake() -> &'static str {
async fn get_flake(State(state): State<Arc<AppState>>) -> &'static str {
let opensearch = &state.opensearch;
"Flake"
}

Expand Down
4 changes: 4 additions & 0 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ in
pkgs.cloudflared
pkgs.openapi-generator-cli
pkgs.nodePackages.pyright
] ++ lib.optionals pkgs.stdenv.isDarwin [
pkgs.darwin.CF
pkgs.darwin.Security
pkgs.darwin.configd
];

# https://github.com/cachix/devenv/pull/745
Expand Down

0 comments on commit f45be4b

Please sign in to comment.