Example Introduction

Preparations

Example introduction

This example permits user to login system by SSO(URS), then storage the session of logined-user to a distributed K/V system(Redis), and allow users to fill out forms. after verification, the form data will be stored to RDS(likely mysql Server) and the form file will be stored to the NOS(net object storage) system. It provides some APIs of Restfule style for management backend to audit.



Directory structure of this example

    .
    ├── bin
    ├── config
    ├── docs
    ├── etc
    |── plugins
    ├── public
    ├── src
    ├── test
    ├── .babelrc
    ├── .dockerignore
    ├── Dockerfile
    ├── README.md
    ├── package.json
    └── test.sh
    
The example directory:
File / Directory Description

bin

This directory stores the startup scripts and main programs for each environment (such as DEVELOP,TEST,PRODUCTION environment).

Now you only need to care about the following two files:

  • start.sh : as the start script for TEST and PRODUCTION env
  • dev_start: as the start script for DEVELOP env
  • main.js: as the main program for DEVLOP env(need babel-node run this main program)
  • main.min.js: as the main program for TEST and PRODUCTION env(transformed into es5 by babel, node can run it)
  • You can ignore the other files.

    config

    You can ignore this directory, the configuration will be provided by the configuration services

    docs

    You can save a number of documents related to the project, such as protocol documents, requirements design documents, meeting memories, etc.

    etc

    This is the internal configuration for the need of the Easynode framework

    You can ignore it now.

    plugins

    The directory includes source code of a single page application based on react framework , and configuration for packing. see details

    src

    This directory contains the source code of backend for this example. see details

    test

    This directory contains the source code of test for main business lines

    .babelrc

    The entire range of Babel API options are allowed.

    .dockerignore

    If a file named .dockerignore exists in the source repository, then it is interpreted as a newline-separated list of exclusion patterns. Exclusion patterns match files or directories relative to the source repository that will be excluded from the context. It can even be used to ignore the Dockerfile and .dockerignore files.

    Dockerfile

    Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands you would normally execute manually in order to build a Docker image. By calling docker build from your terminal, you can have Docker build your image step by step, executing the instructions successively.

    README.md

    This file is the introduction of the project's portal, written in MarkDown format.

    package.json

    Specifics of npm's package.json handling,It must be actual JSON, not just a JavaScript object literal.

    test.sh

    It just a test startup shell script, and can be run after the container has been started.

    The plugins directory:
    File / Directory Description

    apidoc

    This directory contains the configuration and scripts to generate the API document of the backend's code.

    assets

    This directory stores a static resource file, such as a picture.

    build

    This directory stores packaged files and mapped files for the front-end resource files such as html, css, js, png as suffix.

    css

    An output directory of the MCSS tool.

    js

    The front-end source code folder of the project, based on the React framework.

                    .
                        ├── constants, Constant definition
                        ├── forms, Each form component
                        ├── mocks, mock data for testing
                        ├── services, services class
                        |── utils, utils class
                        ├── widgets, widget components
                        ├── App.jsx, App component
                        ├── Footer.js, Footer component
                        ├── Header.js, Header component
                        ├── index.js,App entry
                        └── routes.jsx, front-end route based on hash
                    
                

    lib

    Third party dependence, will be replaced webpack. You can ignore it now.

    mcss

    Modular css with mcss

    test

    Static pages before design of components, You can ignore it now.

    views

    Similar to the WWW directory, but can not be named as the WWW directory, because it has been occupied by the easynode framework

    yuidoc

    Description document as a micro service interface. It contains the configuration and scripts to generate the Restful style API document

    README.md

    This file is the introduction of the front-end's portal, written in MarkDown format.

    .eslintrc

    ESLint is designed to be completely configurable, meaning you can turn off every rule and run only with basic syntax validation, or mix and match the bundled rules and your custom rules to make ESLint perfect for your project. This is a configuration file tell eslint how to work.

    mcss.json

    This is a configuration file tell mcss how to work.

    webpack.config.js

    This is a configuration file tell webpack how to work for the development environment.

    webpack.prod.config.js

    This is a configuration file tell webpack how to work for the test or production environment.

    The src/netease/icp/backend directory, The organization structure of this directory is as similar as to organize in the Java programming language that the directory path as the namespace of the class. You can declare a class and create a object like below:
        
        var LoginService = using('netease.icp.backend.services.LoginService');
        var loginService = new LoginService(app);
        
    
    File / Directory Description

    controllers

    This is the controller layer of the MVC Pattern. Controller acts on both model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps view and model separate.

    json

    This is the model layer of the MVC Pattern, is not been used to render to views directly but carring data for protocol.

    models

    This is the model layer of the MVC Pattern, is not been used to render to views directly but as a model of database.

    routes

    Routing refers to the definition of application end points (URIs) and how they respond to client requests. It's master responsiblity is to map the method and URIs and Generator. example:

    • httpServer.addRoute('get', '/login/callback', Controllers.loginCallback(httpServer));
    • httpServer.addRoute('get', '/logout', Controllers.logout(httpServer));

    services

    This is the logic service layer of business.

    Main.js

    The main entrance of this program is similar to the main in Java or c/c++ language

    How to run the example

    • step 1:

      Start a mysql container.

      docker run --name mysql --restart=always -v `pwd`:/etc/mysql/conf.d -v `pwd`/db/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD='test' -e MYSQL_DATABASE=test -e MYSQL_USER=test -e MYSQL_PASSWORD=test -p 3306:3306 -d mysql

    • step 2:

      Start a redis container.

      docker run -d --name redis -p 6379:6379 -v `pwd`/db/:/data redis redis-server /usr/local/etc/redis/redis.conf
    • step 3:

      fill in the config.json , and you can store the file as the same position with dev_start.sh at bin directory

      config.json If the config din't been encoded. You need to uncomment the line "config = JSON.parse(StringUtil.decryptAdv(config));" at the file Main.js.
    • step 4:

      build the image

      docker build -t your_image_name .
    • step 5:

      Run the example

      docker run -ti --name dev_example -p 8888:8888 -v `pwd`:/usr/src/app --workdir=/usr/src/app/bin --env ENV=DEVELOP -e CONFIG_URL=/usr/src/app/bin/config.json -e PORT=8888 your_image_name ./start.sh

    How to debug the example

    • step 1:

      Run the container

      docker run -ti --name dev_example -p 8888:8888 -v `pwd`:/usr/src/app --workdir=/usr/src/app/bin --env ENV=DEVELOP -e CONFIG_URL=/usr/src/app/bin/config.json -e PORT=8888 your_image_name /bin/bash
    • step 2:

      Run the example

      After entering the container, cd /usr/src/app/bin, and run

      supervisor -w ../src -spid /var/tmp/icp.pid sh dev_start.sh

      note: /var/temp/icp.pid is the value of the key easynode.app.id in the file etc/EasyNode.conf, The hot loader willn't work if you have not config the key.

    • step 3:

      Debug the front-end code

      cd plugins; webpack -w & mcss mcss/index.mcss -o css/ -w