Skip to content

Commit

Permalink
correct config server behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitRanque committed Oct 19, 2023
1 parent bf4e01f commit e42fb16
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
46 changes: 33 additions & 13 deletions src/configuration_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import JSONSchema, { JSONSchemaObject } from "@json-schema-tools/meta-schema";
import { Connector } from "./connector";
import { ConnectorError } from "./error";

import { ErrorResponseSchema } from "./generated";
import { ErrorResponseSchema, SchemaResponse } from "./generated";

export interface ConfigurationServerOptions {
port: number;
Expand All @@ -18,8 +18,12 @@ const errorResponses = {
501: ErrorResponseSchema,
};

export async function start_configuration_server<Configuration, State>(
connector: Connector<Configuration, State>,
export async function start_configuration_server<
RawConfiguration,
Configuration,
State
>(
connector: Connector<RawConfiguration, Configuration, State>,
options: ConfigurationServerOptions
) {
const server = Fastify({
Expand All @@ -38,7 +42,7 @@ export async function start_configuration_server<Configuration, State>(
},
async function get_schema(
_request: FastifyRequest
): Promise<Configuration> {
): Promise<RawConfiguration> {
return connector.make_empty_configuration();
}
);
Expand All @@ -56,12 +60,12 @@ export async function start_configuration_server<Configuration, State>(
},
async (
request: FastifyRequest<{
Body: Configuration;
Body: RawConfiguration;
}>
): Promise<Configuration> => {
): Promise<RawConfiguration> => {
return connector.update_configuration(
// type assetion required because Configuration is a generic parameter
request.body as Configuration
// type assertion required because Configuration is a generic parameter
request.body as RawConfiguration
);
}
);
Expand Down Expand Up @@ -91,15 +95,23 @@ export async function start_configuration_server<Configuration, State>(
},
},
async (
request: FastifyRequest<{ Body: Configuration }>
): Promise<Configuration> => {
return connector.validate_raw_configuration(
// type assetion required because Configuration is a generic parameter
request.body as Configuration
request: FastifyRequest<{ Body: RawConfiguration }>
): Promise<{ schema: SchemaResponse; resolved_configuration: string }> => {
const resolvedConfiguration = await connector.validate_raw_configuration(
// type assertion required because Configuration is a generic parameter
request.body as RawConfiguration
);
const schema = await connector.get_schema(resolvedConfiguration);

return {
schema,
resolved_configuration: encodeJSON(resolvedConfiguration),
};
}
);

server.get("/health", async () => {});

server.setErrorHandler(function (error, _request, reply) {
if (error.validation) {
reply.status(400).send({
Expand Down Expand Up @@ -130,3 +142,11 @@ export async function start_configuration_server<Configuration, State>(
process.exit(1);
}
}

function encodeJSON(payload: unknown): string {
return Buffer.from(JSON.stringify(payload), "utf8").toString("base64");
}
// unused for now, but keeping as reference in case it is needed later.
function decodeJSON<T>(payload: string): T {
return JSON.parse(Buffer.from(payload, "base64").toString("utf8"));
}
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export { Connector, start_configuration_server, start_server };
* This shoudl be the entrypoint of your connector
* @param connector An object that implements the Connector interface
*/
export function start<Configuration, State>(
connector: Connector<Configuration, State>
export function start<RawConfiguration, Configuration, State>(
connector: Connector<RawConfiguration, Configuration, State>
) {
const program = new Command();

Expand All @@ -28,8 +28,8 @@ export function start<Configuration, State>(
program.parseAsync(process.argv).catch(console.error);
}

export function get_serve_command<Configuration, State>(
connector: Connector<Configuration, State>
export function get_serve_command<RawConfiguration, Configuration, State>(
connector: Connector<RawConfiguration, Configuration, State>
) {
return new Command("serve")
.addOption(
Expand All @@ -53,9 +53,11 @@ export function get_serve_command<Configuration, State>(
});
}

export function get_serve_configuration_command<Configuration, State>(
connector: Connector<Configuration, State>
) {
export function get_serve_configuration_command<
RawConfiguration,
Configuration,
State
>(connector: Connector<RawConfiguration, Configuration, State>) {
return new Command("configuration").addCommand(
new Command("serve")
.addOption(
Expand Down
12 changes: 4 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ export async function start_server<RawConfiguration, Configuration, State>(
connector: Connector<RawConfiguration, Configuration, State>,
options: ServerOptions
) {
const configuration = await get_configuration<Configuration>(
options.configuration
const data = fs.readFileSync(options.configuration);
const rawConfiguration = JSON.parse(data.toString("utf8"));
const configuration = await connector.validate_raw_configuration(
rawConfiguration
);

const metrics = {}; // todo
Expand Down Expand Up @@ -216,9 +218,3 @@ export async function start_server<RawConfiguration, Configuration, State>(
process.exit(1);
}
}

function get_configuration<Configuration>(path: string): Configuration {
const data = fs.readFileSync(path);
const configuration = JSON.parse(data.toString());
return configuration as Configuration;
}

0 comments on commit e42fb16

Please sign in to comment.