elk-rs 0.1.0

A Public SMTP Server to forward Messages into A Backend System
// Copyright 2022 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

use std::error::Error;

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let addr = "127.0.0.1:8080";
    let listener = TcpListener::bind(addr).await?;

    println!("Listening on address: {}", addr);

    loop {
        let (mut socket, _) = listener.accept().await?;

        tokio::spawn(async move {
            let mut buf = vec![0; 1024];

            loop {
                let n = socket
                    .read(&mut buf)
                    .await
                    .expect("failed to read data from socket");

                if n == 0 {
                    return;
                }

                let command = &buf[0..n];
                let out;

                let _s = match std::str::from_utf8(command) {
                    Ok(v) => {
                        out = String::from(v);
                    }
                    Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
                };

                socket
                    .write_all(&out.as_str().as_bytes())
                    .await
                    .expect("failed to write data to socket");
            }
        });
    }
}