nwf modules - elvinzeng/nwf GitHub Wiki
In order to reuse the business function module, we abstract a concept that called nwf module. A nwf module is a software package that can be manage by framework and reuse between projects.
To demonstrate the nwf module functions, we gives an example here. The helloworld module.
elvin@elvin-idreamtech ~/temp/nwf/demoproject $ ./nwf_module_manage.sh -a
db_postgres
ๆฐๆฎๅบ่ฎฟ้ฎๅฑapi-postgres็
-------
helloworld
A demo module for nwf.
-------
elvin@elvin-idreamtech ~/temp/nwf/demoproject $ ./nwf_module_manage.sh -i helloworld
Already up-to-date.
Already up-to-date.
module 'helloworld' founded in repository 'nwf'
start install module helloworld...
copy files...
executing www/modules/helloworld/install.sh
helloworld module install...
module helloworld installattion completed.
elvin@elvin-idreamtech ~/temp/nwf/demoproject $ sh ./start.sh
Already up-to-date.
/home/elvin/temp/nwf/demoproject
---ParaEngine Server V1.1---
open in you browser: http://localhost:8099/helloMod/test1
run script without any parameter will output help info.
elvin@elvin-idreamtech ~/temp/nwf/demoproject $ ./nwf_module_manage.sh
options:
-i 'module name'
install module
-d 'module name'
delete module
-u 'module name'
reinstall module
-m
list all installed modules
-a
list all available modules
now, we provide these parameters. but we may add some parameters or options. remember if you want to get latest help info, run script directly and find in output info.
Module has two possible sources, one is the nwf built-in module, the other is the user's own private module source.
Here is how to configure the private source module.
Modify file module_source_repos.conf under the root directory of project, change the following configuration:
elvin@elvin-idreamtech ~/temp/nwf/demoproject $ cat module_source_repos.conf
nwfModules [email protected]:rddept/nwfModules.git
nwfm [email protected]:rddept/nwfm.git
You can add multiple modules source, therefore only need to add more lines of configuration to the configuration file. Each line corresponds to a module source. Each line is divided into two fields, with a space division. The first field is repository name, the second field is the path of the repository. If the first character is '#', this line will be a comment line. Repository name will decide the directory name of repository in project, this is not allowed to repeat, so we suggest repository name consistent with repository name on url. In order to avoid making mistakes, pay attention to using the newline of Unix style at configuration file, encoding to utf-8. If it is operating under Windows, please use the text editor for programmers, such as notepad++. Then, run the script again with option '-a'. Now you can see all the new data.
In addition, each configuration line can have a third field. The third field is an optional field that specifies the branch of the module source repository. If not specified, the default is the master branch.
elvin@elvin-idreamtech ~/temp/nwf/demoproject $ cat module_source_repos.conf
nwfModules [email protected]:rddept/nwfModules.git dev
nwfm [email protected]:rddept/nwfm.git dev
First, create a nwf application. Then, create a directory in "www/modules" directory, the directory is module name. Here in the helloworld module, for example. Directory "www/modules/" file structure is as follows:
. โโโ helloworld --> module root directory โโโ del.sh --> module uninstall script(optional) โโโ desc.txt --> description info of module(optional) โโโ hello.html --> demo โโโ dependencies.conf --> dependencies(optional) โโโ HelloModController.lua --> demo โโโ HelloModValidator.lua --> demo โโโ init.lua --> initializer of module(required) โโโ install.sh --> module install script(optional)
install.sh
#!/bin/bash
echo helloworld module install...
mkdir ../../view/helloworld
cp hello.html ../../view/helloworld
del.sh
#!/bin/bash
echo helloworld module delete...
rm ../../view/helloworld/hello.html
rmdir ../../view/helloworld
desc.txt
elvin@elvin-idreamtech ~/temp/nwf/demoproject/www/modules/helloworld $ cat desc.txt
A demo module for nwf.
init.lua
--
-- init script for helloworld module
-- Author: elvin
-- Date: 17-3-24
-- Time: 10:46
-- desc: this script will be load at mvc framework loaded..
--
print("helloworld module init...");
NPL.load("(gl)www/modules/helloworld/HelloModController.lua");
NPL.load("(gl)www/modules/helloworld/HelloModValidator.lua");
you can explicit register request mappings in init.lua or in your controller.
nwf.registerRequestMapping("/aaa/bbb/ccc/ddd", function(ctx)
return "test", {message = "Hello, Elvin!"};
end, function(params)
-- do validation here
-- return validation result here;
end);
you shoud put you global variables in nwf.modules.moduleName
. e.g.:nwf.modules.helloworld.xxx
,nwf.modules.helloworld.constant
,nwf.modules.helloworld.abc
.
File dependencies.conf under the module root directory is a special files. This is a plain text file which specified dependency modules. The format of this file is simple, just need to write a module name in each line. Utf-8, newline style use Unix style.
Attention: You need to select a globally unique name for you module, to avoid the error.
Commit you directory helloworld of module to directory nwf_modules under the root directory of repository of nwf project, and then send a pull request.
Commit you directory helloworld of module to directory nwf_modules under the root directory of repository of your private modules project.
Through the nwf module mechanism, we can easily extract the public code in the multi-project system, as a separate project is referenced by other projects.
Here is the example of the system I am currently doing. The system is split into five subprojects, the directory structure is as follows:
.
โโโ nwf_init.sh
โโโ nwfModules
โโโ zysy-collector
โโโ zysy-company
โโโ zysy-consumer
โโโ zysy-modules
โโโ zysy-platform
zysy-collector,zysy-company,zysy-consumer,zysy-platform is web project. zysy-modules is a separate project referenced by other web projects. this project is a nwf module repository of some business function module of this system. nwfModules is also separate project referenced by other web projects. this project is a nwf module repository of some common function, such as email, sms.
Suppose I would like to directly write code in zysy-modules project, and then directly start the site project will be able to view the effect of the module.
Then only need to insert the following configuration in the mvc_settings.lua
of the site project:
local moduleSearchPath = '../zysy-modules/nwf_modules/';
table.insert(nwf.mod_path, 1, moduleSearchPath);
By add this configuration, the site will be given priority for zysy-module when loading modules. This can be done very easily by debugging the module code. After module code changes, just restart the web project, you can directly preview the latest changes. After the module code debugging is completed, and then submit zysy-modules project. And re-install all dependencies of web application before deploy to web server. see also project setting docใ
In order to support this special module debugging method, in the module to load other lua file can not use the NPL.load
again.
Because NPL.load
can not load files with relative paths. To solve this problem, the nwf framework provides a function load
for loading files in a module by relative path.
In your module init.lua, if you need to load file under current directory or subdirectory, only need to use the path relative to init.lua as a parameter to load the file.
e.g.:
.
โโโ db_postgres
โโโ helloworld
โ โโโ del.sh
โ โโโ desc.txt
โ โโโ hello.html
โ โโโ HelloModController.lua
โ โโโ HelloModValidator.lua
โ โโโ init.lua
โ โโโ install.sh
โโโ preload_controller_mod
helloworld/init.lua
print("helloworld module init...");
load("HelloModController.lua");
load("HelloModValidator.lua");
-- load("aaa/bbb/ccc.lua");
Note that the load
function can only be used to load files in modules and can not be used in controllers, validators, filters.