Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conjoon/lumen-app-email#67 #68

Merged
merged 6 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Set this to true oder false, based on your environment
# See https://laravel.com/docs/8.x/configuration for more information
APP_DEBUG=false
APP_ENV=production
APP_URL=https://ddev-ms-email.ddev.site
APP_EMAIL_PATH=rest-api-email
APP_AUTH_PATH=rest-imapuser
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# conjoon/lumen-app-email ![MIT](https://img.shields.io/github/license/conjoon/lumen-app-email) ![Tests](https://github.com/conjoon/lumen-app-email/actions/workflows/run.tests.yml/badge.svg)
Laravel/Lumen Microservice for RESTful communication with IMAP/SMTP servers.
A service for IMAP/SMTP email messaging based on Lumen.

## What is lumen-app-email?
**conjoon/lumen-app-email** is a PHP application built with [Lumen](https://lumen.laravel.com).
It provides the REST API implementation according to [conjoon\/rest-api-description\#rest-api-email](conjoon/rest-api-description)
It provides the Backend API implementation according to [conjoon\/rest-api-description\#rest-api-email](conjoon/rest-api-description)
and serves as a lightweight backend providing functionality for reading, writing and sending email messages.
<br />
It is a ready-to-use backend for accessing IMAP/SMTP-Servers with minimal setup required.
Expand Down Expand Up @@ -47,7 +47,7 @@ Host: hostname
- need baked-in caching


## Supported REST API
## Supported Backend API
* **rest-api-email**
<br>For the list of endpoints this microservice provides, please refer to the
[OpenApi-documentation of `rest-api-email`](https://github.com/conjoon/rest-api-description), available as OpenAPI documentation at [conjoon.stoplight.io](https://conjoon.stoplight.io/docs/rest-api-description/)
Expand Down
6 changes: 3 additions & 3 deletions app/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
*/
"api" => [
"service" => [
"email" => "rest-api-email",
"auth" => "rest-imapuser"
"email" => env("APP_EMAIL_PATH", "rest-api-email"),
"auth" => env("APP_AUTH_PATH", "rest-imapuser")
],
"versions" => ["v0"],
"latest" => "v0"
Expand Down Expand Up @@ -101,7 +101,7 @@
|
*/

"url" => env("APP_URL", "https://lumen-app-email.ddev.site"),
"url" => env("APP_URL", "https://ddev-ms-email.ddev.site"),

/*
|--------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion app/config/imapserver.php.example
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ return [
"outbox_address" => "smtp.gmail.com",
"outbox_port" => 465,
"outbox_secure" => "ssl",
"root" => ["[Gmail]", "[Google Mail]"],
"root" => ["INBOX", "[Gmail]", "[Google Mail]"],
"match" => ["/\@(googlemail.|gmail.)(com)$/mi"]
],

Expand Down
6 changes: 4 additions & 2 deletions app/routes/rest-api-email/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
$versions = config("app.api.versions");
$latest = config("app.api.latest");

$prefix = config("app.api.service.email");

foreach ($versions as $version) {
$router->group([
"middleware" => "auth_" . ucfirst($version),
'namespace' => "App\Http\\" . ucfirst($version) . "\Controllers",
'prefix' => "rest-api-email/api/" . $version
'prefix' => "$prefix/" . $version
], function () use ($router, $version) {

require base_path("routes/rest-api-email/api_" . $version . ".php");
Expand All @@ -55,7 +57,7 @@
$router->group([
"middleware" => "auth_" . ucfirst($latest),
'namespace' => "App\Http\\" . ucfirst($latest) . "\Controllers",
'prefix' => "rest-api-email/api"
'prefix' => $prefix
], function () use ($router, $latest) {
require base_path("routes/rest-api-email/api_" . $latest . ".php");
});
6 changes: 4 additions & 2 deletions app/routes/rest-imapuser/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
$versions = config("app.api.versions");
$latest = config("app.api.latest");

$prefix = config("app.api.service.auth");

foreach ($versions as $version) {
$app->router->group([
'namespace' => "App\Http\\" . ucfirst($version) . "\Controllers",
'prefix' => "rest-imapuser/api/" . $version
'prefix' => "$prefix/" . $version
], function () use ($router, $version) {
require base_path("routes/rest-imapuser/api_" . $version . ".php");
});
Expand All @@ -52,7 +54,7 @@
// config for latest
$router->group([
'namespace' => "App\Http\\" . ucfirst($latest) . "\Controllers",
'prefix' => "rest-imapuser/api"
'prefix' => $prefix
], function () use ($router, $latest) {
require base_path("routes/rest-imapuser/api_" . $latest . ".php");
});
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
"post-create-project-cmd": [
"@php artisan install"
]
},
"authors": [
Expand Down
119 changes: 119 additions & 0 deletions lib/src/Console/Commands/BaseConfigurationCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* conjoon
* lumen-app-email
* Copyright (C) 2022 Thorsten Suckow-Homberg https://github.com/conjoon/lumen-app-email
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Dotenv\Dotenv;

/**
*
*/
abstract class BaseConfigurationCommand extends Command
{
/**
* Settings read from config/app.php
* @var array
*/
protected $appSettings = [];


/**
* env settings read out from the .env file, which might differ from those found in
* the app settings from config/app.php
* @var array
*/
protected $envSettings = [];


/**
* Prepares configuration process by reading in configuration from .env and config/app.php.
* @return void
*/
protected function prepare()
{
$this->appSettings = include($this->getAppPath());

if (file_exists($this->getEnvFile())) {
$envFile = Dotenv::create($this->getEnvPath());
$this->envSettings = $envFile->load();
}
}


protected function updateEnvSettings($key, $value)
{
$this->envSettings[$key] = $value;
}


protected function flushEnv()
{
$lines = [];

foreach ($this->envSettings as $key => $value) {
$lines[] = "$key=$value";
}

$config = implode("\n", $lines);

file_put_contents($this->getEnvFile(), $config);
$this->line("Updating configuration file...");
}


protected function manualOption()
{
return "<fg=white;bg=blue;options=bold>[type in manually]</>";
}


private function getAppPath()
{
return $this->getRootDir() . "/app/config/app.php";
}


private function getEnvPath()
{
return $this->getRootDir();
}


protected function getEnvFile()
{
return $this->getRootDir() . "/.env";
}


private function getRootDir()
{
return realpath(__DIR__ . "/../../../../");
}
}
126 changes: 126 additions & 0 deletions lib/src/Console/Commands/ConfigureApiCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

/**
* conjoon
* lumen-app-email
* Copyright (C) 2022 Thorsten Suckow-Homberg https://github.com/conjoon/lumen-app-email
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

declare(strict_types=1);

namespace App\Console\Commands;

use Conjoon\Util\ArrayUtil;

/**
*
*/
class ConfigureApiCommand extends BaseConfigurationCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'configure:api';

/**
* The console command description.
*
* @var string
*/
protected $description = "Configure the API paths for this service.";


/**
* Execute the console command.
*/
public function handle()
{
$this->prepare();

$apis = [
"email" => ["api.service.email" => "APP_EMAIL_PATH"],
"auth" => ["api.service.auth" => "APP_AUTH_PATH"]
];

foreach ($apis as $serviceName => $config) {
$appConfigPath = array_keys($config)[0];
$envKey = array_values($config)[0];

$defaultPaths = [$this->getDefaultPathOption($appConfigPath)];
$defaultPaths[] = $this->manualOption();

$path = $this->requireApiPath(
"Please provide the path where the <fg=white;bg=green>$serviceName service</> should be located",
$serviceName,
$defaultPaths
);

$this->updateEnvSettings($envKey, $path);
$this->flushEnv();
}
}


private function requireApiPath(string $message, string $serviceName, array $options)
{
$args = func_get_args();

$path = $this->choice(
$message,
$options,
$options[0]
);

if ($path === $this->manualOption()) {
$path = $this->ask("Please type in the path");
}

$url = $this->envSettings["APP_URL"] . "/" . $path . "/{apiVersion}";

if (!$path || trim((string)$path) == "" || !parse_url($url)) {
$this->error(($path ? $path : "This") . " does not seem to be a valid path. Please try again.");
return $this->requireApiPath(...$args);
}

if (
$this->choice(
"<fg=white;bg=blue>$serviceName service</> locatable " .
"<fg=blue;options=underscore>$url</>, is that okay?",
["yes", "no"],
"yes"
) === "no"
) {
return $this->requireApiPath(...$args);
}


return $path;
}


private function getDefaultPathOption($appPathKey)
{
return ArrayUtil::unchain($appPathKey, $this->appSettings);
}
}
Loading