fly
A library for develop backend easy
Last updated 16 days ago by hfcorriez .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install fly 
SYNC missed versions from official npm registry.

    _/_/_/_/  _/    _/      _/
   _/        _/      _/  _/
  _/_/_/    _/        _/
 _/        _/        _/
_/        _/_/_/_/  _/

FLY is functionally: A library for develop backend easy.

  • Isolation: one file is one function
  • Modularization: one directory is one service
  • Configurability: config anything hierarchically

Installation

Require node >= 8

## Yarn
$ yarn global add fly

## NPM
$ npm install -g fly

Example

Write function

proxy.js

module.exports = {
  async main (event, context) {
    return {url : event.query.url}
  },

  configHttp: {
    method: 'GET',
    path: '/proxy'
  }
}

Run

$ fly http -r ↙

┌────────┬────────────────┬────────┬────────┐
│ Method │ Path           │ Domain │ Fn     │
│ GET    │ /api           │        │ index  │
│ GET    │ /api/userLogin │        │ login  │
│ POST   │ /upload        │        │ upload │
│ GET    │ /              │        │ home   │
│ GET    │ /static/:path+ │        │ static │
└────────┴────────────────┴────────┴────────┘
SERVER READY
  NAME:      HTTP
  ADDRESS:   http://127.0.0.1:5000
  PID:       20815
  WORK DIR:  /Users/YourName/Code/fly-example
  HOT RELOAD: true

Defintions

Function Defintion

{
  extends: String,                              // Extends from function, support file, package
  imports: Object {String: String}              // Inject function to context
  config: Object {String: Any},                 // Config object
  retry: Number || Boolean,                     // Retry count, true is 3
  main: Function (event, ctx),                  // Main
  validate: Function (event, ctx),              // Validate
  before: Function (event, ctx),                // Before filter
  after: Function (event, ctx),                 // After filter
  catch: Function (event, ctx),                 // Error catch
  config<Event>: Object || Boolean || Function, // Startup event
  before<Event>: Function (event, ctx),         // Before filter
  after<Event>: Function (event, ctx),          // After filter
  validate<Event>: Function (event, ctx),       // Validate event
  catch<Event>: Function (event, ctx),          // Error catch
}

Context Defintion

eventId: String                       # Event ID
eventType: String                     # Event Type:http, command, null is internal
originalEvent: Event                  # OriginalEvent
parentEvent: Event                    # Parent Event

call: Function                        # Invoke function
list: Function                        # List functions
get: Function                         # Get function
error: Function                       # Trigger error internal
<fn>: Function                        # The functions imported

trace: Object                         # Current trace
config: Object                        # Config object

Command Usage

Usage:

  fly <command> [--options]

System Commands:

  api [command]                  API service
    [command]                    start | stop | reload | restart | status | log
    --port,-p number             Bind port
    --instance,-i number         The instance number
    --all,-a                     All applications
    --bind,-b string
  call <fn>                      Call function
    <fn>                         Function name
    --type string                Set event type
    --data,-d string             Set event data
    --timeout, -t                Set timeout
  show <fn>                      Show function info
    <fn>                         Function name
  help                           Show help
    --system,-s                  Show system commands
  http [command]                 HTTP service
    [command]                    start | stop | reload | restart | status | log
    --port,-p number             Bind port
    --instance,-i number         The instance number
    --all,-a                     All applications
    --bind,-b string
    --hotreload,-r               Run with hot reload mode
  install                        Install deps
    --list,-l                    List packages to install
  list                           List functions
    --type string                List with type
    --all                        List all commands
  new [dir]                      Create new service dir
    [dir]                        Dir name
    --force                      Force create when dir exists
  serve [command]                Serve service
    [command]                    start | stop | reload | restart | status | log
    --port,-p number             Bind port
    --instance,-i number         The instance number
    --all,-a                     All applications
    --bind,-b string

Global options:

    --id,-i string               Set event id
    --verbose,-V                 Show verbose

Events

Event can be anything, but must can be JSONify

HTTP

Http Event

