@khgame/turtle
An enterprise level javascript/typescript server framework.
Last updated 7 months ago by kinghand .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @khgame/turtle 
SYNC missed versions from official npm registry.

Turtle

Features

  • plugin system
  • zero-configuration service discovery / rpc
  • distributed lock
  • progressive development
  • graceful exit
  • command line interface

usage

install

npm i --save @khgame/turtle or using yarn yarn add @khgame/turtle

quick start

import {turtle, ITurtleRedis, RedisDriver} from "@khgame/turtle"

turtle.setConf({
                 "name": "server_name",
                 "id": 0,
                 "port": 11821,
                 "drivers": {
                   "mongo": {
                     "host": "127.0.0.1",
                     "port": 27017,
                     "database": "khgame_nft_svr",
                     "username": "",
                     "password": ""
                   },
                   "redis": {
                     "db": 0,
                     "family": 4,
                     "host": "127.0.0.1",
                     "port": 6379,
                     "keyPrefix": "KH_NFTServ_default_redisKey:",
                     "key_mutex_wait_threshold": 100
                   },
                   "discover/consul": {
                         "health": {
                           "api": "api/health"
                         }
                   },
                   CUSTOM_DRIVER_CLASS
                 },
                 "rules": {}
               });
// console.log(turtle.conf) // by this you can access the config

turtle.initialDrivers([ "redis", "mongo", "discover/consul" ]); // using built in drivers
// to use redis driver, redisio should be installed
// to use mongo driver, mongoose should be installed
// to use discover/consul driver, consul should be installed

//...
await RedisDriver.inst.redis.set("key", "val"); // or you can use turtle.drivers
await turtle.drivers("redis").get("key");
//RedisDriver.inst
//...

using drivers

mongo

to use mongo driver, mongoose should be installed

interface IMongoConf {
    host: string;
    port?: string | number;
    database: string;
    username?: string;
    password?: string;
}

redis

to use redis driver, redisio should be installed

interface IRedisConf extends IORedis.RedisOptions {
    key_mutex_wait_threshold?: number;
}

discover/consul

to use discover/consul driver, consul should be installed

interface IHealth {
    api?: string;
    script?: string;
    interval?: string;
    ttl?: string;
    notes?: string;
    status?: string;
}

interface IConsulConf {
    optional?: boolean; // default false
    options?: {
        host?: string; // (String, default: 127.0.0.1): agent address
        port?: number; //  (Integer, default: 8500): agent HTTP(S) port
        secure?: boolean; // (Boolean, default: false): enable HTTPS
        ca?: string[];
    };
    health: IHealth | IHealth[];
    dc?: string;
    tags?: string[];
}
did

create api

once you implemented an api, you can manage it's lifecycle like this

    /** 1. set config to turtle */
    turtle.setConf(/** ... */, false);
    /** 2. create the api instance (or instances) */
    const api = new ApiClass();
    /** 3. put the api instance to turtle.startAll */
    await turtle.startAll([api]);

this is an example and you can clone the repo, and run npm run ep:api to test it by your self

starting with cli application

you can easily create your turtle cli application with several definitions

// bin/index.ts
const cli = new CommandLineApp("example", "0.0.1", [], [() => new ApiClass()], {
        "name": "example",
        "id": 0,
        "port": 8080,
        "drivers": {
        },
        "rules": {
        }
    }
);

cli.run();

this is an example and you can clone the repo, and run npm run ep:cli --help to test it by your self

Using Cli

Install

npm i -g @khgame/turtle

Create new project

turtle init

  • When command init are executed, questions of the initiation of a new project will be asked one by one. To create a new turtle project, you just need answer these questions. All question are optional, you can just skip all question to create a default project. For some questions, you may need some more information to decide the answer of it, such as what drivers should be added, or what templates should be used. The related information are provided below.
  • drivers: you can select drivers when using init command, both code and config will be generate
  • template: you can select official template or custom template here. to using custom template, just input the git address to clone
    • official templates: web

Show all turtle in directory

turtle ls [dir_name]

  • options:

    option alias need args desc
    --info -i false show all runtime info of the turtle
    --process -p false show pid and alive-status of the turtle process

Restart

turtle restart <turtle_name|turtle_file_name|pid>

  • if the input are not given, all turtles in current directory and their PIDs will be printed.

  • to reset the turtles ENV, you can set environments before the command directly e.p. NODE_ENV=production turtle restart ...

  • options:

    option alias need args desc
    --follow -f false restart process and tail the stdout file

Stop

turtle stop <turtle_name|turtle_file_name|pid>

  • idem

Log

turtle log

  • By default, the log command will only show turtles' stdout files in the executing dir

  • You can using option -p to print a logfile for -f to follow a logfile. If these options are detected, a question will show up after the logFiles' list. Hence you just need to select a log file to print or tail by their sequence printed.

  • Options:

    option alias need args desc
    --print -p false print the stdout file
    --follow -f false tail the stdout file

    When sequence -1 are specified will select the latest log file.

    Therefore, you can using pipe to print contents of the latest log file: (echo -1) | turtle log -p.

    And you can remove 0-size log with command turtle log | grep "0.00 kb" | awk '{print substr($0, 8)}' | awk '{print $3}' | xargs -I {} rm {}

