This file contains 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
#[tokio::main] | |
async fn main() -> Result<(), AppError> { | |
// load env | |
dotenv().ok(); | |
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber` | |
// While `tracing` is used internally, `log` can be used as well if preferred. | |
tracing_subscriber::fmt() | |
.with_max_level(tracing::Level::INFO) |
This file contains 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 enum AppErrorType { | |
SerdeJsonError, | |
ReqwestError, | |
AwsSdkError, | |
DbError | |
} | |
#[derive(Debug, PartialEq)] | |
pub struct AppError { | |
pub message: Option<String>, |
This file contains 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 Processor { | |
pub async fn run(&mut self) -> Result<usize, AppError> { | |
let html_data = self.get_data_from_url().await?; | |
self.process_data(html_data).await?; | |
let count = self.insert().await?; | |
Ok(count) | |
} | |
async fn get_data_from_url(&mut self) -> Result<HtmlData, AppError> { | |
// http request to get the page |
This file contains 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
#[async_trait(?Send)] | |
pub trait DataStore { | |
async fn insert(&self, data: &TradingView) -> Result<(), AppError>; | |
} | |
#[derive(Debug)] | |
pub struct AppDB { | |
// any type inside a Box must implement the Producer trait. | |
pub db: Box<dyn DataStore>, | |
} |
This file contains 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
use async_trait::async_trait; | |
use core::fmt::Debug; | |
use crate::error::AppError; | |
pub mod slack; | |
#[async_trait(?Send)] | |
pub trait Producer { | |
async fn run(&self, msg: &str) -> Result<(), AppError>; | |
} |
This file contains 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 struct Proccessor { | |
pub url: String, | |
pub data: Option<Vec<Value>>, | |
pub alert: AppAlert, | |
pub db: AppDB, | |
} |
This file contains 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
export default function TwitterCard({ tweet }) { | |
return ( | |
<div className="mx-auto rounded-lg bg-white shadow p-5 text-gray-800"> | |
{tweet ? ( | |
<> | |
{tweet && | |
tweet.map((msg) => ( | |
<div key={msg.id}> | |
<div className="flex-grow pl-3"> | |
<h6 className="font-bold text-md">{msg.user.screen_name}</h6> |
This file contains 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
import { useState } from 'react'; | |
import { w3cwebsocket as W3CWebSocket } from "websocket"; | |
import TopicForm from '../components/topicForm'; | |
import TwitterCard from '../components/twitterCard'; | |
export default function Home() { | |
const [wss, setWss] = useState(); | |
const [wsSession, setWssSession] = useState(); | |
const [twitterMsg, setTwitterMsg] = useState([]); |
This file contains 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
// NextJS API - Registeration | |
// folder - pages/api/register.js | |
const RegisterHandler = async (req, res) => { | |
try { | |
let request_url = "http://localhost:8000/register"; | |
// This is a contrived example, normally your external API would exist on another domain. | |
const response = await fetch(request_url, { | |
method: "POST", |
This file contains 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 twitter_stream( | |
topic: String, | |
sender: mpsc::UnboundedSender<std::result::Result<Message, warp::Error>>, | |
) { | |
let consumer_key = env::var("consumer_key").expect("load consumer key failed failed"); | |
let consumer_secret = env::var("consumer_secret").expect("load consumer_secret failed failed"); | |
let access_token = env::var("access_token").expect("load access_key failed failed"); | |
let access_token_secret = | |
env::var("access_token_secret").expect("load access_token_secret failed failed"); |