WebTemplateStudio icon indicating copy to clipboard operation
WebTemplateStudio copied to clipboard

Add MoleculerJS

Open AndreMaz opened this issue 7 years ago • 23 comments

Moleculer is a progressive microservices framework for Node.js. It is highly customizable and has a lot of cool features. It also has a project template that can be modified/adapted, and be added to the WebTemplateStudio.

AndreMaz avatar Aug 21 '19 22:08 AndreMaz

Hi @AndreMaz, thank you so much for your suggestion! It seems a great framework to consider, we will add it into backlog and investigate when we have dev capability!

Also, if you are interested in contributing, please feel free to look at the contribution guidelines.

Tanya0609 avatar Aug 23 '19 19:08 Tanya0609

Hey @Tanya0609. Thanks for your response.

I can definitely help with the template but I need some guidance. Where should I put the metadata (description, logo, and etc.) about Moleculer? I mean, how can I add a new framework to the list back-end frameworks? The Add Backend Framework Wiki has no mention about it.

AndreMaz avatar Aug 30 '19 20:08 AndreMaz

So I've managed to get things working Wizard: image Moleculer+Vue FM2eNnx8T4

The only thing that is missing is the integration with Azure Cosmos.

Is there any interest in having this added to WebTemplateStudio? If so, then I can make a PR.

AndreMaz avatar Sep 02 '19 20:09 AndreMaz

Hi @AndreMaz this looks great, if you make a PR, then we would look at it. Thank you!

smmatte avatar Sep 05 '19 20:09 smmatte

@smmatte PR https://github.com/microsoft/WebTemplateStudio/pull/1173 got 3 approvals so I'm assuming that it eventually is going to be merged. Following this line of thought I want to discuss the contents of next PR.

I was thinking about adding unit tests (Moleculer plays well with Jest) and Docker files. What do you think?