Current Tags

  • 0.0.100                                ...           latest (7 months ago)

98 Versions

  • 0.0.100                                ...           7 months ago
  • 0.0.99                                ...           7 months ago
  • 0.0.98                                ...           7 months ago
  • 0.0.97                                ...           9 months ago
  • 0.0.96                                ...           9 months ago
  • 0.0.95                                ...           9 months ago
  • 0.0.94                                ...           9 months ago
  • 0.0.93                                ...           9 months ago
  • 0.0.92                                ...           9 months ago
  • 0.0.91                                ...           9 months ago
  • 0.0.90                                ...           9 months ago
  • 0.0.89                                ...           9 months ago
  • 0.0.88                                ...           9 months ago
  • 0.0.87                                ...           9 months ago
  • 0.0.86                                ...           9 months ago
  • 0.0.85                                ...           9 months ago
  • 0.0.84                                ...           9 months ago
  • 0.0.83                                ...           9 months ago
  • 0.0.82                                ...           9 months ago
  • 0.0.81                                ...           9 months ago
  • 0.0.80                                ...           9 months ago
  • 0.0.79                                ...           9 months ago
  • 0.0.78                                ...           9 months ago
  • 0.0.77                                ...           9 months ago
  • 0.0.76                                ...           9 months ago
  • 0.0.75                                ...           9 months ago
  • 0.0.74                                ...           9 months ago
  • 0.0.73                                ...           9 months ago
  • 0.0.71                                ...           10 months ago
  • 0.0.70                                ...           10 months ago
  • 0.0.69                                ...           10 months ago
  • 0.0.68                                ...           10 months ago
  • 0.0.67                                ...           10 months ago
  • 0.0.66                                ...           10 months ago
  • 0.0.65                                ...           10 months ago
  • 0.0.64                                ...           10 months ago
  • 0.0.63                                ...           10 months ago
  • 0.0.62                                ...           10 months ago
  • 0.0.61                                ...           10 months ago
  • 0.0.60                                ...           10 months ago
  • 0.0.59                                ...           10 months ago
  • 0.0.58                                ...           10 months ago
  • 0.0.57                                ...           10 months ago
  • 0.0.56                                ...           10 months ago
  • 0.0.55                                ...           10 months ago
  • 0.0.54                                ...           10 months ago
  • 0.0.53                                ...           10 months ago
  • 0.0.52                                ...           10 months ago
  • 0.0.51                                ...           10 months ago
  • 0.0.50                                ...           10 months ago
  • 0.0.49                                ...           10 months ago
  • 0.0.48                                ...           a year ago
  • 0.0.47                                ...           a year ago
  • 0.0.45                                ...           a year ago
  • 0.0.44                                ...           a year ago
  • 0.0.43                                ...           a year ago
  • 0.0.42                                ...           a year ago
  • 0.0.41                                ...           a year ago
  • 0.0.40                                ...           a year ago
  • 0.0.39                                ...           a year ago
  • 0.0.38                                ...           a year ago
  • 0.0.37                                ...           a year ago
  • 0.0.36                                ...           a year ago
  • 0.0.35                                ...           a year ago
  • 0.0.34                                ...           a year ago
  • 0.0.33                                ...           a year ago
  • 0.0.32                                ...           a year ago
  • 0.0.31                                ...           a year ago
  • 0.0.30                                ...           a year ago
  • 0.0.29                                ...           a year ago
  • 0.0.28                                ...           a year ago
  • 0.0.27                                ...           a year ago
  • 0.0.26                                ...           a year ago
  • 0.0.25                                ...           a year ago
  • 0.0.24                                ...           a year ago
  • 0.0.23                                ...           a year ago
  • 0.0.22                                ...           a year ago
  • 0.0.21                                ...           a year ago
  • 0.0.20                                ...           a year ago
  • 0.0.19                                ...           a year ago
  • 0.0.18                                ...           a year ago
  • 0.0.17                                ...           a year ago
  • 0.0.16                                ...           a year ago
  • 0.0.15                                ...           a year ago
  • 0.0.14                                ...           a year ago
  • 0.0.13                                ...           a year ago
  • 0.0.12                                ...           a year ago
  • 0.0.11                                ...           a year ago
  • 0.0.10                                ...           a year ago
  • 0.0.9                                ...           a year ago
  • 0.0.8                                ...           a year ago
  • 0.0.7                                ...           a year ago
  • 0.0.6                                ...           a year ago
  • 0.0.5                                ...           a year ago
  • 0.0.4                                ...           a year ago
  • 0.0.3                                ...           a year ago
  • 0.0.2                                ...           a year ago
  • 0.0.1                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (19)
Dev Dependencies (25)

Copyright 2014 - 2016 © taobao.org |