Skip to content

Lakutata Entrypoint ​

The entrypoint component connects controllers to transports. It holds the controllers and the transport adapters (HTTP, command line, service calls, schedules, queues…): each adapter receives its transport's requests, and the entrypoint runs the matching action, with its middlewares, validation and access control.

1. Declaring the entrypoint ​

The lakutata package already declares a component named entrypoint. An application specifies the component's options with buildEntrypoints, under the same name, and lists it in bootstrap:

typescript
import {Application} from 'lakutata'
import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
import {GET} from 'lakutata/decorator/ctrl'
import {expressHTTPEntrypoint} from 'lakutata/adapter/express'

class HealthController extends Controller {
    @GET('/health')
    public async health(): Promise<string> {
        return 'ok'
    }
}

Application.run(() => ({
    id: 'api.app',
    name: 'API',
    components: {
        entrypoint: buildEntrypoints({
            controllers: [HealthController],
            http: expressHTTPEntrypoint({port: 8080})
        })
    },
    bootstrap: ['entrypoint']
}))

A component is created the first time something needs it. No other object needs the entrypoint, so without bootstrap: ['entrypoint'] it is never created: no server listens, no command runs, no job starts.

2. Options ​

OptionDefaultMeaning
controllers[]The controller classes
http, cli, service, schedule, queue, mcp, dbusThe transport adapters (below): one adapter, or an array of adapters
middlewares[]Middlewares for every action (see Middlewares and Access Control)
rules[]Access rules for the actions declared with acl: true (see Middlewares and Access Control)
shutdownTimeout30000The time, in milliseconds, allowed for requests in progress when the application stops (section 4)
httpActionGroups, cliActionGroups, serviceActionGroups{}The groups the actions may use, with their names (section 5)

The adapters:

OptionAdapterImported fromTo installGuide
httpexpressHTTPEntrypoint()lakutata/adapter/expressexpressHTTP
httpfastifyHTTPEntrypoint()lakutata/adapter/fastifyfastifyHTTP
clicommanderCLIEntrypoint()lakutata/adapter/commandercommanderCommand Line
servicehttpServiceEntrypoint()lakutata/com/entrypointService Calls
servicesocketIOServiceEntrypoint()lakutata/adapter/socket.iosocket.ioService Calls
servicejsonRPCServiceEntrypoint()lakutata/adapter/jsonrpcws, for WebSocketService Calls
serviceelectronIPCServiceEntrypoint()lakutata/adapter/electron(Electron)Service Calls
servicenwjsServiceEntrypoint()lakutata/adapter/nwjs(NW.js)Service Calls
servicereactNativeServiceEntrypoint()lakutata/adapter/react-nativeService Calls
servicegrpcServiceEntrypoint()@lakutata/grpc/service@lakutata/grpcgRPC
servicenatsServiceEntrypoint()@lakutata/nats@lakutata/natsNATS
schedulescheduleEntrypoint()lakutata/com/entrypointScheduled Jobs
queuequeueEntrypoint()lakutata/com/queueThe client for the queue serverQueues
mcphttpMCPEntrypoint(), stdioMCPEntrypoint()@lakutata/mcp@lakutata/mcpMCP
dbusdbusEntrypoint()@lakutata/dbus@lakutata/dbusD-Bus

3. Several adapters ​

Each option accepts an array, to serve one transport in several ways. For example, service actions over Socket.IO for browsers and over JSON-RPC for scripts, or HTTP actions on two ports:

typescript
import {buildEntrypoints} from 'lakutata/com/entrypoint'
import {expressHTTPEntrypoint} from 'lakutata/adapter/express'
import {jsonRPCServiceEntrypoint} from 'lakutata/adapter/jsonrpc'
import {socketIOServiceEntrypoint} from 'lakutata/adapter/socket.io'

export const entrypoint = buildEntrypoints({
    controllers: [],
    http: [
        expressHTTPEntrypoint({name: 'public', port: 8080}),
        expressHTTPEntrypoint({name: 'internal', host: '127.0.0.1', port: 9090})
    ],
    service: [
        socketIOServiceEntrypoint({port: 3000}),
        jsonRPCServiceEntrypoint({port: 4000, path: '/rpc'})
    ]
})

Every adapter serves all the actions of its transport. The name option distinguishes the adapters.

4. Starting and stopping ​

When the application launches, the entrypoint first checks the controllers. The launch fails on each of these mistakes, with the exception shown:

  • two actions of the same transport with the same name (DuplicateActionNameException);
  • an action in a group that its *ActionGroups option does not declare (InvalidActionGroupException);
  • a prefix that conflicts with an action's pattern (InvalidPrefixException);
  • a middleware that is neither a function nor a Middleware class (InvalidMiddlewareException);
  • an adapter specified under another transport's key, or an option that is neither an entrypoint option nor a transport (InvalidEntrypointAdapterException).

The adapters then start one after another, in the order of the options. If one fails to start, such as a server whose port is already in use, the launch fails. The command line and scheduled jobs begin once the whole application has launched.

When the application stops (on SIGTERM or SIGINT, or after app.exit()), the adapters stop in reverse order:

  1. they stop accepting new requests. Depending on the transport, a request that still arrives is refused with an EntrypointStoppedException (status 503 over HTTP);
  2. the requests in progress are given shutdownTimeout (30 seconds by default) to finish;
  3. the requests still running are aborted: their this.signal fires, and they fail with an EntrypointStoppedException;
  4. the adapters close their servers and connections.

5. Groups and the list of the actions ​

An action's groups option puts it in groups. Access rules can use them (see Middlewares and Access Control), and the entrypoint can list actions by group, to build an API index or a permission list. The groups of HTTP, command-line and service actions must be declared, with their names:

typescript
import {Application} from 'lakutata'
import {buildEntrypoints, Controller, type Entrypoint} from 'lakutata/com/entrypoint'
import {DELETE, GET} from 'lakutata/decorator/ctrl'
import {Inject} from 'lakutata/decorator/di'

class UserController extends Controller {
    @Inject('entrypoint')
    protected readonly entrypoint: Entrypoint

    @GET('/users', {description: 'List the users'})
    public async list(): Promise<string[]> {
        return []
    }

    @DELETE('/users/:id', {description: 'Delete a user', groups: ['admin']})
    public async remove(): Promise<void> {
    }

    //The actions of each group: [{"group":"Administration","actions":["Delete a user"]}]
    @GET('/permissions')
    public async permissions(): Promise<{ group: string, actions: string[] }[]> {
        return this.entrypoint.getHttpActions().groups.map((group) => ({
            group: group.name,
            actions: group.actions.map((action): string => action.description)
        }))
    }
}

Application.run(() => ({
    id: 'users.app',
    name: 'Users',
    components: {
        entrypoint: buildEntrypoints({
            controllers: [UserController],
            httpActionGroups: {admin: 'Administration'}
        })
    },
    bootstrap: ['entrypoint']
}))

getHttpActions(), getCliActions() and getServiceActions() return {groups, actions}: groups holds the declared groups (id, name and their actions), actions every action of the transport. getActions(channel) lists any other transport's actions (getActions('schedule')). Each action is described by:

PropertyMeaning
idA stable ID, computed from its controller, method and address
name, description, groups, aclIts options
controller, methodThe names of its controller class and its method
route / command / patternIts address, including the prefix
jsonSchemaIts DTO's JSON Schema

6. Pushing events to the clients ​

The clients of service transports that keep them connected (Socket.IO, Electron, NW.js, the HTTP service's event stream, JSON-RPC over WebSocket or stdio, gRPC) can receive events from the application. entrypoint.notify(clientId, event, ...args) sends an event to the client whose ID an action found in this.context.clientId, and returns whether that client received it. entrypoint.broadcast(event, ...args) sends it to every connected client, and returns their number. See gRPC and Service Calls.

7. Customizing the entrypoint ​

A subclass of Entrypoint can replace the component's class: declare it with class, and specify the same options next to it.

typescript
import {AccessControl, Entrypoint} from 'lakutata/com/entrypoint'

//Logs every decision of the access control
class AuditedAccessControl extends AccessControl {
    protected async match(): Promise<boolean> {
        const allowed: boolean = await super.match()
        console.log(`${this.target.name}: ${allowed ? 'allowed' : 'denied'}`)
        return allowed
    }
}

export class AuditedEntrypoint extends Entrypoint {
    protected readonly accessControl: typeof AccessControl = AuditedAccessControl
}

//components: {entrypoint: {class: AuditedEntrypoint, controllers: [...], http: ...}}, bootstrap: ['entrypoint']

An adapter for another transport implements the EntrypointAdapter interface from @lakutata/entrypoint/channel. The http, cli and service options still accept the Lakutata 2.x entrypoint functions (buildHTTPEntrypoint, buildCLIEntrypoint, buildServiceEntrypoint).

Released under the MIT License.