JavaScript is disabled, refresh for a better experience. ambee/giterated-api

ambee/giterated-api

Git repository hosting, collaboration, and discovery for the Fediverse.

Add connection pool

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨98d3b04

⁨src/main.rs⁩ - ⁨2913⁩ bytes
Raw
1 use std::str::FromStr;
2
3 use giterated_api::{DaemonConnectionPool, GiteratedApiBuilder};
4 use giterated_daemon::model::{
5 instance::Instance,
6 repository::{Repository, RepositoryVisibility},
7 user::User,
8 };
9 use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation};
10 use serde::{Deserialize, Serialize};
11 // use jwt::SignWithKey;
12
13 #[macro_use]
14 extern crate tracing;
15
16 #[tokio::main]
17 async fn main() -> Result<(), anyhow::Error> {
18 tracing_subscriber::fmt::init();
19
20 let pool = DaemonConnectionPool::connect(Instance::from_str("giterated.dev")?).unwrap();
21
22 let mut api = GiteratedApiBuilder::from_local("giterated.dev")
23 .unwrap()
24 .private_key(include_str!("example_keys/giterated.key"))
25 .public_key(include_str!("example_keys/giterated.key.pub"))
26 .build()
27 .await
28 .unwrap();
29
30 info!("Lets try to make an account!");
31
32 let response = api
33 .register(
34 String::from("ambee"),
35 None,
36 String::from("lolthisisinthecommithistory"),
37 &pool,
38 )
39 .await;
40
41 info!("Registration response: {:?}", response);
42
43 let token = api
44 .authentication_token(
45 String::from("foobar"),
46 String::from("ambee"),
47 String::from("password"),
48 &pool,
49 )
50 .await
51 .unwrap();
52
53 println!("Token: {}", token);
54
55 let public_key = api.public_key().await;
56
57 println!("Server public key:\n{}", public_key);
58 let verification_key = DecodingKey::from_rsa_pem(public_key.as_bytes()).unwrap();
59 let data: TokenData<UserTokenMetadata> = decode(
60 &token,
61 &verification_key,
62 &Validation::new(Algorithm::RS256),
63 )
64 .unwrap();
65
66 println!("The token was valid! Data:\n{:#?}", data.claims);
67
68 info!("Lets extend that token!");
69
70 let new_token = api
71 .extend_token(String::from("foobar"), token.clone(), &pool)
72 .await
73 .unwrap();
74 info!("New Token Returned:\n{:?}", new_token);
75
76 info!("Try to create a repository? uwu");
77
78 let repository = api
79 .create_repository(
80 new_token.unwrap(),
81 String::from("super-repository"),
82 None,
83 RepositoryVisibility::Public,
84 String::from("master"),
85 User::from_str("ambee:giterated.dev").unwrap(),
86 &pool,
87 )
88 .await
89 .unwrap();
90
91 assert!(repository);
92
93 info!("Lets view our repository!");
94
95 let view = api
96 .repository_info(
97 &token,
98 Repository::from_str("ambee:giterated.dev/[email protected]").unwrap(),
99 &pool,
100 )
101 .await
102 .unwrap();
103
104 info!("Repository Info:\n{:#?}", view);
105
106 Ok(())
107 }
108
109 #[derive(Debug, Serialize, Deserialize)]
110 struct UserTokenMetadata {
111 user: User,
112 generated_for: Instance,
113 exp: u64,
114 }
115