1.0.4 • Published 10 months ago

@creditkarma/thrift-server-express v1.0.4

Weekly downloads
1,516
License
Apache-2.0
Repository
github
Last release
10 months ago

Thrift Server Express

Express middleware for processing Thrift requests.

Usage

Codegen

Requires @creditkarma/thrift-typescript >= v1.0.0

The easiest way to get started is to generate your thrift services using @creditkarma/thrift-typescript.

npm install --save-dev @creditkarma/thrift-typescript

Add a script to your package.json to codegen. The 'target' option is important to make thrift-typescript generate for this library instead of the Apache libraries.

"scripts": {
  "codegen": "thrift-typescript --target thrift-server --sourceDir thrift --outDir codegen
}

Example Service

service Calculator {
  i32 add(1: i32 left, 2: i32 right)
  i32 subtract(1: i32 left, 2: i32 right)
}

Install

npm install --save express
npm install --save thrift
npm install --save @creditkarma/thrift-server-express

Use the Plugin

To get things working you need to register the middleware as you would any other express middleware. An important thing to note is the use of bodyParse.raw(). This is required, Thrift must operate on the raw binary Buffers.

import * as bodyParser from 'body-parser'
import * as express from 'express'
import { thriftExpress } from '../main'

import {
  Calculator,
} from './codegen/calculator'

const PORT = 8080

const app = express()

/**
 * Implementation of our thrift service.
 *
 * Notice the optional final parameter, "context" - this is the Express request object,
 * passed along to our service by the Thrift Express middleware. Thus, you have access to
 * all HTTP request data from within your service implementation.
 */
const serviceHandlers: Calculator.IHandler<express.Request> = {
    add(left: number, right: number, context?: express.Request): number {
        return left + right
    },
    subtract(left: number, right: number, context?: express.Request): number {
        return left - right
    },
}

app.use(
    '/thrift',
    bodyParser.raw(),
    thriftExpress<Calculator.Processor>({
        serviceName: 'calculator-service',
        handler: new Calculator.Processor(serviceHandlers),
    }),
)

app.get('/control', (req: express.Request, res: express.Response) => {
  res.send('PASS')
})

app.listen(PORT, () => {
  console.log(`Express server listening on port: ${PORT}`)
})

Options

  • serviceName - The name of your service. Used for logging and tracing.
  • handler - The service Processor instance to handle service method calls.
  • transport - The kind of Thrift transport to use. Only 'buffered' is currently supported.
  • protocol - The kind of Thrift protocol to use. Only 'binary' is currently supported.

Thrift Server Factory

In the event that you will be creating an Express server only to serve Thrift, you can use the createThriftServer factory function to create a Express.Application and register the thriftExpress middleware in one step.

The factory function takes all the same configuration options as the middleware.

import * as express from 'express'
import { createThriftServer } from '@creditkarma/thrift-server-hapi'
import { Calculator } from './codegen/calculator'

const PORT = 8080

const app: express.Application = createThriftServer<Calculator.Processor>({
    serviceName: 'calculator-service',
    path: '/thrift',
    handler: new Calculator.Processor({
        add(left: number, right: number, context?: express.Request): number {
            return left + right
        },
        subtract(left: number, right: number, context?: express.Request): number {
            return left - right
        },
    })
})

app.listen(PORT, () => {
  console.log(`Express server listening on port: ${PORT}`)
})

Contributing

For more information about contributing new features and bug fixes, see our Contribution Guidelines. External contributors must sign Contributor License Agreement (CLA)

License

This project is licensed under Apache License Version 2.0

1.1.0-alpha.1

10 months ago

1.0.6-alpha.0

1 year ago

0.18.1-alpha.8

2 years ago

0.18.1-alpha.7

2 years ago

0.18.1-alpha.6

2 years ago

0.18.1-alpha.9

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

0.18.1-alpha.5

2 years ago

0.18.0-alpha.0

2 years ago

0.18.0-alpha.1

2 years ago

