1.0.4 • Published 11 months ago

@creditkarma/thrift-server-hapi v1.0.4

Weekly downloads
277
License
Apache-2.0
Repository
github
Last release
11 months ago

Thrift Server Hapi

Hapi plugin for processing Thrift requests.

Usage

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 hapi
npm install --save thrift
npm install --save @creditkarma/thrift-server-hapi

Register

To get things working you need to register the ThriftPlugin and define handlers for your service methods.

The ThriftPlugin create a Hapi route at the given path on which to serve this Thrift service.

import * as Hapi from 'hapi'
import { ThriftPlugin } from '@creditkarma/thrift-server-hapi'
import { UserService } from './codegen/user_service'

const PORT: number = 8080

const server = new Hapi.Server({ debug: { request: [ 'error' ] } })

server.connection({ port: PORT })

/**
 * Implementation of our Thrift service.
 *
 * Notice the second parameter, "context" - this is the Hapi request object,
 * passed along to our service by the Hapi Thrift plugin. Thus, you have access to
 * all HTTP request data from within your service implementation.
 */
const handlers: UserService.IHandler<Hapi.Request> = {
    getUser(request: RecommendationsRequest, context?: Hapi.Request) {
        const userId = request.userId

        if (userId.toNumber() <= 0) {
            throw new Error('User ID must be greater than zero')
        }

        return getUserForId(userId)
    },
}

const processor: UserService.Processor<Hapi.Request> = new UserService.Processor(handlers)

/**
 * Register the Thrift plugin.
 *
 * This will allow us to define Hapi routes for our Thrift service(s).
 * They behave like any other HTTP route handler, so you can mix and match
 * Thrift / REST endpoints on the same server instance.
 *
 * This plugin adds a route to your server for handling Thrift requests. The path
 * option is the path to attache the route handler to and the handler is the
 * Thrift service processor instance.
 */
server.register(ThriftPlugin<UserService.Processor>({
    serviceName: 'user-service',
    path: '/thrift',
    handler: processor,
}), err => {
    if (err) {
        throw err
    }
})

/**
 * Start your hapi server
 */
server.start((err) => {
    if (err) {
        throw err
    }
    server.log('info', `Server running 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.
  • path - The path on which to server your Thrift service. Defaults to '/'.
  • 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 a Hapi server only to serve Thrift, you can use the createThriftServer factory function to create a Hapi.Server and register the ThriftPlugin in one step.

The factory function takes all of the same configuration options as the plugin with the addition of port. What port do you want your server to run on?

import * as Hapi from 'hapi'
import { createThriftServer } from '@creditkarma/thrift-server-hapi'
import { UserService } from './codegen/user_service'

const PORT: number = 8080

const server: Hapi.Server = createThriftServer<UserService.Processor>({
    serviceName: 'user-service',
    path: '/thrift',
    port: PORT,
    handler: new UserService.Processor({
        getUser(request: RecommendationsRequest, context?: Hapi.Request) {
            const userId = request.userId

            if (userId.toNumber() <= 0) {
                throw new Error('User ID must be greater than zero')
            }

            return getUserForId(userId)
        },
    })
})

/**
 * Start your hapi server
 */
server.start((err) => {
    if (err) {
        throw err
    }
    server.log('info', `Server running 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

11 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

5 years ago

0.15.2

5 years ago

0.15.1

5 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

6 years ago

0.6.31

6 years ago

0.9.2

6 years ago

0.10.0-alpha.2

6 years ago

0.10.0-alpha.1

6 years ago

0.9.1

6 years ago

0.9.0-alpha.10

6 years ago

0.9.0-alpha.9

6 years ago

0.9.0-alpha.8

6 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

7 years ago

0.0.35

7 years ago

0.0.34

7 years ago

0.0.32

7 years ago

0.0.31

7 years ago

0.0.30

7 years ago

0.0.28

7 years ago

0.0.27

7 years ago

0.0.24

7 years ago

0.0.23

7 years ago

0.0.22

7 years ago

0.0.21

7 years ago

0.0.20

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.8

7 years ago

0.0.1

7 years ago