Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Introduction


This page contains the folder and file schema that will be generated from the swagger editor.The approach relies on the composer to generate the service at init time ( also allows us the ability to support update service in later stages )provides information on how to generate a microservice using NPM and Grunt from the files that were created and provided by the output from step 01 - Swagger editor.

The previous step, created the main files and the recipe needed to create a microservice. In this step, we just need to run NPM and Grunt so that the recipe gets applied and generate the files and folders that will hold the business logic of our microservice.

The folder/files and code structure relies on the soajs.composer that gives us the flexibility to create and modify a microservice without the need to maintain the REST Layer, Model Injection at run-time or Initializing and closing database connectivity.

This means that the folders and files will be generated to be composer dependent. To learn more about the composer click here.

Generated

schema

This is the generated folder schema that you will get when you press generate service button. We will check that every input is valid before we create the service folder that will be downloaded into your PC.

This won't be your final folder schema, proceed with the following steps to completely build your service

Files


Step 01 - Swagger editor generates a list of files for us, puts them in a folder, zips the folder and returns it to us.

Attached you find a sample of a microservice that was generated in that step.

View file
namesampleSchema.zip
height250

After you got your folder, go to The generated files are not final, you still need to run a final command to run the build recipe.


Build


Download the zip file and extract it, then open your terminal and run the following commands:

Code Block
language
powershell
bash
titleBuild Your microservice
linenumberstrue
$ cd 
yourNewServiceLocalPath
your_new_service_extracted_folder
$ mkdir node_modules
$ npm install
$ grunt build

These commands The above will create a folder "node_modules" inside your main service extracted folder, download all the dependencies needed inside it and finally build your run a grunt build command that populates the final list of files and folders for your microservice.

On build, we will create the following files :

  • The Update the config.js file containing and add all your APIs.
    • Each API will point to a middleware file located in
    the
    • a new generated folder lib/mw
    that was created after the command grunt build,
    • where you
    will
    • should write your business logic.
  • The folder lib will contain another folder named models where we will Generate a new folder lib that contains sub-folders:
    • models: where we should create you database models pending on your database information captured in the service information tab of step 1.
    In
    • Inside each model, you will find all the
    essential function
    • functions written and ready to
    use like
    • for usage: save, update, find, insert, etc... .
    You should
    • Simply enter the database name and collection
    directly in case you have created one instance of a database model, or make it dynamic in case of multiple instance of a database model.
  • The index file created will call the composer that deploys your service.
  • The test folder will contain what is necessary to
    • at the top of the file where instructed or pass them as dynamic values from the business logic inside lib/mw files.
  • Generate a test folder that contains the necessary files and folders so you can write and run your test cases with the help of GruntFile.js.
This is the final generated schema :
  • Grunt.

Attached you find a sample of schema after we ran the above commands on the sample mentioned in the above section.

View file
namesimpleService.zip
height250

Editing your code

In this version, after you build your service, you can only edit your swagger.yml and run the following command to rebuild your service :


WarningDon't

Fill in the Business Logic


Once you run the commands above, all you need to do now is open each middleware file and write the business logic inside it.

Each API points to a middleware file named after the route and method of the API; both the method and route where extracted from the swagger.yaml file which contains the YAML code you entered in the Swagger UI Editor in step 01 - Swagger editor.

Warning
  • Do not change the files name after the first generate.
  • Don't Do not change the service name after the first generate.
  • Don't Do not change the folder schema after the first generate.

Update a microservice


After you build your service, you can always modify it.

When you modify a service, you can add/update or remove APIs. and you can do that very easily.

Simply update the swagger.yml YAML code with your new changes and run the below command.

Code Block
language
powershell
bash
titleRegenerate Microservice folders/files
linenumberstrue
$ cd your_new_service_extracted_folder
$ grunt rebuild

When we you rebuild your service, we will keep , the generator keeps all your existing middleware files even if you deleted an API or changed its name.The file config.js file will be updated and the new middlewares will be created in case of any.it does not delete them or replace them.

This means if you kept an API untouched or removed an API from your YAML code, the middleware file will still be there.

If you renamed an API route or changed its method, a new middleware file is generated, so make sure you cater for the business logic code update.

The generator also rewrites the config.js file.

This also means that after you rebuild, all that is needed is for you to fill in the business logic of the APIs.