0.18.0-alpha.5

2 years ago

0.18.1-alpha.0

2 years ago

0.18.1-alpha.4

2 years ago

0.18.1-alpha.3

2 years ago

0.17.2

3 years ago

0.17.1

3 years ago

0.17.0-alpha.0

3 years ago

0.16.1

4 years ago

0.16.0

4 years ago

0.15.3

4 years ago

0.15.2

4 years ago

0.15.1

4 years ago

0.14.6

5 years ago

0.14.5

5 years ago

0.14.4

5 years ago

0.14.3

5 years ago

0.14.2

5 years ago

0.14.1

5 years ago

0.13.8

5 years ago

0.13.7

5 years ago

0.13.6

5 years ago

0.13.5

5 years ago

0.13.4

5 years ago

0.13.3

5 years ago

0.13.2

5 years ago

0.6.35

5 years ago

0.12.5

5 years ago

0.12.4

5 years ago

0.12.3

5 years ago

0.12.3-alpha.0

5 years ago

0.12.2

5 years ago

0.12.1

5 years ago

0.6.34

5 years ago

0.11.2

5 years ago

0.11.1

5 years ago

0.6.33

5 years ago

0.6.32

5 years ago

0.11.0-alpha.2

5 years ago

0.11.0-alpha.1

5 years ago

0.11.0-alpha.0

5 years ago

0.8.5

5 years ago

0.10.4

5 years ago

0.10.3

5 years ago

0.10.2

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.3

5 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.6.31

5 years ago

0.9.2

5 years ago

0.10.0-alpha.2

5 years ago

0.10.0-alpha.1

5 years ago

0.9.1

5 years ago

0.9.0-alpha.10

5 years ago

0.9.0-alpha.9

5 years ago

0.9.0-alpha.8

5 years ago

0.6.30

6 years ago

0.9.0-alpha.7

6 years ago

0.9.0-alpha.6

6 years ago

0.6.29

6 years ago

0.6.28

6 years ago

0.6.27

6 years ago

0.9.0-alpha.5

6 years ago

0.9.0-alpha.4

6 years ago

0.9.0-alpha.3

6 years ago

0.6.25

6 years ago

0.6.24

6 years ago

0.8.3-alpha.4

6 years ago

0.8.3-alpha.3

6 years ago

0.8.3-alpha.2

6 years ago

0.8.3-alpha.1

6 years ago

0.8.3-alpha.0

6 years ago

0.6.23

6 years ago

0.6.22

6 years ago

0.9.0-alpha.2

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0-1

6 years ago

0.7.3

6 years ago

0.6.21

6 years ago

0.7.0

6 years ago

0.6.19

6 years ago

0.7.0-4

6 years ago

0.6.18

6 years ago

0.6.17

6 years ago

0.6.16

6 years ago

0.6.15

6 years ago

0.6.14

6 years ago

0.6.14-1

6 years ago

0.6.14-0

6 years ago

0.6.13

6 years ago

0.6.13-2

6 years ago

0.6.13-1

6 years ago

0.6.13-0

6 years ago

0.6.12

6 years ago

0.6.11

6 years ago

0.6.10

6 years ago

0.7.0-3

6 years ago

0.6.9

6 years ago

0.6.8

6 years ago

0.6.7

6 years ago

0.7.0-2

6 years ago

0.7.0-1

6 years ago

0.7.0-0

6 years ago

0.4.5

6 years ago

0.6.6

6 years ago

0.6.6-1

6 years ago

0.6.6-0

6 years ago

0.6.5

6 years ago

0.6.5-2

6 years ago

0.6.4

6 years ago

0.6.3

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.5.0-1

6 years ago

0.5.0-0

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.3

6 years ago

0.2.0

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.0.37

6 years ago

0.0.36

6 years ago

0.0.35

6 years ago

0.0.34

6 years ago

0.0.32

6 years ago

0.0.31

6 years ago

0.0.30

6 years ago

0.0.28

6 years ago

0.0.27

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago