Prepare for embedded mode

This commit is contained in:
Bernd Bestel
2018-07-16 21:17:32 +02:00
parent db9ee93d2b
commit 3b4141eb4d
11 changed files with 36 additions and 15 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
/public/node_modules /public/node_modules
/vendor /vendor
/.release /.release
embedded.txt

View File

@@ -59,13 +59,16 @@ There is no plugin included for any service, see the reference implementation in
### Database migrations ### Database migrations
Database schema migration is automatically done when visiting the root (`/`) route (click on the logo in the left upper edge). Database schema migration is automatically done when visiting the root (`/`) route (click on the logo in the left upper edge).
### Demo mode
When the file `data/demo.txt` exists, the application will work in a demo mode which means authentication is disabled and some demo data will be generated during the database schema migration.
### Adding your own CSS or JS without to have to modify the application itself ### Adding your own CSS or JS without to have to modify the application itself
- When the file `data/custom_js.html` exists, the contents of the file will be added just before `</body>` (end of body) on every page - When the file `data/custom_js.html` exists, the contents of the file will be added just before `</body>` (end of body) on every page
- When the file `data/custom_css.html` exists, the contents of the file will be added just before `</head>` (end of head) on every page - When the file `data/custom_css.html` exists, the contents of the file will be added just before `</head>` (end of head) on every page
### Demo mode
When the file `data/demo.txt` exists, the application will work in a demo mode which means authentication is disabled and some demo data will be generated during the database schema migration.
### Embedded mode
When the file `/embedded.txt` exists, it must contain a valid and writable path which will be used as the data directory instead of `data/` and authentication will be disabled (used in [grocy-desktop](https://github.com/berrnd/grocy-desktop)).
## Screenshots ## Screenshots
#### Dashboard #### Dashboard
![Dashboard](https://github.com/berrnd/grocy/raw/master/publication_assets/dashboard.png "Dashboard") ![Dashboard](https://github.com/berrnd/grocy/raw/master/publication_assets/dashboard.png "Dashboard")

13
app.php
View File

@@ -6,8 +6,17 @@ use \Psr\Http\Message\ResponseInterface as Response;
use \Grocy\Helpers\UrlManager; use \Grocy\Helpers\UrlManager;
use \Grocy\Controllers\LoginController; use \Grocy\Controllers\LoginController;
if (file_exists(__DIR__ . '/embedded.txt'))
{
define('DATAPATH', file_get_contents(__DIR__ . '/embedded.txt'));
}
else
{
define('DATAPATH', __DIR__ . '/data');
}
require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/data/config.php'; require_once DATAPATH . '/config.php';
require_once __DIR__ . '/config-dist.php'; //For not in own config defined values we use the default ones require_once __DIR__ . '/config-dist.php'; //For not in own config defined values we use the default ones
// Setup base application // Setup base application
@@ -18,7 +27,7 @@ $appContainer = new \Slim\Container([
], ],
'view' => function($container) 'view' => function($container)
{ {
return new \Slim\Views\Blade(__DIR__ . '/views', __DIR__ . '/data/viewcache'); return new \Slim\Views\Blade(__DIR__ . '/views', DATAPATH . '/viewcache');
}, },
'LoginControllerInstance' => function($container) 'LoginControllerInstance' => function($container)
{ {

View File

@@ -22,7 +22,7 @@ class ApiKeyAuthMiddleware extends BaseMiddleware
$route = $request->getAttribute('route'); $route = $request->getAttribute('route');
$routeName = $route->getName(); $routeName = $route->getName();
if ($this->ApplicationService->IsDemoInstallation()) if ($this->ApplicationService->IsDemoInstallation() || $this->ApplicationService->IsEmbeddedInstallation())
{ {
$response = $next($request, $response); $response = $next($request, $response);
} }

View File

@@ -19,9 +19,9 @@ class SessionAuthMiddleware extends BaseMiddleware
$route = $request->getAttribute('route'); $route = $request->getAttribute('route');
$routeName = $route->getName(); $routeName = $route->getName();
if ($routeName === 'root' || $this->ApplicationService->IsDemoInstallation()) if ($routeName === 'root' || $this->ApplicationService->IsDemoInstallation() || $this->ApplicationService->IsEmbeddedInstallation())
{ {
define('AUTHENTICATED', $this->ApplicationService->IsDemoInstallation()); define('AUTHENTICATED', $this->ApplicationService->IsDemoInstallation() || $this->ApplicationService->IsEmbeddedInstallation());
$response = $next($request, $response); $response = $next($request, $response);
} }
else else

View File

@@ -9,7 +9,15 @@ class ApplicationService extends BaseService
*/ */
public function IsDemoInstallation() public function IsDemoInstallation()
{ {
return file_exists(__DIR__ . '/../data/demo.txt'); return file_exists(DATAPATH . '/demo.txt');
}
/**
* @return boolean
*/
public function IsEmbeddedInstallation()
{
return file_exists(__DIR__ . '/../embedded.txt');
} }
private $InstalledVersion; private $InstalledVersion;

View File

@@ -14,7 +14,7 @@ class DatabaseService
{ {
if ($this->DbConnectionRaw == null) if ($this->DbConnectionRaw == null)
{ {
$pdo = new \PDO('sqlite:' . __DIR__ . '/../data/grocy.db'); $pdo = new \PDO('sqlite:' . DATAPATH . '/grocy.db');
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->DbConnectionRaw = $pdo; $this->DbConnectionRaw = $pdo;
} }

View File

@@ -123,7 +123,7 @@ class DemoDataGeneratorService extends BaseService
public function RecreateDemo() public function RecreateDemo()
{ {
unlink(__DIR__ . '/../data/grocy.db'); unlink(DATAPATH . '/grocy.db');
$this->PopulateDemoData(); $this->PopulateDemoData();
} }
} }

View File

@@ -36,7 +36,7 @@ class LocalizationService
private function LogMissingLocalization(string $culture, string $text) private function LogMissingLocalization(string $culture, string $text)
{ {
$file = __DIR__ . "/../data/missing_translations_$culture.json"; $file = DATAPATH . "/missing_translations_$culture.json";
$missingTranslations = array(); $missingTranslations = array();
if (file_exists($file)) if (file_exists($file))

View File

@@ -222,7 +222,7 @@ class StockService extends BaseService
throw new \Exception('No barcode lookup plugin defined'); throw new \Exception('No barcode lookup plugin defined');
} }
$path = __DIR__ . "/../data/plugins/$pluginName.php"; $path = DATAPATH . "/plugins/$pluginName.php";
if (file_exists($path)) if (file_exists($path))
{ {
require_once $path; require_once $path;

View File

@@ -30,8 +30,8 @@
<script src="{{ $U('/node_modules/swagger-ui-dist/swagger-ui-standalone-preset.js?v=', true) }}{{ $version }}"></script> <script src="{{ $U('/node_modules/swagger-ui-dist/swagger-ui-standalone-preset.js?v=', true) }}{{ $version }}"></script>
<script src="{{ $U('/viewjs', true) }}/openapiui.js?v={{ $version }}"></script> <script src="{{ $U('/viewjs', true) }}/openapiui.js?v={{ $version }}"></script>
@if(file_exists(__DIR__ . '/../../data/add_before_end_body.html')) @if(file_exists(DATAPATH . '/add_before_end_body.html'))
@php include __DIR__ . '/../../data/add_before_end_body.html' @endphp @php include DATAPATH . '/add_before_end_body.html' @endphp
@endif @endif
</body> </body>
</html> </html>