1.0.4 • Published 2 years ago

@creditkarma/thrift-server-hapi v1.0.4

Weekly downloads
277
License
Apache-2.0
Repository
github
Last release
2 years 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

2 years ago

1.0.6-alpha.0

2 years ago

0.18.1-alpha.8

3 years ago

0.18.1-alpha.7

3 years ago

0.18.1-alpha.6

3 years ago

0.18.1-alpha.9

3 years ago

1.0.4

2 years ago

1.0.3

2 years ago

0.18.1-alpha.5

3 years ago

0.18.0-alpha.0

3 years ago

0.18.0-alpha.1

3 years ago

0.18.0-alpha.5

3 years ago

0.18.1-alpha.0

3 years ago

0.18.1-alpha.4

3 years ago

0.18.1-alpha.3

3 years ago

0.17.2

4 years ago

0.17.1

4 years ago

0.17.0-alpha.0

4 years ago

0.16.1

5 years ago

0.16.0

5 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

6 years ago

0.13.6

6 years ago

0.13.5

6 years ago

0.13.4

6 years ago

0.13.3

6 years ago

0.13.2

6 years ago

0.6.35

6 years ago

0.12.5

6 years ago

0.12.4

6 years ago

0.12.3

6 years ago

0.12.3-alpha.0

6 years ago

0.12.2

6 years ago

0.12.1

6 years ago

0.6.34

6 years ago

0.11.2

6 years ago

0.11.1

6 years ago

0.6.33

6 years ago

0.6.32

6 years ago

0.11.0-alpha.2

6 years ago

0.11.0-alpha.1

6 years ago

0.11.0-alpha.0

6 years ago

0.8.5

6 years ago

0.10.4

6 years ago

0.10.3

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.3

6 years ago

0.8.4

6 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

7 years ago

0.7.0

7 years ago

0.6.19

7 years ago

0.7.0-4

7 years ago

0.6.18

7 years ago

0.6.17

7 years ago

0.6.16

7 years ago

0.6.15

7 years ago

0.6.14

7 years ago

0.6.14-1

7 years ago

0.6.14-0

7 years ago

0.6.13

7 years ago

0.6.13-2

7 years ago

0.6.13-1

7 years ago

0.6.13-0

7 years ago

0.6.12

7 years ago

0.6.11

7 years ago

0.6.10

7 years ago

0.7.0-3

7 years ago

0.6.9

7 years ago

0.6.8

7 years ago

0.6.7

7 years ago

0.7.0-2

7 years ago

0.7.0-1

7 years ago

0.7.0-0

7 years ago

0.4.5

7 years ago

0.6.6

7 years ago

0.6.6-1

7 years ago

0.6.6-0

7 years ago

0.6.5

7 years ago

0.6.5-2

7 years ago

0.6.4

7 years ago

0.6.3

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.5.0-1

7 years ago

0.5.0-0

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.3

7 years ago

0.2.0

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.0.37

7 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