Update version checks
parent: tbd commit: 1ed7c39
Showing 8 changed files with 131 insertions and 12 deletions
.sqlx/query-4ee84adf4a01a20c49eb53336525708cde3d375c26458caed10dc17b9f899cd6.json
@@ -0,0 +1,27 @@ | ||
1 | { | |
2 | "db_name": "PostgreSQL", | |
3 | "query": "INSERT INTO discoveries VALUES ($1, $2, $3, $4)", | |
4 | "describe": { | |
5 | "columns": [], | |
6 | "parameters": { | |
7 | "Left": [ | |
8 | "Text", | |
9 | "Text", | |
10 | { | |
11 | "Custom": { | |
12 | "name": "discovery_type", | |
13 | "kind": { | |
14 | "Enum": [ | |
15 | "instance", | |
16 | "repository" | |
17 | ] | |
18 | } | |
19 | } | |
20 | }, | |
21 | "Text" | |
22 | ] | |
23 | }, | |
24 | "nullable": [] | |
25 | }, | |
26 | "hash": "4ee84adf4a01a20c49eb53336525708cde3d375c26458caed10dc17b9f899cd6" | |
27 | } |
Cargo.lock
@@ -629,7 +629,7 @@ dependencies = [ | ||
629 | 629 | |
630 | 630 | [[package]] |
631 | 631 | name = "giterated-daemon" |
632 | version = "0.0.2" | |
632 | version = "0.0.3" | |
633 | 633 | dependencies = [ |
634 | 634 | "aes-gcm", |
635 | 635 | "anyhow", |
Cargo.toml
@@ -1,6 +1,6 @@ | ||
1 | 1 | [package] |
2 | 2 | name = "giterated-daemon" |
3 | version = "0.0.2" | |
3 | version = "0.0.3" | |
4 | 4 | edition = "2021" |
5 | 5 | |
6 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
src/backend/discovery.rs
@@ -1,18 +1,108 @@ | ||
1 | use std::{ | |
2 | collections::hash_map::DefaultHasher, | |
3 | hash::{Hash, Hasher}, | |
4 | }; | |
5 | ||
1 | 6 | use anyhow::Error; |
7 | use base64::{engine::general_purpose::STANDARD, Engine as _}; | |
8 | use chrono::{DateTime, Utc}; | |
9 | use serde::{Deserialize, Serialize}; | |
10 | use sqlx::PgPool; | |
2 | 11 | |
3 | use crate::{messages::discovery::DiscoveryMessage, model::repository::Repository}; | |
12 | use crate::{ | |
13 | messages::discovery::{DiscoveryMessage, DiscoveryMessageKind}, | |
14 | model::{discovery::DiscoveryItem, repository::Repository}, | |
15 | }; | |
4 | 16 | |
5 | 17 | use super::DiscoveryBackend; |
6 | 18 | |
7 | pub struct GiteratedDiscoveryProtocol {} | |
19 | pub struct GiteratedDiscoveryProtocol { | |
20 | pub pool: PgPool, | |
21 | } | |
22 | ||
23 | #[derive(Debug, Hash, Serialize, Deserialize, Clone, sqlx::Type)] | |
24 | #[sqlx(type_name = "discovery_type", rename_all = "lowercase")] | |
25 | pub enum DiscoveryType { | |
26 | Instance, | |
27 | Repository, | |
28 | } | |
8 | 29 | |
9 | 30 | #[async_trait::async_trait] |
10 | 31 | impl DiscoveryBackend for GiteratedDiscoveryProtocol { |
11 | async fn try_handle(&mut self, _request: &DiscoveryMessage) -> Result<bool, Error> { | |
12 | todo!() | |
32 | async fn try_handle(&mut self, request: &DiscoveryMessage) -> Result<bool, Error> { | |
33 | if request | |
34 | .message | |
35 | .validate(&request.message.instance) | |
36 | .await | |
37 | .is_err() | |
38 | { | |
39 | return Ok(false); | |
40 | } | |
41 | ||
42 | let inner = request.message.inner().await.clone(); | |
43 | ||
44 | match inner { | |
45 | DiscoveryMessageKind::Discoveries(mut discoveries) => { | |
46 | let discoveries = discoveries.discoveries.drain(..).map(|discovery| { | |
47 | let hash = { | |
48 | let mut hasher = DefaultHasher::new(); | |
49 | discovery.hash(&mut hasher); | |
50 | STANDARD.encode(hasher.finish().to_be_bytes()) | |
51 | }; | |
52 | ||
53 | let (discovery_type, discovery) = match discovery { | |
54 | DiscoveryItem::Instance { | |
55 | instance, | |
56 | signature, | |
57 | } => (DiscoveryType::Instance, instance.to_string()), | |
58 | DiscoveryItem::Repository { | |
59 | repository, | |
60 | signature, | |
61 | } => (DiscoveryType::Repository, repository.to_string()), | |
62 | }; | |
63 | ||
64 | DiscoveriesRow { | |
65 | discovery_hash: hash, | |
66 | discovery_time: Utc::now(), | |
67 | discovery_type, | |
68 | discovery, | |
69 | } | |
70 | }); | |
71 | ||
72 | for row in discoveries { | |
73 | let result = sqlx::query!( | |
74 | r#"INSERT INTO discoveries VALUES ($1, $2, $3, $4)"#, | |
75 | row.discovery_hash, | |
76 | row.discovery_time.to_string(), | |
77 | row.discovery_type as _, | |
78 | row.discovery | |
79 | ) | |
80 | .execute(&self.pool) | |
81 | .await; | |
82 | ||
83 | match result { | |
84 | Ok(_) => {}, | |
85 | Err(err) => { | |
86 | error!("Error inserting discovery. {:?}", err); | |
87 | } | |
88 | } | |
89 | } | |
90 | Ok(true) | |
91 | } | |
92 | DiscoveryMessageKind::Offer(offer) => Ok(true), | |
93 | DiscoveryMessageKind::Request(request) => Ok(true), | |
94 | } | |
13 | 95 | } |
14 | 96 | |
15 | 97 | async fn search(&mut self, _search: &str) -> Result<Vec<Repository>, Error> { |
16 | 98 | todo!() |
17 | 99 | } |
18 | 100 | } |
101 | ||
102 | #[derive(Debug, sqlx::FromRow, sqlx::Type)] | |
103 | pub struct DiscoveriesRow { | |
104 | discovery_hash: String, | |
105 | discovery_time: DateTime<Utc>, | |
106 | discovery_type: DiscoveryType, | |
107 | discovery: String, | |
108 | } | |
108 | \ No newline at end of file |
src/connection.rs
@@ -2,7 +2,7 @@ use std::{collections::HashMap, net::SocketAddr, str::FromStr, sync::Arc}; | ||
2 | 2 | |
3 | 3 | use anyhow::Error; |
4 | 4 | use futures_util::{stream::StreamExt, SinkExt}; |
5 | use semver::{Version}; | |
5 | use semver::Version; | |
6 | 6 | use serde::Serialize; |
7 | 7 | use tokio::{ |
8 | 8 | net::TcpStream, |
@@ -16,7 +16,7 @@ use tokio_tungstenite::{tungstenite::Message, WebSocketStream}; | ||
16 | 16 | |
17 | 17 | use crate::{ |
18 | 18 | authentication::AuthenticationTokenGranter, |
19 | backend::{DiscoveryBackend, IssuesBackend, RepositoryBackend, UserBackend}, | |
19 | backend::{DiscoveryBackend, RepositoryBackend, UserBackend}, | |
20 | 20 | handshake::{HandshakeFinalize, HandshakeMessage, HandshakeResponse}, |
21 | 21 | listener::Listeners, |
22 | 22 | messages::{ |
src/lib.rs
@@ -18,7 +18,7 @@ pub fn version() -> Version { | ||
18 | 18 | } |
19 | 19 | |
20 | 20 | pub fn validate_version(other: &Version) -> bool { |
21 | let version_req = VersionReq::from_str("=0.0.1").unwrap(); | |
21 | let version_req = VersionReq::from_str("=0.0.3").unwrap(); | |
22 | 22 | |
23 | 23 | version_req.matches(other) |
24 | 24 | } |
src/main.rs
@@ -64,13 +64,15 @@ async fn main() -> Result<(), Error> { | ||
64 | 64 | })); |
65 | 65 | |
66 | 66 | let user_backend: Arc<Mutex<dyn UserBackend + Send>> = Arc::new(Mutex::new(UserAuth::new( |
67 | db_pool, | |
67 | db_pool.clone(), | |
68 | 68 | &Instance::from_str("giterated.dev").unwrap(), |
69 | 69 | token_granter.clone(), |
70 | 70 | ))); |
71 | 71 | |
72 | 72 | let discovery_backend: Arc<Mutex<dyn DiscoveryBackend + Send>> = |
73 | Arc::new(Mutex::new(GiteratedDiscoveryProtocol {})); | |
73 | Arc::new(Mutex::new(GiteratedDiscoveryProtocol { | |
74 | pool: db_pool.clone(), | |
75 | })); | |
74 | 76 | |
75 | 77 | info!("Connected"); |
76 | 78 |