nwf modules - elvinzeng/nwf GitHub Wiki

what is nwf module

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.

helloworld module

To demonstrate the nwf module functions, we gives an example here. The helloworld module.

install

list all available module

elvin@elvin-idreamtech ~/temp/nwf/demoproject $ ./nwf_module_manage.sh -a


db_postgres
ๆ•ฐๆฎๅบ“่ฎฟ้—ฎๅฑ‚api-postgres็‰ˆ
-------
helloworld
A demo module for nwf.
-------

install

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.

run server

elvin@elvin-idreamtech ~/temp/nwf/demoproject $ sh ./start.sh
Already up-to-date.
/home/elvin/temp/nwf/demoproject
            ---ParaEngine Server V1.1---  

evaluate effects

open in you browser: http://localhost:8099/helloMod/test1

module management commands

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.

configuration of module source

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

create module

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.

publish module

publish as a framework built-in module

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.

publish to private module source

Commit you directory helloworld of module to directory nwf_modules under the root directory of repository of your private modules project.

debug module on private module source directory

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.

scene

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.

how to debug

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ใ€‚

load

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.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