Skip to content

[WIP] Generate server implementation for pure Typescript business logic #2317

@crisidev

Description

@crisidev

napi-rs will allow us to generate an idiomatic Python implementation which is exported from Rust as a shared library.

The framework heavy-lifting will be handled completely in Rust, while the business logic will be implemented in pure Typescript.

Example

import {
    App,
    Handlers,
    GetPokemonSpeciesInput,
    GetPokemonSpeciesOutput,
    Language,
    DoNothingInput,
    DoNothingOutput,
    Socket,
    CheckHealthOutput,
    CheckHealthInput,
    GetServerStatisticsInput,
    GetServerStatisticsOutput,
} from ".";

class HandlerImpl implements Handlers {
    // TODO: implement
    async doNothing(input: DoNothingInput): Promise<DoNothingOutput> {
        return {};
    }
    // TODO: implement
    async checkHealth(input: CheckHealthInput): Promise<CheckHealthOutput> {
        return {};
    }
    // TODO: implement
    async getServerStatistics(
        input: GetServerStatisticsInput
    ): Promise<GetServerStatisticsOutput> {
        return { callsCount: 0 };
    }
    async getPokemonSpecies(
        input: GetPokemonSpeciesInput
    ): Promise<GetPokemonSpeciesOutput> {
        return {
            name: input.name,
            flavorTextEntries: [
                {
                    language: Language.English,
                    flavorText:
                        "When several of these Pokémon gather, their electricity could build and cause lightning storms.",
                },
                {
                    language: Language.Italian,
                    flavorText:
                        "Quando vari Pokémon di questo tipo si radunano, la loro energia può causare forti tempeste.",
                },
                {
                    language: Language.Spanish,
                    flavorText:
                        "Cuando varios de estos Pokémon se juntan, su energía puede causar fuertes tormentas.",
                },
                {
                    language: Language.Japanese,
                    flavorText:
                        "ほっぺたの りょうがわに ちいさい でんきぶくろを もつ。ピンチのときに ほうでんする。",
                },
            ],
        };
    }
}

// Pass the handlers to the App.
const app = new App(new HandlerImpl());
// Start the app 🤘
const numCPUs = cpus().length / 2;
const socket = new Socket("127.0.0.1", 9090);
app.start(socket);

// TODO: This part should be abstracted out and done directly in Rust.
// We could take an optional number of workers and the socket as input
// of the App.start() method.
if (cluster.isPrimary) {
    // Fork workers.
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork();
    }
}

Features

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions