{"id":14989435,"url":"https://github.com/leinue/microless","last_synced_at":"2025-10-18T11:41:06.905Z","repository":{"id":57150519,"uuid":"96899991","full_name":"leinue/microless","owner":"leinue","description":"Using docker and nodejs to build Microservice","archived":false,"fork":false,"pushed_at":"2017-08-27T15:01:22.000Z","size":165,"stargazers_count":17,"open_issues_count":0,"forks_count":7,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-03T01:04:34.549Z","etag":null,"topics":["devops","docker","faas","javascript","koajs","microservice","nodejs","serverless"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leinue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-11T14:04:38.000Z","updated_at":"2023-09-08T17:27:21.000Z","dependencies_parsed_at":"2022-09-03T16:50:45.730Z","dependency_job_id":null,"html_url":"https://github.com/leinue/microless","commit_stats":null,"previous_names":["authing/microless"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leinue%2Fmicroless","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leinue%2Fmicroless/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leinue%2Fmicroless/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leinue%2Fmicroless/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leinue","download_url":"https://codeload.github.com/leinue/microless/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248501874,"owners_count":21114688,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["devops","docker","faas","javascript","koajs","microservice","nodejs","serverless"],"created_at":"2024-09-24T14:18:22.092Z","updated_at":"2025-10-05T21:48:55.350Z","avatar_url":"https://github.com/leinue.png","language":"JavaScript","readme":"![arch](https://github.com/Authing/micro.js/blob/master/assets/logo.png?raw=true)\n\nMicroservice framework for node.js to make container-based microservice web applications and APIs more enjoyable to write. Micro is based on koa.js, allowing you to use all the features that koa has.\n\n## Docs\n\n1. [中文文档](https://github.com/Authing/microless/blob/master/docs/CN_README.md)\n\n## Features\n\n\u003e Make microservice reachable \n\n![arch](https://github.com/Authing/micro.js/blob/master/assets/Architecture.png?raw=true)\n\n1. Deploying microservices with docker containers, using docker stack deploy and docker swarm\n2. Transferring the data between the services via JSON strings and RESTful API\n3. Every single serivice has it's own database\n4. API Gateway serves as the controlling unit, which controls the whole system.You can do some universal works like auth or log\n5. Using docker deploy to manage containers and services\n6. Easily Integrated with koa middlewares\n\n## Installation\n\n### Install Docker\n\nplease visit [docker doc](https://docs.docker.com/get-started)\n\n**ATTENTION:** ```docker swarm``` and ``` docker-compose ``` is also needed.\n\n### Install Microless\n\nMicroless requires node v7.6.0 or higher for ES2015 and async function support.\n\n```\n$ npm install microless --save\n```\n\n## Getting started\n\nThe example shows the ability to start a python container using microless, you can get the source code in folder [example](https://github.com/Authing/microless/tree/master/example)\n\n### Write Python Code\n\n``` python\n\nfrom flask import Flask\n\napp = Flask(__name__)\n\n@app.route(\"/\")\ndef hello():\n    html = \"\u003ch3\u003eHello {name}!\u003c/h3\u003e\"\n    return html.format(name=\"world\")\n\nif __name__ == \"__main__\":\n    app.run(host='0.0.0.0', port=80)\n\n```\n\nThis program will run a python server at port 80.\n\n### Write Dockerfile\n\n``` shell\n\n# Use an official Python runtime as a parent image\nFROM python:2.7-slim\n\n# Set the working directory to /app\nWORKDIR /app\n\n# Copy the current directory contents into the container at /app\nADD . /app\n\n# Install any needed packages specified in requirements.txt\nRUN pip install Flask\n\n# Make port 80 available to the world outside this container\nEXPOSE 80\n\n# Run app.py when the container launches\nCMD [\"python\", \"app.py\"]\n\n```\n\nThis Dockerfile defines a image which can start a python server at port 80.\n\n### Write Compose File\n\nSave as ```docker-compose.yml```\n\n``` shell\n\nversion: \"2\"\nservices:\n  web:\n    image: 'example_web'\n    build: .\n    ports:\n      - \"4000:80\"\n\n```\nThis compose file starts a python container called ```web``` with exposed port ```4000```, which uses the above ```Dockerfile```.\n\nFor more details please visit [docker docs](http://docker.com).\n\n### Write Microless Code\n\n``` javascript\n\n// import microless\nconst Micro = require('microless');\n\n// config restful api routers\nconst routers = {\n  '/': {\n    method: 'get' // define the request method\n  }\n}\n\nvar micro = new Micro({\n\n  name: 'test', //project name\n\n  compose: {\n    src: './docker-compose.yml' //docker compose file\n    dockerfile: '.'\n  },\n\n  // router to microservice  \n  modems: {\n\n    // name in docker compose files\n    web: {\n      configs: routers,\n    }\n\n  },\n\n  server: {\n    port: 3001\n  }\n});\n\n```\n\nThis will run the service in a docker container named ```'example_web_1'```.\n\nThen the project will run at port 3001.\n\nWhen you visit ```http://locahost:3001```, you will see the result from python programs, every single request from ```http://locahost:3001``` will automatically router to the right microservice.\n\n![run](https://github.com/Authing/microless/blob/master/assets/run.png?raw=true)\n\n## Other Configs\n\n### Compose\n\nCompose defines the src of file ```docker-compose``` and ```dockefile```\n\n``` javascript\n\n  compose: {\n    src: './docker-compose.yml', //default is './docker-compose.yml'\n    dockerfile: '.' //dockerfile directory, default is .\n  }\n\n```\n\n### Modems\n\nModems mainly defines the router to microservice. Every single request from ```http://locahost:3001``` will automatically router to the right microservice, so your router configs in the microless must ```as same as``` the router defined in the microservice.\n\nFor example, if you define a container called ```web``` in docker-compose.yml, you must write ```web``` as a ```key``` in modems like this:\n\n``` javascript\n\n  modems: {\n    web: {\n      configs: routers\n    },\n\n    a: {\n      configs: aRouters\n    },\n\n    ...\n  }\n\n```\n\n#### Router Configs\n\nA symbol config is like this:\n\n``` javascript\n\n  const routers = {\n    '/': {\n      //called when route ends\n      afterRoute: function(ctx, next, response) {\n        ctx.body = response.body;\n      },\n      method: 'get'\n    },\n\n    '/shit/:id': {\n      //called when route ends\n      afterRoute: function(ctx, next) {\n        ctx.body = 'shit api 0.1, params=' + JSON.stringify(this.params);\n      },\n      method: 'get'\n    }\n  }\n\n```\n\nThere are two attributes that router config has:\n\n1. ```method```: **required**, defines a http request method\n2. ```afterRoute```: **optional**, called when route ends, you can handle the result from microservice and display it in other way.when you use this attribute, you must write ```ctx.body = xxx```, otherwise you would see a blank page.\n\nP.S. The router follows the koa-router.\n\n#### Error Handling in Modems\n\nCurrently, microless has three error handling methods when modem to a microservice:\n\n``` javascript\n\n  modems: {\n    web: {\n      configs: routers,\n\n      //called when modem on error\n      onError: function(ctx, next, error) {\n        ctx.body = error;\n      },\n\n      //called when method not supported\n      methodNotSupported: function(ctx, next, error) {\n\n      },\n\n      //called when route not found\n      routeNotFound: function(ctx, next, error) {\n\n      }\n    }\n  }\n\n```\n\n1. ```onError```: **optional**, called when modem on error.\n2. ```methodNotSupported```: **optional**, called when method not supported.\n3. ```routeNotFound```: **optional**, called when route not found.\n\n### Server\n\nServer just has one attribute:\n\n1. ```port```: **required**, defines the main port of the service.\n\n### Error Handling\n\n1. ```onSuccess```: **optional**, called when successfully exectuing docker-compose\n2. ```onError```: **optional**, called when exectuing docker-compose failed\n\nA complete start code is like this:\n\n``` javascript\n\nconst Micro = require('../src');\n\nconst routers = {\n  '/': {\n    //called when route ends\n    afterRoute: function(ctx, next, response) {\n      ctx.body = response.body;\n    },\n    method: 'get'\n  },\n\n  '/shit/:id': {\n    //called when route ends\n    afterRoute: function(ctx, next) {\n      ctx.body = 'shit api 0.1, params=' + JSON.stringify(this.params);\n    },\n    method: 'get'\n  }\n}\n\nvar micro = new Micro({\n\n  name: 'test',\n\n  compose: {\n    src: './docker-compose.yml',\n    dockerfile: '.'\n  },\n\n  modems: {\n    web: {\n      configs: routers,\n\n      //called when modem on error\n      onError: function(ctx, next, error) {\n        ctx.body = error;\n      },\n\n      //called when method not supported\n      methodNotSupported: function(ctx, next, error) {\n\n      },\n\n      //called when route not found\n      routeNotFound: function(ctx, next, error) {\n\n      }\n    }\n  },\n\n  server: {\n    port: 3001\n  },\n\n  //called when successfully exectuing docker-compose\n  // onSuccess: function() {\n\n  // },\n\n  //called when exectuing docker-compose failed\n  onError: function(error) {\n    console.log(error);\n  }\n});\n\n\n```\n\nEnjoy your microservice with docker and nodejs :)\n\nFor more visit author's website: [ivydom](http://ivydom.com)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleinue%2Fmicroless","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleinue%2Fmicroless","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleinue%2Fmicroless/lists"}