method: String                # request http method, lowercase
path: String                  # request http path
origin: String                # request http origin
host: String                  # request http host
domain: String                # request domain
url: String                   # request full url
protocol: String              # request protocol
port: Number                  # request port
ip: String                    # request ip
headers: Object               # request headers
body: Object                  # request body
files: Object                 # request files
query: Object                 # request query
search: String                # request search string without ?
cookies: Object               # request cookies

Http Config

method: String                    # Set method, eg: get, post, put, delete
path: String                      # Set path, eg: /api
domain: String | Array            # Set domain you want supply service
cache: Boolean | Number           # Set page cache header, `true` is 600 seconds
cors: Boolean | String            # Set http CORS header, `true` is for all origin, String set origin, object set params
  origin: String
  headers: String
  methods: String
upload:
  allowTypes: Array               # mimetypes, eg: ['png', 'image/jpeg']
  maxSize: Number                 # maxSize, default is 100mb

Command

Command Event

args: Object                      # command args, eg: "--help"
params: Object                    # command params, eg: "call <param>", param will pass as params.param

Command Config

_: String                         # command declare, eg: "call <param>"
args: Object                      # command args declares: `"--help": Boolean`
alias: Object                     # command alias declares, eg: `"--help": '-h'`
descriptions: Object              # command descriptions
  _: String                       # command description
  <param>: String                 # param description
  <args>: String                  # arg description

Command Example

module.exports = {
  main (event, ctx) {
    const command = event.params.command
    const showFull = event.args.full

    // logic
  },

  configCommand: {
    _: 'help <command>',
    args: {
      '--full': Boolean
    },
    alias: {
      '--full': '-f'
    },
    descriptions: {
      _: 'Show help for commands',
      '<help>': 'Command name'
      '--full': 'Show full descriptions'
    }
  }
}

Cron

Cron Event

time: timestamp

Cron Config

time: '* * * * *'   # See format defintion https://en.wikipedia.org/wiki/Cron
timeout: 60         # Maximum time limit

Cron Example

module.exports = {
  main (event, ctx) {
    // tick on every 30min
  },

  configCron: {
    time: '*/30 * * * *'
  }
}

fly.yml

Optional. You can place fly.yml in directory to overwrite funciton's config

fly.yml

files:
  - "**/*.fly.js"

# Events config overwrite
events:
  http:
    domain:
      - api.com
      - localhost

# Config overwrite
config:
  db:
    host: localhost
  module@:
    db: 'test:3333'
  url@:
    url: hello

API

You can use in Nodejs and call fly function directly

Usage

const Fly = require('node-fly')
const fly = new Fly('/dir')
await fly.call('test', {host: 'api.com'})

LICENSE

Copyright (c) 2019 hfcorriez hfcorriez@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Current Tags

  • 2.0.10-beta                                ...           beta (3 years ago)
  • 3.3.6                                ...           latest (16 days ago)

