Giter Club home page Giter Club logo

tms-koa's Introduction

tms-koa

基于koa的轻量级快速开发框架,包含 MVC 中的 M 和 C 两部分,适合于实现 API 服务和前后端彻底分离的应用。

内置基于 access_token 的访问鉴权机制,更容易实现 API 调用的负载分担。

内置通过连接池访问 MySQL 数据库,支持进行读写分离。内置 SQL 语句的封装,内置 escape 防止 sql 注入。目前 where 条件中,exists,and,or 形成的 sql 不会进行 escape 处理,需要在外部自行处理。select 中的 fields 和 table 未进行 escape 处理,不能够直接使用用户输入的内容作为字段名和表名。orderby 和 groupby 未做 escape 处理,不能够直接使用用户输入。

安装

npm install tms-koa --save

为了运行系统需要安装 MySQL 和 Redis。

配置信息

在项目的根目录下建立文件/config/app.js,指定下列信息:

module.exports = {
  port: 3000,
  name: "tms-koa-0",
  router: {
    auth: {
      prefix: "" // 接口调用url的前缀
    },
    controllers: {
      prefix: "" // 接口调用url的前缀
    }
  },
  tmsTransaction: false
}

参考:https://www.npmjs.com/package/koa-router

在项目的根目录下建立文件/config/redis.js,指定下列 Redis 连接信息:

module.exports = {
  host: "127.0.0.1",
  port: "6379"
}

https://www.npmjs.com/package/redis

在项目的根目录下建立文件/config/db.js,指定下列 MySQL 数据库连接信息:

module.exports = {
  master: {
    connectionLimit: 10,
    host: "",
    port: "",
    user: "",
    password: "",
    database: ""
  },
  write: {
    connectionLimit: 10,
    host: "",
    port: "",
    user: "",
    password: "",
    database: ""
  }
}

参考:https://www.npmjs.com/package/mysql

鉴权机制

在项目的根目录下建立文件/auth/client.js,实现一个根据 http 请求 返回Clinet对象的方法。

通过调用/auth/token获得access_token,它的值和client.js返回的对象存在一一对应的关系。

获得的access_token会存储在 Redis 中,有效期是7200秒。格式为应用名称(app.js 中的 name),内容名AccessTokentoken字符串用户id字符串(来源于 client.js 中指定的 id),中间用:分隔。

tms-koa-0:AccessToken:c89d35281105456babd15d94831424c7:userid

利用这个机制可以用tms-koa实现一个基于 token 的 api 鉴权中心。

控制器(API)

项目根目录下创建controllers目录,路径和 url 匹配

需要从 Ctrl 类继承。

const { Ctrl, ResultData } = require("tms-koa")

class Main extends Ctrl {
  tmsRequireTransaction() {
    return {
      get: true
    }
  }
  get() {
    return new ResultData("I am an api.")
  }
}
module.exports = Main

模型(model)

项目根目录下创建models目录。

模型必须从 DbModel 继承。

必须在导出包中提供一个用户创建实例的create方法。DbModel类中已经内置一个创建实例的方法的create方法,它的子类可参照下面的例子进行调用。

const { DbModel } = require("tms-koa")

class Template extends DbModel {
  constructor({ db = null, debug = false } = {}) {
    super("template", { db, debug })
  }
}

module.exports = { Template, create: Template.create.bind(Template) }

已经在 model 层中进行 escape 处理,防止 sql 注入。关于 escape 请参考:tests/lib/model/escape.spec.js。

静态文件

项目根目录下创建public目录。

记录控制器事物

在连接的数据库中执行下面的脚本。

CREATE TABLE `tms_transaction` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `begin_at` double(13,3) NOT NULL,
 `end_at` double(13,3) NOT NULL DEFAULT '0.000',
 `request_uri` text,
 `user_agent` text,
 `referer` text,
 `remote_addr` text,
 `userid` varchar(40) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

app.js文件中将tmsTransaction设置为true

在控制器类(Ctrl)中添加方法,说明需要支持事物的接口。

tmsRequireTransaction() {
    return {
        get: true
    }
}

在控制器类(Ctrl)中添加方法,说明需要在调用接口前执行的代码。

async tmsBeforeEach(method) {
  // 返回ResultFault及其子类的对象,终止接口调用
  // return new ResultFault('发生错误')

  return true
}

tms-koa's People

Contributors

jasony62 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.