-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add
api-transaction-pool
package (#268)
* extract api building blocks into api-common * update yml * lint * style: resolve style guide violations * fix * revert lint * implement transaction-pool api package * add abstract service provider for api server * cleanup * style: resolve style guide violations * remove unused endpoints * style: resolve style guide violations --------- Co-authored-by: oXtxNt9U <[email protected]>
- Loading branch information
Showing
33 changed files
with
486 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Providers } from "@mainsail/kernel"; | ||
import Joi from "joi"; | ||
|
||
import { preparePlugins } from "./plugins"; | ||
// import { preparePlugins } from "./plugins"; | ||
import { AbstractServer, ServerType } from "./server"; | ||
|
||
export type ServerConstructor<T extends AbstractServer> = new (...arguments_: any[]) => T; | ||
export abstract class AbstractServiceProvider<T extends AbstractServer> extends Providers.ServiceProvider { | ||
protected abstract httpIdentifier(): symbol; | ||
protected abstract httpsIdentifier(): symbol; | ||
protected abstract getServerConstructor(): ServerConstructor<T>; | ||
protected abstract getHandlers(): any | any[]; | ||
|
||
public async register(): Promise<void> { | ||
if (this.config().get("server.http.enabled")) { | ||
await this.buildServer(ServerType.Http, this.httpIdentifier()); | ||
} | ||
|
||
if (this.config().get("server.https.enabled")) { | ||
await this.buildServer(ServerType.Https, this.httpsIdentifier()); | ||
} | ||
} | ||
|
||
public async boot(): Promise<void> { | ||
if (this.config().get("server.http.enabled")) { | ||
await this.app.get<T>(this.httpIdentifier()).boot(); | ||
} | ||
|
||
if (this.config().get("server.https.enabled")) { | ||
await this.app.get<T>(this.httpsIdentifier()).boot(); | ||
} | ||
} | ||
|
||
public async dispose(): Promise<void> { | ||
if (this.config().get("server.http.enabled")) { | ||
await this.app.get<T>(this.httpIdentifier()).dispose(); | ||
} | ||
|
||
if (this.config().get("server.https.enabled")) { | ||
await this.app.get<T>(this.httpsIdentifier()).dispose(); | ||
} | ||
} | ||
|
||
public configSchema(): Joi.ObjectSchema { | ||
return Joi.object({ | ||
plugins: Joi.object({ | ||
pagination: Joi.object({ | ||
limit: Joi.number().integer().min(0).required(), | ||
}).required(), | ||
socketTimeout: Joi.number().integer().min(0).required(), | ||
trustProxy: Joi.bool().required(), | ||
whitelist: Joi.array().items(Joi.string()).required(), | ||
}).required(), | ||
|
||
server: Joi.object({ | ||
http: Joi.object({ | ||
enabled: Joi.bool().required(), | ||
host: Joi.string().required(), | ||
port: Joi.number().integer().min(1).max(65_535).required(), | ||
}).required(), | ||
https: Joi.object({ | ||
enabled: Joi.bool().required(), | ||
host: Joi.string().required(), | ||
port: Joi.number().integer().min(1).max(65_535).required(), | ||
tls: Joi.object({ | ||
cert: Joi.string().when("...enabled", { is: true, then: Joi.required() }), | ||
key: Joi.string().when("...enabled", { is: true, then: Joi.required() }), | ||
}).required(), | ||
}).required(), | ||
}).required(), | ||
}).unknown(true); | ||
} | ||
|
||
protected async buildServer(type: ServerType, id: symbol): Promise<void> { | ||
this.app.bind<T>(id).to(this.getServerConstructor()).inSingletonScope(); | ||
|
||
const server = this.app.get<T>(id); | ||
|
||
await server.initialize(type, { | ||
...this.config().get(`server.${type.toLowerCase()}`), | ||
|
||
routes: { | ||
cors: true, | ||
}, | ||
}); | ||
|
||
await server.register(preparePlugins(this.config().get("plugins"))); | ||
|
||
await server.register({ | ||
plugin: this.getHandlers(), | ||
routes: { prefix: "/api" }, | ||
}); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,58 @@ | ||
import { Providers } from "@mainsail/kernel"; | ||
import { AbstractServiceProvider, ServerConstructor } from "@mainsail/api-common"; | ||
import Joi from "joi"; | ||
|
||
import Handlers from "./handlers"; | ||
import { Identifiers as ApiIdentifiers } from "./identifiers"; | ||
import { preparePlugins } from "./plugins"; | ||
import { Server } from "./server"; | ||
|
||
export class ServiceProvider extends Providers.ServiceProvider { | ||
public async register(): Promise<void> { | ||
if (this.config().get("server.http.enabled")) { | ||
await this.buildServer("http", ApiIdentifiers.HTTP); | ||
} | ||
|
||
if (this.config().get("server.https.enabled")) { | ||
await this.buildServer("https", ApiIdentifiers.HTTPS); | ||
} | ||
export class ServiceProvider extends AbstractServiceProvider<Server> { | ||
protected httpIdentifier(): symbol { | ||
return ApiIdentifiers.HTTP; | ||
} | ||
|
||
public async boot(): Promise<void> { | ||
if (this.config().get("server.http.enabled")) { | ||
await this.app.get<Server>(ApiIdentifiers.HTTP).boot(); | ||
} | ||
|
||
if (this.config().get("server.https.enabled")) { | ||
await this.app.get<Server>(ApiIdentifiers.HTTPS).boot(); | ||
} | ||
protected httpsIdentifier(): symbol { | ||
return ApiIdentifiers.HTTPS; | ||
} | ||
|
||
public async dispose(): Promise<void> { | ||
if (this.config().get("server.http.enabled")) { | ||
await this.app.get<Server>(ApiIdentifiers.HTTP).dispose(); | ||
} | ||
|
||
if (this.config().get("server.https.enabled")) { | ||
await this.app.get<Server>(ApiIdentifiers.HTTPS).dispose(); | ||
} | ||
protected getServerConstructor(): ServerConstructor<Server> { | ||
return Server; | ||
} | ||
|
||
public configSchema(): object { | ||
return Joi.object({ | ||
options: Joi.object({ | ||
estimateTotalCount: Joi.bool().required(), | ||
}).required(), | ||
protected getHandlers(): any | any[] { | ||
return Handlers; | ||
} | ||
|
||
plugins: Joi.object({ | ||
cache: Joi.object({ | ||
checkperiod: Joi.number().integer().min(0).required(), | ||
enabled: Joi.bool().required(), | ||
stdTTL: Joi.number().integer().min(0).required(), | ||
}).required(), | ||
log: Joi.object({ | ||
enabled: Joi.bool().required(), | ||
public configSchema(): Joi.ObjectSchema { | ||
return super.configSchema().concat( | ||
Joi.object({ | ||
options: Joi.object({ | ||
estimateTotalCount: Joi.bool().required(), | ||
}).required(), | ||
pagination: Joi.object({ | ||
limit: Joi.number().integer().min(0).required(), | ||
}).required(), | ||
rateLimit: Joi.object({ | ||
blacklist: Joi.array().items(Joi.string()).required(), | ||
duration: Joi.number().integer().min(0).required(), | ||
enabled: Joi.bool().required(), | ||
points: Joi.number().integer().min(0).required(), | ||
whitelist: Joi.array().items(Joi.string()).required(), | ||
}).required(), | ||
socketTimeout: Joi.number().integer().min(0).required(), | ||
trustProxy: Joi.bool().required(), | ||
whitelist: Joi.array().items(Joi.string()).required(), | ||
}).required(), | ||
|
||
server: Joi.object({ | ||
http: Joi.object({ | ||
enabled: Joi.bool().required(), | ||
host: Joi.string().required(), | ||
port: Joi.number().integer().min(1).max(65_535).required(), | ||
}).required(), | ||
https: Joi.object({ | ||
enabled: Joi.bool().required(), | ||
host: Joi.string().required(), | ||
port: Joi.number().integer().min(1).max(65_535).required(), | ||
tls: Joi.object({ | ||
cert: Joi.string().when("...enabled", { is: true, then: Joi.required() }), | ||
key: Joi.string().when("...enabled", { is: true, then: Joi.required() }), | ||
plugins: Joi.object({ | ||
cache: Joi.object({ | ||
checkperiod: Joi.number().integer().min(0).required(), | ||
enabled: Joi.bool().required(), | ||
stdTTL: Joi.number().integer().min(0).required(), | ||
}).required(), | ||
log: Joi.object({ | ||
enabled: Joi.bool().required(), | ||
}).required(), | ||
pagination: Joi.object({ | ||
limit: Joi.number().integer().min(0).required(), | ||
}).required(), | ||
rateLimit: Joi.object({ | ||
blacklist: Joi.array().items(Joi.string()).required(), | ||
duration: Joi.number().integer().min(0).required(), | ||
enabled: Joi.bool().required(), | ||
points: Joi.number().integer().min(0).required(), | ||
whitelist: Joi.array().items(Joi.string()).required(), | ||
}).required(), | ||
socketTimeout: Joi.number().integer().min(0).required(), | ||
trustProxy: Joi.bool().required(), | ||
whitelist: Joi.array().items(Joi.string()).required(), | ||
}).required(), | ||
}).required(), | ||
}).unknown(true); | ||
} | ||
|
||
private async buildServer(type: string, id: symbol): Promise<void> { | ||
this.app.bind<Server>(id).to(Server).inSingletonScope(); | ||
|
||
const server: Server = this.app.get<Server>(id); | ||
|
||
await server.initialize(`Public API (${type.toUpperCase()})`, { | ||
...this.config().get(`server.${type}`), | ||
|
||
routes: { | ||
cors: true, | ||
}, | ||
}); | ||
|
||
await server.register(preparePlugins(this.config().get("plugins"))); | ||
|
||
await server.register({ | ||
plugin: Handlers, | ||
routes: { prefix: "/api" }, | ||
}); | ||
}).unknown(true), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Path-based git attributes | ||
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html | ||
|
||
# Ignore all test and documentation with "export-ignore". | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/README.md export-ignore |
Oops, something went wrong.