This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Lister struct { | |
| pubsub *pubsub.Client | |
| ctx context.Context | |
| } | |
| func NewPubSub(projectID string) (*Lister, error) { | |
| ctx := context.Background() | |
| client, err := pubsub.NewClient(ctx, projectID) | |
| if err != nil { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (l *Lister) ListenSubscription(cm chan<- *pubsub.Message, subID string) error { | |
| defer l.pubsub.Close() | |
| sub := l.pubsub.Subscription(subID) | |
| // Receive blocks until the context is cancelled or an error occurs. | |
| err := sub.Receive(l.ctx, func(ctx context.Context, msg *pubsub.Message) { | |
| log.Debugf("[ListenSubscription] Receive message from pubsub:%q\n", string(msg.Data)) | |
| cm <- msg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a channel to handle messages to as they come in. | |
| cm := make(chan *pubsub.Message) | |
| defer close(cm) | |
| // Handle individual messages in a goroutine. | |
| go func(cm <-chan *pubsub.Message) { | |
| for msg := range cm { | |
| log.Debugf("[main] Got message :%q\n", string(msg.Data)) | |
| data, err := newProcessor.Run(1) | |
| if err != nil { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[allow(dead_code)] | |
| pub struct Application { | |
| port: u16, | |
| server: Server, | |
| } | |
| impl Application { | |
| pub async fn new() -> Result<Self, AppError> { | |
| dotenv().ok(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| impl AppState { | |
| pub async fn new() -> Result<Self, AppError> { | |
| let database = Database::new().await?; | |
| Ok(AppState { | |
| db: database.pool, | |
| }) | |
| } | |
| pub async fn get_srv(self) -> Result<Server, AppError> { | |
| let mut listenfd = ListenFd::from_env(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub fn routes_conf(config: &mut web::ServiceConfig) { | |
| // health check | |
| config.service(check); | |
| config.service(get_delays); | |
| config.service(search_by_date); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let q = format!( | |
| "select | |
| date, | |
| departure, | |
| depart_scheduled, | |
| destination, | |
| arrival_scheduled, | |
| delay, | |
| created_at, | |
| count(*) OVER() AS total_count |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "query": { | |
| "must": { | |
| "ranges": [ | |
| {"date": {"gte": "2021-10-01", "lte": "2021-11-01"}} | |
| ] | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub fn new(q: &str) -> Result<Self, AppError> { | |
| // get the input search string | |
| let raw_query: SearchRawQuery = serde_json::from_str(q)?; | |
| let mut query_keys_validation_list: Vec<&Option<Vec<Value>>> = vec![]; | |
| // check if have must keyword | |
| if let Some(must) = &raw_query.query.must { | |
| query_keys_validation_list.extend_from_slice(&[ | |
| &must.ranges, | |
| ]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub async fn to_sql_query(&self, namespace: &str) -> Result<SearchSQLQuery, AppError> { | |
| let mut sql_query: String; | |
| match namespace { | |
| SEARCH_NAMESPACE_DATE_RANGE => { | |
| sql_query = "select date, departure, depart_scheduled, destination, arrival_scheduled, delay, created_at, count(*) OVER() AS total_count from delays".to_string(); | |
| } | |
| _ => { | |
| return Err(AppError { | |
| message: None, |