Skip to content

Commit

Permalink
Merge pull request #34 from mpiorowski/feature/users-tokio-postgres
Browse files Browse the repository at this point in the history
added-tokio-postgres-for-users-service
  • Loading branch information
mpiorowski authored May 20, 2023
2 parents 460b551 + 0245fbe commit c058a52
Show file tree
Hide file tree
Showing 18 changed files with 697 additions and 222 deletions.
2 changes: 1 addition & 1 deletion client/src/lib/proto/main.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ service UsersService {
rpc Auth(AuthRequest) returns (User) {}
rpc GetUsers(UserIds) returns (stream User) {}
rpc GetUser(UserId) returns (User) {}
rpc CreateUser(User) returns (User) {}
rpc UpdateUser(User) returns (User) {}
rpc UpdatePaymentId(PaymentId) returns (Empty) {}
}

Expand Down
24 changes: 12 additions & 12 deletions client/src/lib/proto/proto/UsersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ export interface UsersServiceClient extends grpc.Client {
auth(argument: _proto_AuthRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
auth(argument: _proto_AuthRequest, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;

CreateUser(argument: _proto_User, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
CreateUser(argument: _proto_User, metadata: grpc.Metadata, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
CreateUser(argument: _proto_User, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
CreateUser(argument: _proto_User, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
createUser(argument: _proto_User, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
createUser(argument: _proto_User, metadata: grpc.Metadata, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
createUser(argument: _proto_User, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
createUser(argument: _proto_User, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;

GetUser(argument: _proto_UserId, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
GetUser(argument: _proto_UserId, metadata: grpc.Metadata, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
GetUser(argument: _proto_UserId, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
Expand All @@ -51,25 +42,34 @@ export interface UsersServiceClient extends grpc.Client {
updatePaymentId(argument: _proto_PaymentId, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_Empty__Output>): grpc.ClientUnaryCall;
updatePaymentId(argument: _proto_PaymentId, callback: grpc.requestCallback<_proto_Empty__Output>): grpc.ClientUnaryCall;

UpdateUser(argument: _proto_User, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
UpdateUser(argument: _proto_User, metadata: grpc.Metadata, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
UpdateUser(argument: _proto_User, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
UpdateUser(argument: _proto_User, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
updateUser(argument: _proto_User, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
updateUser(argument: _proto_User, metadata: grpc.Metadata, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
updateUser(argument: _proto_User, options: grpc.CallOptions, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;
updateUser(argument: _proto_User, callback: grpc.requestCallback<_proto_User__Output>): grpc.ClientUnaryCall;

}

export interface UsersServiceHandlers extends grpc.UntypedServiceImplementation {
Auth: grpc.handleUnaryCall<_proto_AuthRequest__Output, _proto_User>;

CreateUser: grpc.handleUnaryCall<_proto_User__Output, _proto_User>;

GetUser: grpc.handleUnaryCall<_proto_UserId__Output, _proto_User>;

GetUsers: grpc.handleServerStreamingCall<_proto_UserIds__Output, _proto_User>;

UpdatePaymentId: grpc.handleUnaryCall<_proto_PaymentId__Output, _proto_Empty>;

UpdateUser: grpc.handleUnaryCall<_proto_User__Output, _proto_User>;

}

export interface UsersServiceDefinition extends grpc.ServiceDefinition {
Auth: MethodDefinition<_proto_AuthRequest, _proto_User, _proto_AuthRequest__Output, _proto_User__Output>
CreateUser: MethodDefinition<_proto_User, _proto_User, _proto_User__Output, _proto_User__Output>
GetUser: MethodDefinition<_proto_UserId, _proto_User, _proto_UserId__Output, _proto_User__Output>
GetUsers: MethodDefinition<_proto_UserIds, _proto_User, _proto_UserIds__Output, _proto_User__Output>
UpdatePaymentId: MethodDefinition<_proto_PaymentId, _proto_Empty, _proto_PaymentId__Output, _proto_Empty__Output>
UpdateUser: MethodDefinition<_proto_User, _proto_User, _proto_User__Output, _proto_User__Output>
}
6 changes: 3 additions & 3 deletions client/src/routes/(app)/profile/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const actions = {
console.log(data);
const metadata = await createMetadata(uri);
const user = await new Promise<User__Output>((resolve, reject) => {
client.createUser(data, metadata, (err, response) =>
client.updateUser(data, metadata, (err, response) =>
err || !response ? reject(err) : resolve(response),
);
});
Expand Down Expand Up @@ -219,7 +219,7 @@ export const actions = {
};
metadata = await createMetadata(uri);
const user = await new Promise<User__Output>((resolve, reject) => {
client.createUser(data, metadata, (err, response) =>
client.updateUser(data, metadata, (err, response) =>
err || !response ? reject(err) : resolve(response),
);
});
Expand Down Expand Up @@ -287,7 +287,7 @@ export const actions = {
name: schema.data.name,
};
await new Promise<User__Output>((resolve, reject) => {
client.createUser(data, metadata, (err, response) =>
client.updateUser(data, metadata, (err, response) =>
err || !response ? reject(err) : resolve(response),
);
});
Expand Down
2 changes: 1 addition & 1 deletion proto/main.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ service UsersService {
rpc Auth(AuthRequest) returns (User) {}
rpc GetUsers(UserIds) returns (stream User) {}
rpc GetUser(UserId) returns (User) {}
rpc CreateUser(User) returns (User) {}
rpc UpdateUser(User) returns (User) {}
rpc UpdatePaymentId(PaymentId) returns (Empty) {}
}

Expand Down
6 changes: 3 additions & 3 deletions service-notes-go/proto/main.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions service-notes-go/proto/main_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 14 additions & 7 deletions service-notes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod proto;
use std::str::FromStr;

use anyhow::{Context, Result};
use deadpool_postgres::{Manager, ManagerConfig, Pool};
use deadpool_postgres::{Pool};
use proto::notes_service_server::NotesServiceServer;
use sqlx::postgres::PgPoolOptions;
use tonic::transport::Server;
Expand All @@ -21,28 +21,35 @@ async fn main() -> Result<()> {
let database_url = std::env::var("DATABASE_URL").context("DATABASE_URL not set")?;
let port = std::env::var("PORT").context("PORT not set")?;

// Database
// Migrations
let pool = PgPoolOptions::new()
.max_connections(20)
.max_connections(1)
.connect(&database_url)
.await
.with_context(|| format!("Failed to connect to database: {}", database_url))?;
// Migrations
.context("Failed to connect to database")?;
sqlx::migrate!("./migrations")
.run(&pool)
.await
.context("Failed to run migrations")?;
println!("Migrations ran successfully");
pool.close().await;

// Database connection pool
let pg_config = tokio_postgres::Config::from_str(&database_url)?;
let mgr = Manager::from_config(pg_config, tokio_postgres::NoTls, ManagerConfig::default());
let pool = Pool::builder(mgr)
let manager = deadpool_postgres::Manager::from_config(
pg_config,
tokio_postgres::NoTls,
deadpool_postgres::ManagerConfig {
recycling_method: deadpool_postgres::RecyclingMethod::Fast,
},
);
let pool = deadpool_postgres::Pool::builder(manager)
.max_size(20)
.build()
.context("Failed to create database pool")?;
println!("Connected to database");


let addr = ("[::]:".to_owned() + &port).parse()?;
println!("Server started on port: {}", port);

Expand Down
6 changes: 3 additions & 3 deletions service-users-go/proto/main.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions service-users-go/proto/main_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c058a52

Please sign in to comment.