Also, while playing with WebTS I've noticed that file structure of generated projects is not consistent. To show you what I mean check the top-level file structure for Angular+Node, React+Node, Vue+Node and Vue+Moleculer image

  • e2e folder is only present in Angular+Node (but I guess it's because Angular is the most stable framework). However, where would the unit tests go? Into a new tests folder? In this case we would have 2 top-level folders (tests and e2e) with tests. Placing e2e at top-level doesn't seem right.

  • In Node and Flask projects the app logic is located in server folder while in case of Moleculer it resides in services folder. Moleculer is a microservices framework so it would be wrong to place services in server folder just to keep consistency with Node and Flask.

I would suggest to create a uniform and consistent top-level file structure. This way any generated project would feel familiar regardless of tech stack. For example:

.
├── backend/            - Backend related files
│ ├── tests/            - Backend unit tests would be placed here
│ ├── data/             - Data samples
│ └── ***               - Any other file or folder used in backend
├── frontend/           - Frontend related files
│ ├── tests/            - Frontend unit tests would be placed here
│ ├── components/       - Components folder
│ └── ***               - Any other file or folder used in frontend
├── README.md           - README file        
├── tests/              - E2E tests would be placed here
└── ***                 - Project config files or folder(s) that are related to backend and frontend

backend is a more neutral term than server or services. Also, frontend as a folder name is more explicit than src. Just by looking at these folders I would know where to go to tweak some component or fix a buggy DB query.

Anyway, this is just an idea and this is probably something that should be done by WebTS dev team.

AndreMaz avatar Sep 15 '19 14:09 AndreMaz

Also, while playing with WebTS I've noticed that file structure of generated projects is not consistent. To show you what I mean check the top-level file structure for Angular+Node, React+Node, Vue+Node and Vue+Moleculer

@AndreMaz can you open a separate issue for the proposal to unify the file structure? I can see arguments for both options, unified output between different frameworks and each framework being as close to how people work on the framework possible. I'd like to get more feedback from the community.

The only thing that is missing is the integration with Azure Cosmos.

What's the status regarding integration of Moleculer and CosmosDB? Can we help somehow?

sibille avatar Sep 27 '19 11:09 sibille

@AndreMaz can you open a separate issue for the proposal to unify the file structure?

Yes, of course. @sibille I've created https://github.com/microsoft/WebTemplateStudio/issues/1189

What's the status regarding integration of Moleculer and CosmosDB? Can we help somehow?

To be honest, I've never used CosmosDB before. So I'll need some time to integrate it with Moleculer. Right now I'm just looking at tutorials and trying to become familiar with it. So, any help would be appreciated.

AndreMaz avatar Sep 27 '19 11:09 AndreMaz

What's the status regarding integration of Moleculer and CosmosDB? Can we help somehow?

@sibille status update: I've managed to make Moleculer work with CosmosDB (via Mongo API). Still have to do some tweaks in composition templates and test everything but in a few days I will create a PR

AndreMaz avatar Sep 29 '19 19:09 AndreMaz

@AndreMaz, after the last changes moleculer seems to have stopped working when deployed to azure app service. Could you have a look? Seems that the backend is not started correctly.

sibille avatar Oct 21 '19 15:10 sibille

@sibille sure thing. Can you provide any additional info that could narrow down the source of the problem?

AndreMaz avatar Oct 21 '19 15:10 AndreMaz

Steps to reproduce:

  1. Create an app with react + moleculer + all pages and app service
  2. Open project
  3. Launch VS Code Command: "Web Template Studio: Deploy App" This command executes "npm install", "npm run build" and deploys the folder "server" to Azure App Service.

-> The frontend is deployed correctly, but there seems to be no connection with the backend services (because the backend did not get deployed + started in Azure App Service) For example the react + node app, has a package.json file in the server folder with a script that launches the backend server.

So, we think moleculer should include the services into the server folder and provide some way to launch them so they are available to the frontend.

sibille avatar Oct 21 '19 16:10 sibille

Thanks for the repro steps. So this is kind of related to the uniform top-level file structure issue that I've raised some time ago.

I will talk with @icebob to see what he thinks about it. But this would conflict with the idea of https://github.com/microsoft/WebTemplateStudio/issues/1124#issuecomment-535902279

each framework being as close to how people work on the framework possible.

because in Moleculer apps the logic is located in services folder contrary to Express and Flask where logic resides in server.

AndreMaz avatar Oct 21 '19 16:10 AndreMaz

I think we can make the folder name configurable. What would a typical deployment of moleculer in Azure App Service look like?

sibille avatar Oct 21 '19 16:10 sibille

Ok. Now that I've looked closely at react + node app I think that I understand what's actually happening.

The react + node app has two package.json files - one at top dir . that corresponds to dev environment and another in ./server that corresponds to production environment . So in dev mode the npm start corresponds to:

"concurrently \"yarn start-frontend || npm run start-frontend\" \"yarn start-backend || npm run start-backend\""

And in production mode npm start corresponds to: node ./server.js

However, in case of Moleculer there is no server folder nor package.json in it. So when deploying the app in Azure App Services there is no entry point that will start the backend.


I don't fully understand the rationale behind the 2 package.json files. @sibille @dgomezc can you explain this design? Why not have just one package.json located at top level?

With a single package.json we could have something like this for react + node app:

  • For development: npm run dev would correspond to "concurrently \"yarn start-frontend || npm run start-frontend\" \"yarn start-backend || npm run start-backend\""
  • For production: npm run start would be node ./server.js

Then for Launch VS Code Command: "Web Template Studio: Deploy App" we would have to run npm install --production (to install only the dependencies and not devDependencies), npm run build (to build frontend) and then run npm run start.


Regarding typical Moleculer deployment it has only one top-level package.json with the following scripts:

"dev": "moleculer-runner --repl --hot services",
"start": "moleculer-runner"

and a Docker file (also top level) that looks like this:

FROM node:current-alpine
ENV NODE_ENV=production

RUN mkdir /app
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --production

COPY . .

CMD ["npm", "start"]

Docker copies all the contents located in . the then runs npm start that will "ask" moleculer-runner to start the services.

Finally, in docker-compose.yaml I "inform" with environment variables the moleculer-runner what service(s) should start in each container. For example, take a look at a container that would start only the api service

version: "3.2"
services:
  api:
    build:
      context: .
    image: moleculer-demo
    container_name: moleculer-demo-api
    env_file: docker-compose.env
    environment:
      SERVICES: api # Start only API service in this container
      PORT: 3000
    depends_on:
      - nats

AndreMaz avatar Oct 21 '19 18:10 AndreMaz

@sibille I can update the templates in any way that you that you like:

  • Option 1: Move all Moleculer's related files to server and add an additional package.json just for deployment. This way the only thing that will be affected would be Moleculer's templates.
  • Option 2: Change the current way of deploying things. This would require to:
    • Update the way that Launch VS Code Command: "Web Template Studio: Deploy App" works
    • Refactor all templates that have 2 package.json files in their structure in a way that there is only one package file
    • Change the package scripts to something like:
     "dev": "concurrently \"yarn start-frontend || npm run start-frontend\" \"yarn start-backend || npm run start-backend\""
     "start": <backend-related script that starts the application>
    
    In case of Moleculer startwould look like:
    • Option A: "start": "moleculer-runner --env" + In .env file I would have to set services directory.
    • Option B: "start": "moleculer-runner --env services" directly setting the services dir

What you think is the best solution for this?

AndreMaz avatar Oct 21 '19 19:10 AndreMaz

Not sure why package.json are separate, will follow up on this.

For now I'd go with the first option. We can then make the folder name configurable as part of https://github.com/microsoft/WebTemplateStudio/issues/1181, so moleculer can use "services" and node + python can use "server".

sibille avatar Oct 21 '19 20:10 sibille

Not sure why package.json are separate, will follow up on this.

Please do it. Personally, I think that it's very confusing to have 2 packages for a single project. Also, I've made a quick search for some popular node boilerplates and none of them have 2 package.json

  • https://github.com/sahat/hackathon-starter
  • https://github.com/kriasoft/nodejs-api-starter
  • https://github.com/madhums/node-express-mongoose
  • https://github.com/talyssonoc/node-api-boilerplate

I'm really curious about the current way of structuring things.

AndreMaz avatar Oct 21 '19 21:10 AndreMaz

@sibille is there any update on double package.json?

AndreMaz avatar Oct 22 '19 20:10 AndreMaz

Yes, there are various reasons:

  • Allow development of client and server separately
  • The package.json from the server folder is needed for deployment on Azure App Service (https://docs.microsoft.com/en-us/azure/app-service/containers/configure-language-nodejs). We only need the server script and dependencies here, as the client part is deployed as a static website.
  • Outer package.json is for local development as you said,

That said, happy to discuss other strategies or ways to make the current setup clearer, but let's open a separate issue as this a bigger change that affects the other frameworks.


Back to moleculer: Did deployment to Azure App Service work for moleculer with the server folder and package.json? We're planning to ship a version of WebTS this week to get some other bugs fixed and it would be great to include the fix for moleculer deployment.

sibille avatar Oct 23 '19 07:10 sibille

Sorry, I didn't tested yet. Give me a couple of hours and I will create a PR.

That said, happy to discuss other strategies or ways to make the current setup clearer, but let's open a separate issue as this a bigger change that affects the other frameworks.

Ok. I will do it.

AndreMaz avatar Oct 23 '19 08:10 AndreMaz