initial working version including basic documentation
This commit is contained in:
parent
93b2c01961
commit
a4657bd1e7
|
@ -5,5 +5,10 @@
|
|||
*.sass-cache
|
||||
*.volt.php
|
||||
/.idea/
|
||||
/config/config.php
|
||||
/conf/config.xml
|
||||
/config/config.local.php
|
||||
/conf/*
|
||||
!/conf/.placeholder
|
||||
/cache/*
|
||||
!/cache/.placeholder
|
||||
/temp/*
|
||||
!/temp/.placeholder
|
||||
|
|
56
README.md
56
README.md
|
@ -1,2 +1,56 @@
|
|||
# ui_devtools
|
||||
Support tools for local frontend development
|
||||
Support tools to ease local frontend development using the built-in
|
||||
php webserver.
|
||||
|
||||
The main goal of this repository is to allow user interface development without the need
|
||||
for an actual OPNsense deployment, which eases the process of designing frontend modules.
|
||||
|
||||
Debugging (using xdebug) is supported, although keep in mind that this will slow down all requests as
|
||||
the static pages are also delivered using the php interpreter.
|
||||
|
||||
Requirements
|
||||
============
|
||||
|
||||
Make sure you have the same php (http://www.php.net/) and phalcon (https://phalconphp.com/) versions installed on the target to
|
||||
which you would like to deploy the test server.
|
||||
|
||||
As of this writing OPNsense uses php *7.1.x* and Phalcon *3.3.x*.
|
||||
|
||||
In theory this approach should function on both unix like machines and Windows.
|
||||
|
||||
Setup
|
||||
===========
|
||||
|
||||
Clone this repository and copy `config/config.local.php.sample` to
|
||||
`config/config.local.php` then fill in the required parameters as noted in the
|
||||
configuration sample.
|
||||
|
||||
Normally the only relevant configuration section is the `environment` section, which needs
|
||||
absolute paths to both the OPNsense core files and the plugins you wish to expose.
|
||||
|
||||
For example, using the default build directories:
|
||||
|
||||
```
|
||||
'environment' => array(
|
||||
/* packages to include in setup */
|
||||
'packages' => array(
|
||||
'/usr/plugins/security/tinc'
|
||||
),
|
||||
/* location of OPNsense core package */
|
||||
'coreDir' => '/usr/core',
|
||||
)
|
||||
```
|
||||
|
||||
All working directories are pointed to the local directory where this
|
||||
repository is checked out, to minimize the dependencies.
|
||||
|
||||
|
||||
Startup
|
||||
=======
|
||||
|
||||
Startup the local server, which listens to port 8000 on localhost
|
||||
```
|
||||
php run_server.php
|
||||
```
|
||||
|
||||
Finally point your browser to http://localhost:8000/ and test your ui software.
|
||||
|
|
|
@ -27,36 +27,25 @@
|
|||
*
|
||||
*/
|
||||
|
||||
$conf = new \Phalcon\Config(array(
|
||||
// copy this template to config.local.php and adjust settings
|
||||
return new \Phalcon\Config(array(
|
||||
'application' => array(
|
||||
'controllersDir' => '{plugin_directory}/src/opnsense/mvc/app/controllers/',
|
||||
'modelsDir' => '{plugin_directory}/src/opnsense/mvc/app/models/',
|
||||
'viewsDir' => '{plugin_directory}/src/opnsense/mvc/app/views/',
|
||||
'libraryDir' => '.',
|
||||
'cacheDir' => '{temp_directory}/cache',
|
||||
'baseUri' => '/opnsense_gui/',
|
||||
/* Usually keep defaults as is */
|
||||
'cacheDir' => __DIR__ . '/../cache/',
|
||||
'baseUri' => '/opnsense_gui/',
|
||||
),
|
||||
'globals' => array(
|
||||
/* Usually keep defaults as is */
|
||||
'config_path' => __DIR__ . '/../conf/',
|
||||
'temp_path' => '/tmp/',
|
||||
'temp_path' => __DIR__ . '/../temp/',
|
||||
'debug' => false,
|
||||
'simulate_mode' => true
|
||||
),
|
||||
'environment' => array(
|
||||
/* php includes to add */
|
||||
'includes' => array(
|
||||
'.'
|
||||
/* packages to include in setup */
|
||||
'packages' => array(
|
||||
),
|
||||
/* location of OPNsense core package */
|
||||
'coreDir' => '{root_of_OPNsense_core_repo}',
|
||||
)
|
||||
));
|
||||
|
||||
// Add core views
|
||||
if (strpos($conf->application->viewsDir, $conf->environment->coreDir) === false) {
|
||||
$conf->application->viewsDir = array($conf->application->viewsDir,
|
||||
"{$conf->environment->coreDir}/src/opnsense/mvc/app/views/"
|
||||
);
|
||||
}
|
||||
|
||||
return $conf;
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (C) 2018 Deciso B.V.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
$conf = include __DIR__ . "/config.local.php";
|
||||
|
||||
// Always register OPNsense core libraries into package list
|
||||
$conf->merge(new \Phalcon\Config(["environment" =>
|
||||
["packages" => array(
|
||||
preg_replace('#/+#','/',"{$conf->environment->coreDir}/")),],
|
||||
]
|
||||
));
|
||||
|
||||
// Register our document root as one of the directories to server static pages from
|
||||
$conf->merge(new \Phalcon\Config(["application" =>
|
||||
["docroot" => array(
|
||||
preg_replace('#/+#','/',"{$_SERVER['DOCUMENT_ROOT']}/")),],
|
||||
]
|
||||
));
|
||||
|
||||
// register all packages
|
||||
foreach ($conf->environment->packages as $package) {
|
||||
$packageDirs = array(
|
||||
"controllersDir" => preg_replace('#/+#','/',"{$package}/src/opnsense/mvc/app/controllers/"),
|
||||
"modelsDir" => preg_replace('#/+#','/',"{$package}/src/opnsense/mvc/app/models/"),
|
||||
"viewsDir" => preg_replace('#/+#','/',"{$package}/src/opnsense/mvc/app/views/"),
|
||||
"libraryDir" => preg_replace('#/+#','/',"{$package}/src/opnsense/mvc/app/library/"),
|
||||
"docroot" => preg_replace('#/+#','/',"{$package}/src/opnsense/www/"),
|
||||
"contrib" => preg_replace('#/+#','/',"{$package}/src/opnsense/contrib/")
|
||||
);
|
||||
|
||||
foreach ($packageDirs as $packageDir => $location) {
|
||||
if (is_dir($location)) {
|
||||
if (!isset($conf->application->$packageDir) || !in_array($location,
|
||||
$conf->application->$packageDir->toArray())) {
|
||||
// merge configuration
|
||||
$conf->merge(new \Phalcon\Config(["application" => [$packageDir => array($location),],]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $conf;
|
|
@ -26,8 +26,16 @@
|
|||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
// setup environment
|
||||
global $DEV_WORKDIR;
|
||||
$DEV_WORKDIR = getenv("DEV_WORKDIR"); // passed through from run_server
|
||||
|
||||
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
|
||||
|
||||
// load our configuration, needed to support multiple document root directories
|
||||
$config = include "{$DEV_WORKDIR}/config/config.php";
|
||||
|
||||
// handle local hosted files (js, css, etc)
|
||||
$hosted_local_patterns = array();
|
||||
$hosted_local_patterns[] = '/^\/ui\/css\/.*/';
|
||||
|
@ -39,32 +47,29 @@ $hosted_local_patterns[] = '/^\favicon.*/';
|
|||
foreach ($hosted_local_patterns as $pattern) {
|
||||
if (preg_match($pattern, $uri)) {
|
||||
if (strpos($uri, '/ui/') === 0) {
|
||||
$path = $_SERVER['DOCUMENT_ROOT'] . substr($uri, 3);
|
||||
if (is_file($path)) {
|
||||
$tmp_ext = explode('.', strtolower($path));
|
||||
$mimeTypes = [
|
||||
'css' => 'text/css',
|
||||
'js' => 'application/javascript',
|
||||
'jpg' => 'image/jpg',
|
||||
'png' => 'image/png',
|
||||
'map' => 'application/json'
|
||||
];
|
||||
if (isset($mimeTypes[$tmp_ext[count($tmp_ext)-1]])) {
|
||||
header("Content-Type: {$mimeTypes[$tmp_ext[count($tmp_ext)-1]]}");
|
||||
foreach ($config->application->docroot as $docroot) {
|
||||
$path = $docroot . substr($uri, 3);
|
||||
if (is_file($path)) {
|
||||
$tmp_ext = explode('.', strtolower($path));
|
||||
$mimeTypes = [
|
||||
'css' => 'text/css',
|
||||
'js' => 'application/javascript',
|
||||
'jpg' => 'image/jpg',
|
||||
'png' => 'image/png',
|
||||
'map' => 'application/json'
|
||||
];
|
||||
if (isset($mimeTypes[$tmp_ext[count($tmp_ext)-1]])) {
|
||||
header("Content-Type: {$mimeTypes[$tmp_ext[count($tmp_ext)-1]]}");
|
||||
}
|
||||
readfile($path);
|
||||
return true;
|
||||
}
|
||||
readfile($path);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// setup environment
|
||||
global $DEV_WORKDIR;
|
||||
$DEV_WORKDIR = getenv("DEV_WORKDIR"); // passed through from run_server
|
||||
|
||||
// set user to root for local testing
|
||||
session_start();
|
||||
$_SESSION["Username"]="root";
|
||||
|
@ -82,5 +87,6 @@ if (preg_match("/^\/ui\/.*/", $uri)) {
|
|||
$_GET['_url'] = substr($_SERVER['REQUEST_URI'], 4);
|
||||
require_once "{$DEV_WORKDIR}/stubs/api.php";
|
||||
} else {
|
||||
return false;
|
||||
header('Location: /ui/');
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -51,10 +51,12 @@ if (!is_file("{$config->globals->config_path}/config.xml")) {
|
|||
|
||||
// gather php include paths and add to run command
|
||||
$include_paths = array();
|
||||
foreach ($config->environment->includes as $include) {
|
||||
$include_paths[] = trim($include);
|
||||
foreach ($conf->application->contrib as $include) {
|
||||
if (is_dir($include)) {
|
||||
$include_paths[] = trim($include);
|
||||
}
|
||||
}
|
||||
$run_command[] = '-d include_path="'.implode(':', $include_paths).'"';
|
||||
$run_command[] = '-d include_path=".:'.implode(':', $include_paths).'"';
|
||||
|
||||
// listen to localhost
|
||||
$run_command[] = "-S localhost:8000";
|
||||
|
|
|
@ -28,16 +28,14 @@
|
|||
*/
|
||||
$loader = new \Phalcon\Loader();
|
||||
|
||||
$loaderDirs = array();
|
||||
foreach (array("controllersDir", "modelsDir", "libraryDir") as $topic) {
|
||||
foreach ($config->application->$topic as $path) {
|
||||
$loaderDirs[] = $path;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* We're a registering a set of directories taken from the configuration file
|
||||
*/
|
||||
$loader->registerDirs(
|
||||
array(
|
||||
$config->application->controllersDir,
|
||||
$config->application->modelsDir,
|
||||
$config->application->libraryDir,
|
||||
"{$config->environment->coreDir}/src/opnsense/mvc/app/controllers",
|
||||
"{$config->environment->coreDir}/src/opnsense/mvc/app/models",
|
||||
"{$config->environment->coreDir}/src/opnsense/mvc/app/library",
|
||||
)
|
||||
)->register();
|
||||
$loader->registerDirs($loaderDirs)->register();
|
||||
|
|
Loading…
Reference in New Issue