96 Versions

  • 3.3.6                                ...           16 days ago
  • 3.3.5                                ...           2 months ago
  • 3.3.4                                ...           3 months ago
  • 3.3.3                                ...           3 months ago
  • 3.3.2                                ...           4 months ago
  • 3.3.1                                ...           4 months ago
  • 3.3.0                                ...           4 months ago
  • 3.2.2                                ...           6 months ago
  • 3.2.1                                ...           6 months ago
  • 3.2.0                                ...           6 months ago
  • 3.1.3                                ...           7 months ago
  • 3.1.2                                ...           8 months ago
  • 3.1.1                                ...           8 months ago
  • 3.1.0                                ...           8 months ago
  • 3.0.6                                ...           9 months ago
  • 3.0.5                                ...           10 months ago
  • 3.0.4                                ...           a year ago
  • 3.0.3                                ...           a year ago
  • 3.0.2                                ...           a year ago
  • 3.0.1                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.0.6 [deprecated]           ...           3 years ago
  • 2.0.5 [deprecated]           ...           3 years ago
  • 2.0.4 [deprecated]           ...           3 years ago
  • 2.0.3 [deprecated]           ...           3 years ago
  • 2.0.2 [deprecated]           ...           3 years ago
  • 2.0.1 [deprecated]           ...           3 years ago
  • 2.0.0 [deprecated]           ...           3 years ago
  • 2.0.10-beta                                ...           3 years ago
  • 2.0.9-beta                                ...           3 years ago
  • 2.0.8-beta                                ...           3 years ago
  • 2.0.6-beta                                ...           3 years ago
  • 2.0.5-beta                                ...           3 years ago
  • 2.0.4-beta                                ...           3 years ago
  • 2.0.3-beta                                ...           3 years ago
  • 2.0.2-beta                                ...           3 years ago
  • 2.0.1-beta                                ...           3 years ago
  • 2.0.0-beta                                ...           3 years ago
  • 1.4.1 [deprecated]           ...           3 years ago
  • 1.4.0 [deprecated]           ...           3 years ago
  • 1.3.2 [deprecated]           ...           3 years ago
  • 1.3.1 [deprecated]           ...           3 years ago
  • 1.3.0 [deprecated]           ...           4 years ago
  • 1.2.4 [deprecated]           ...           4 years ago
  • 1.2.3 [deprecated]           ...           4 years ago
  • 1.2.2 [deprecated]           ...           4 years ago
  • 1.2.1 [deprecated]           ...           4 years ago
  • 1.2.0 [deprecated]           ...           4 years ago
  • 1.1.2 [deprecated]           ...           4 years ago
  • 1.1.1 [deprecated]           ...           4 years ago
  • 1.1.0 [deprecated]           ...           4 years ago
  • 1.0.2 [deprecated]           ...           4 years ago
  • 1.0.1 [deprecated]           ...           4 years ago
  • 1.0.0 [deprecated]           ...           4 years ago
  • 1.0.0-beta4                                ...           4 years ago
  • 1.0.0-beta3                                ...           4 years ago
  • 1.0.0-beta2                                ...           4 years ago
  • 1.0.0-beta1                                ...           4 years ago
  • 0.9.1 [deprecated]           ...           4 years ago
  • 0.9.0 [deprecated]           ...           4 years ago
  • 0.8.8 [deprecated]           ...           4 years ago
  • 0.8.7 [deprecated]           ...           4 years ago
  • 0.8.6 [deprecated]           ...           4 years ago
  • 0.8.5 [deprecated]           ...           4 years ago
  • 0.8.4 [deprecated]           ...           4 years ago
  • 0.8.3 [deprecated]           ...           4 years ago
  • 0.8.2 [deprecated]           ...           4 years ago
  • 0.8.1 [deprecated]           ...           4 years ago
  • 0.8.0 [deprecated]           ...           4 years ago
  • 0.7.1 [deprecated]           ...           4 years ago
  • 0.7.0 [deprecated]           ...           4 years ago
  • 0.6.1 [deprecated]           ...           4 years ago
  • 0.6.0 [deprecated]           ...           4 years ago
  • 0.5.0 [deprecated]           ...           4 years ago
  • 0.4.2 [deprecated]           ...           4 years ago
  • 0.4.1 [deprecated]           ...           4 years ago
  • 0.4.0 [deprecated]           ...           4 years ago
  • 0.3.4 [deprecated]           ...           4 years ago
  • 0.3.3 [deprecated]           ...           4 years ago
  • 0.3.1 [deprecated]           ...           4 years ago
  • 0.3.0 [deprecated]           ...           4 years ago
  • 0.2.2 [deprecated]           ...           4 years ago
  • 0.2.1 [deprecated]           ...           4 years ago
  • 0.2.0 [deprecated]           ...           4 years ago
  • 0.1.11 [deprecated]           ...           4 years ago
  • 0.1.10 [deprecated]           ...           4 years ago
  • 0.1.7 [deprecated]           ...           4 years ago
  • 0.1.6 [deprecated]           ...           4 years ago
  • 0.1.5 [deprecated]           ...           4 years ago
  • 0.1.4 [deprecated]           ...           4 years ago
  • 0.1.3 [deprecated]           ...           4 years ago
  • 0.1.2 [deprecated]           ...           4 years ago
  • 0.1.1 [deprecated]           ...           4 years ago
  • 0.1.0 [deprecated]           ...           4 years ago
  • 0.0.4 [deprecated]           ...           4 years ago
  • 0.0.3 [deprecated]           ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 6
This Month 6
Last Day 3
Last Week 101
Last Month 718
Dependencies (21)
Dev Dependencies (13)

Copyright 2014 - 2017 © taobao.org |