Total Pageviews

Friday 14 December 2018

一个基于nodejs的Web Server程序: easy-pm

最近又有种造轮子的冲动,于是打算写一个 Web Server,用来在服务器上管理自己的小项目们。
需求如下:
  • GitHub 自动化部署
    • 初始化及运行
    • 自动注册 Webhook
  • 进程管理
    • 守护进程
    • 数据监控
    • 多核
    • 热重启
  • 文件监听
  • Virtual Hosts
  • Automatic HTTPS
  • HTTP/2 & SPDY
具体地说,就是希望创建一份类似下面的配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
"root": "/var/www",
"port": 80,
"ssl": {
"port": 443,
"auto": true
},
"webhook": {
"host": "http://hk.imsun.net",
"token": "GitHubToken"
},
"apps": [
{
"name": "blog",
"domains": ["imsun.net", "www.imsun.net"],
"repository": "git@github.com:imsun/blog.git",
"port": 8000
},
{
"name": "gh-feed",
"domains": ["gh-feed.imsun.net"],
"repository": "git@github.com:imsun/gh-feed.git",
"branch": "release",
"port": 8001,
"max_memory_restart": "100M"
}
]
}
然后服务器就会
  1. 下载运行所有项目;
  2. 注册监听 GitHub Webhooks;
  3. 在 Let’s Encrypt 注册 SSL 证书;
  4. 开启 HTTP/2 服务器,将流量按域名转发给各个项目;
  5. 监控项目运行情况,在报错或资源占用超标时重启。
目前已经基本实现现有需求,未来看情况还可能会支持 NVM 和 Travis CI。
项目地址: https://github.com/imsun/easy-pm
from https://imsun.net/posts/easy-pm-features
---------------------------------------

easy-pm

NPM version
A simple and easy-to-use web server for managing Node.js projects.

Features

  • Automatic HTTPS
  • HTTP/2 & SPDY
  • WebSockets
  • Easy Deployment from GitHub
  • Process Managing
  • Virtual Hosts
  • Gzip
  • Static Server

Installation

$ npm install easy-pm -g

Usage

Just create a configuration file on your server and easy-pm start it.
$ easy-pm start config.json
Basic example:
{
  "root": "/var/www",
  "ssl": {
    "sites": {
      "example.com": "auto",
      "www.example.com": "auto"
    }
  },
  "webhook": {
    "host": "your server address",
    "token": "your github token"
  },
  "apps": [
    {
      "name": "example",
      "domains": ["example.com", "www.example.com"],
      "repository": "git@github.com:your_github_id/example.git",
      "max_memory_restart": "100M"
    },
    {
      "name": "static-example",
      "domains": ["static-example.com"],
      "repository": "git@github.com:your_github_id/example.git",
      "branch": "gh-pages"
    }
  ]
}

Configuration File

  • root - string The directory to put your applications.
  • port - number optional. The port easy-pm running on. Default 80.
  • gzip - boolean optional. Whether enable gzip for all applications. Default true.
  • ssl - object optional.
    • port - number optional. The SSL port easy-pm running on. Default 443.
    • disable_redirect - boolean optional. Disable redirecting http requests to https for all sites. Default false.
    • sites - object.
      • [domain] - object | "auto" If "auto", easy-pm will install and manage SSL certificate from Let's Encryptautomatically.
        • key - string The path of private key.
        • cert - string The path of certificate.
        • disable_redirect - boolean optional. Disable redirecting http requests to https. Default false.
  • webhook - object optional.
    • host - string Address of your server.
    • token - string GitHub token to access GitHub API.
  • apps - [object] check out Application Configuration for available attributes.

Application Configuration

Common

  • type - string optional. Can be "node""static" or "custom". Default "node".
  • name - string Application name.
  • port - number optional. The port used by your application is passed as env.PORT. Default is a random free port.
  • gzip - boolean optional. Whether enable gzip for this application. Default true.
  • domains - [string] optional. Domains to access this application.
  • repository - string Git address of this application.
  • branch - string Branch to work on. Default "master".
  • max_memory_restart - string optional. Maximum memory over which the application will be restarted, ends with "K", "M" or "G".
  • env - object optional. Environment variables for this application.

Node

In node mode, easy-pm will use npm start to your application.

Static

In static mode, easy-pm will run a static server to serve your application.
  • root - string optional. Root path for the static server. Default "./".
  • 404 - string optional. The file to return when resource not found with status 404. Default "404.html".
  • fallback - string optional. Similar to 404, but returns status 200. Used for some single page applications.

Custom

In custom mode, you can run other non-Node.js project and custom your scripts.
  • script - string Path of file to execute.
  • args - [string] optional. Arguments to pass to the script.
  • interpreter - string The interpreter to execute the script.
  • interpreterArgs - [string] optional. Arguments to pass to the interpreter.

CLI

$ easy-pm -h

  Usage: easy-pm [options] [command]


  Commands:

    start <file>  start service with config file
    stop <file>   stop service with config file
    list          list running applications

  Options:

    -h, --help     output usage information
    -V, --version  output the version number
from https://github.com/imsun/easy-pm

No comments:

Post a Comment