Skip to content

Commit

Permalink
Change how host and ports are set
Browse files Browse the repository at this point in the history
  • Loading branch information
micc83 committed Sep 11, 2020
1 parent 845762f commit fb521ff
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 50 deletions.
4 changes: 3 additions & 1 deletion assets/components/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export default {
Viewer,
Waiter
},
props: ['websocket'],
async created() {
new WebSocket("ws://localhost:1338").onmessage = event => {
new WebSocket(`ws://${this.websocket}`).onmessage = event => {
let message = JSON.parse(event.data);
this.messages.unshift(message)
if (!this.message) {
Expand Down Expand Up @@ -88,6 +89,7 @@ export default {
}
.sidebar {
width: 100%;
max-width: 350px;
color: var(--sidebar-text-color);
background-color: var(--sidebar-background-color);
Expand Down
11 changes: 7 additions & 4 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

return [
'smtp' => [
'host' => '127.0.0.1:8025',
'host' => '127.0.0.1',
'port' => '8025'
],

'web' => [
'host' => '127.0.0.1:8080',
'http' => [
'host' => '127.0.0.1',
'port' => '8080'
],

'websocket' => [
'host' => '127.0.0.1:1338',
'host' => '127.0.0.1',
'port' => '1338'
],
];
19 changes: 6 additions & 13 deletions public/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,12 @@
--viewer-background-color: aliceblue;
--viewer-text-color: #212529;

/** Extra */
--waiting-color: #b7caec;


--ultralight-color: aliceblue;
--main-color: #4b7ed6;
--border-color: #dee2e6;
--background-color: #f8f9fa;
--muted-color: #6c757d;
--white: #ffffff;
--black: #000000;

--bs-font-sans-serif: system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";
--bs-font-monospace: SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;
/** Fonts */
--font-sans-serif: system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";
--font-monospace: SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;
}

html, body {
Expand All @@ -49,7 +42,7 @@ html, body {

body {
margin: 0;
font-family: var(--bs-font-sans-serif);
font-family: var(--font-sans-serif);
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
Expand All @@ -58,7 +51,7 @@ body {
}

pre {
font-family: var(--bs-font-monospace);
font-family: var(--font-monospace);
}

svg {
Expand Down
5 changes: 3 additions & 2 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,7 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar
Viewer: _Viewer__WEBPACK_IMPORTED_MODULE_5__["default"],
Waiter: _Waiter__WEBPACK_IMPORTED_MODULE_6__["default"]
},
props: ['websocket'],
created: function created() {
var _this = this;

Expand All @@ -2772,7 +2773,7 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar
while (1) {
switch (_context.prev = _context.next) {
case 0:
new WebSocket("ws://localhost:1338").onmessage = function (event) {
new WebSocket("ws://".concat(_this.websocket)).onmessage = function (event) {
var message = JSON.parse(event.data);

_this.messages.unshift(message);
Expand Down Expand Up @@ -3092,7 +3093,7 @@ exports = module.exports = __webpack_require__(/*! ../../node_modules/css-loader


// module
exports.push([module.i, "\n.app {\n display: flex;\n}\n.sidebar {\n max-width: 350px;\n color: var(--sidebar-text-color);\n background-color: var(--sidebar-background-color);\n border-right: 1px solid var(--sidebar-border-color);\n overflow: auto;\n}\n.viewer-container, .sidebar {\n height: 100vh !important;\n}\n.viewer-container {\n display: flex;\n width: 100%;\n flex-direction: column;\n align-content: stretch;\n overflow:hidden;\n}\n.navbar {\n padding-right: .5rem;\n padding-left: .5rem;\n justify-content: space-between;\n display: flex;\n background-color: var(--header-background-color);\n flex-wrap: nowrap;\n color: var(--header-text-color);\n}\n", ""]);
exports.push([module.i, "\n.app {\n display: flex;\n}\n.sidebar {\n width: 100%;\n max-width: 350px;\n color: var(--sidebar-text-color);\n background-color: var(--sidebar-background-color);\n border-right: 1px solid var(--sidebar-border-color);\n overflow: auto;\n}\n.viewer-container, .sidebar {\n height: 100vh !important;\n}\n.viewer-container {\n display: flex;\n width: 100%;\n flex-direction: column;\n align-content: stretch;\n overflow:hidden;\n}\n.navbar {\n padding-right: .5rem;\n padding-left: .5rem;\n justify-content: space-between;\n display: flex;\n background-color: var(--header-background-color);\n flex-wrap: nowrap;\n color: var(--header-text-color);\n}\n", ""]);

// exports

Expand Down
2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<body>

<div id="app">
<app></app>
<app websocket="<?= $this->websocketHost ?>"></app>
</div>

<script src="app.js?v=<?= $this->version ?>"></script>
Expand Down
6 changes: 3 additions & 3 deletions src/Emails/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Message
private string $textBody;
private string $subject;
private DateTimeImmutable $created_at;
private string $replyTo;
private ?string $replyTo;
/** @var string[] */
private array $allRecipients;
/** @var Attachment[] */
Expand All @@ -38,7 +38,7 @@ class Message
* @param string $subject
* @param string $htmlBody
* @param string $textBody
* @param string $replyTo
* @param string|null $replyTo
* @param string[] $allRecipients
* @param Attachment[] $attachments
*/
Expand All @@ -50,7 +50,7 @@ public function __construct(
string $subject,
string $htmlBody,
string $textBody,
string $replyTo,
?string $replyTo,
array $allRecipients,
array $attachments
) {
Expand Down
9 changes: 6 additions & 3 deletions src/Emails/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ public function parse(string $rawContent, array $allRecipients = []): Message
$message = ParseMessage::from($rawContent);

$from = $message->getHeader('from')->getRawValue();
$recipients = $this->joinNameAndEmail($message->getHeader('to')->getAddresses());
$ccs = $this->joinNameAndEmail($message->getHeader('cc')->getAddresses());
$toHeader = $message->getHeader('to');
$recipients = $this->joinNameAndEmail($toHeader ? $toHeader->getAddresses() : []);
$ccHeader = $message->getHeader('cc');
$ccs = $this->joinNameAndEmail($ccHeader ? $ccHeader->getAddresses() : []);
$subject = $message->getHeaderValue('subject');
$html = $message->getHtmlContent();
$text = $message->getTextContent();
$replyTo = $message->getHeader('reply-to')->getRawValue();
$replyToHeader = $message->getHeader('reply-to');
$replyTo = $replyToHeader ? $replyToHeader->getRawValue() : null;
$attachments = $this->buildAttachmentFrom(
$message->getAllAttachmentParts()
);
Expand Down
4 changes: 2 additions & 2 deletions src/Events/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class Response implements Event
public string $body;
public string $code;

public function __construct(int $code, string $body)
public function __construct(int $code, string $body, string $comment = '')
{
$this->code = (string) $code;
$this->code = "{$code} {$comment}";
$this->body = $body;
}
}
6 changes: 4 additions & 2 deletions src/WebController.php → src/HttpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
use React\Http\Message\Response;
use Throwable;

class WebController
class HttpController
{
private Store $store;
private string $version;
private string $websocketHost;

public function __construct(Store $store)
public function __construct(Store $store, string $websocketHost)
{
$this->store = $store;
$this->version = Config::VERSION;
$this->websocketHost = $websocketHost;
}

public function route(ServerRequestInterface $request): Response
Expand Down
17 changes: 11 additions & 6 deletions src/WebServer.php → src/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@
use React\Http\Server;
use React\Socket\Server as SocketServer;

class WebServer
class HttpServer
{
private LoopInterface $loop;
private Emails\Store $messageStore;
private string $host;
private string $websocketHost;

public function __construct(string $host, LoopInterface $loop, Store $messageStore)
public function __construct(string $host, string $websocketHost, LoopInterface $loop, Store $messageStore)
{
$this->loop = $loop;
$this->messageStore = $messageStore;
$this->host = $host;
$this->websocketHost = $websocketHost;
}

public function start(): void
{
$server = new Server($this->loop, function (ServerRequestInterface $request) {
return (new WebController($this->messageStore))
->route($request);
});
$server = new Server(
$this->loop,
fn (ServerRequestInterface $request) => (
new HttpController($this->messageStore, $this->websocketHost)
)
->route($request)
);

$socket = new SocketServer($this->host, $this->loop);
$server->listen($socket);
Expand Down
6 changes: 3 additions & 3 deletions src/SmtpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(
public function ready(): void
{
$this->collectingData = false;
$this->send(static::READY);
$this->send(static::READY, 'mailamie');
}

public function handle(string $data): void
Expand Down Expand Up @@ -99,11 +99,11 @@ private function addToMessageBody(string $content): void
$this->messageBody .= $content;
}

private function send(int $statusCode): void
private function send(int $statusCode, string $comment = ''): void
{
$this->events->dispatch(
new Response($statusCode, static::$statusDescriptions[$statusCode])
);
$this->connection->write("{$statusCode}\r\n");
$this->connection->write("{$statusCode} {$comment}\r\n");
}
}
45 changes: 35 additions & 10 deletions src/StartServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ protected function configure(): void
"check the project readme file at https://github.com/micc83/mailamie\n" .
"for all the available settings."
);
$this->addUsage('--host=127.0.0.1:25 Ex. SMTP Host definition');
$this->addUsage('--host=127.0.0.1 --port=25 Ex. SMTP Host definition');
$this->setDefinition(
new InputDefinition([
new InputOption(
'host',
'H',
InputOption::VALUE_REQUIRED,
'Set the host on which to listen for SMTP calls'
'Set the host on which to listen for calls'
),
new InputOption(
'port',
'p',
InputOption::VALUE_REQUIRED,
'Set the port on which to listen for SMTP calls'
)
])
);
Expand All @@ -76,18 +82,19 @@ protected function execute(InputInterface $input, OutputInterface $output)

$loop = Factory::create();

$smtpServer = new SmtpServer($this->getHost(), $loop, $dispatcher);
$smtpServer = new SmtpServer($this->getSmtpHost(), $loop, $dispatcher);

$messageStore = new MessageStore();

$webServer = new WebServer(
$this->config->get('web.host'),
$webServer = new HttpServer(
$this->getHttpHost(),
$this->getWebsocketHost(),
$loop,
$messageStore
);

$websocketServer = new WebSocketServer(
$this->config->get('websocket.host'),
$this->getWebsocketHost(),
$loop,
$messageStore
);
Expand All @@ -111,7 +118,7 @@ private function registerEventListenersOn(EventDispatcher $dispatcher, MessageSt
$this->writeInfoBlockOn(
$startingSection,
'✓ SERVER UP AND RUNNING',
"SMTP listening on {$event->host}\n Web interface at http://{$this->config->get('web.host')}"
"SMTP listening on {$event->host}\n Web interface at http://{$this->getHttpHost()}"
);
});

Expand Down Expand Up @@ -144,10 +151,28 @@ private function handleMessage(Message $message, MessageStore $messageStore): vo
$this->writeTable($email->toTable());
}

private function getHost(): string
private function getSmtpHost(): string
{
$host = $this->getInput()->getOption('host') ?:$this->config->get('smtp.host');
$port = $this->getInput()->getOption('port') ?:$this->config->get('smtp.port');

return "{$host}:{$port}";
}

private function getHttpHost(): string
{
return (string)($this->getInput()->getOption('host')
?: $this->config->get('smtp.host'));
$host = $this->getInput()->getOption('host') ?:$this->config->get('http.host');
$port = $this->config->get('http.port');

return "{$host}:{$port}";
}

private function getWebsocketHost(): string
{
$host = $this->getInput()->getOption('host') ?:$this->config->get('websocket.host');
$port = $this->config->get('websocket.port');

return "{$host}:{$port}";
}

private function startingBanner(): ConsoleSectionOutput
Expand Down

0 comments on commit fb521ff

Please sign in to comment.