Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api path config #439

Merged
merged 2 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/cli/src/commands/create-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export class CreateStackCommand extends Command {
options: ['info', 'debug'], // default value if flag not passed (can be a function that returns a string or undefined)
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
apiRoute: flags.string({
description: 'path to use for api route', // help description for flag
hidden: false, // hide from help
default: '/api',
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
};

static args = [
Expand Down Expand Up @@ -195,7 +201,8 @@ export class CreateStackCommand extends Command {
appconfig.readOnly = flags.readonly;
appconfig.enableSwagger = flags.swagger;
appconfig.stackName = stackName!;
appconfig.logLevel = flags.loglevel as LogLevel;;
appconfig.logLevel = flags.loglevel as LogLevel;
appconfig.apiRoutePath = flags.apiRoute;
Helpers.createDir(stackFolder + '/config');
fs.writeFileSync(
path.normalize(stackFolder + '/config/appconfig.json'),
Expand Down
11 changes: 9 additions & 2 deletions packages/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export class Run extends Command {
options: ['info', 'debug'], // default value if flag not passed (can be a function that returns a string or undefined)
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
apiRoute: flags.string({
description: 'path to use for api route', // help description for flag
hidden: false, // hide from help
default: '/api',
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
};

static args = [
Expand All @@ -62,6 +68,7 @@ export class Run extends Command {
defaultConfig.readOnly = flags.readonly;
defaultConfig.enableSwagger = flags.swagger;
defaultConfig.logLevel = flags.loglevel as LogLevel;
defaultConfig.apiRoutePath = flags.apiRoute;
defaultConfig.jsonFile = args.file;
if (args.file && flags.env) {
const promise = startServer(
Expand Down Expand Up @@ -91,7 +98,7 @@ export class Run extends Command {
},
{
text: `${chalk.blueBright('API Routes')}`,
link: 'http://localhost:3000/api/{routes}',
link: 'http://localhost:3000' + flags.apiRoute + '/{routes}',
},
],
{ text: { minWidth: 30 }, link: { minWidth: 20 } },
Expand All @@ -102,7 +109,7 @@ export class Run extends Command {
[
{
text: `${chalk.blueBright('API Routes')}`,
link: 'http://localhost:3000/api/{routes}',
link: 'http://localhost:3000' + flags.apiRoute + '/{routes}',
},
],
{ text: { minWidth: 30 }, link: { minWidth: 20 } },
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/commands/update-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export class UpdateStackCommand extends Command {
options: ['info', 'debug'], // default value if flag not passed (can be a function that returns a string or undefined)
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
apiRoute: flags.string({
description: 'path to use for api route', // help description for flag
hidden: false, // hide from help
default: '/api',
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
};

async run() {
Expand Down Expand Up @@ -132,6 +138,7 @@ export class UpdateStackCommand extends Command {
appConfig.readOnly = flags.readonly;
appConfig.enableSwagger = flags.swagger;
appConfig.logLevel = flags.loglevel as LogLevel;
appConfig.apiRoutePath = flags.apiRoute;
fs.writeFileSync(
path.normalize(stackFolder + '/config/appconfig.json'),
JSON.stringify(appConfig, null, 2),
Expand Down
71 changes: 48 additions & 23 deletions packages/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"nodemon": "2.0.4",
"ts-jest": "26.1.0",
"ts-loader": "7.0.5",
"ts-node": "^8.10.2",
"typescript": "3.9.3"
},
"config": {
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class AppConfig {
enableSwagger = true;
logLevel = LogLevel.info;
stackName = 'jsonsls';
apiRoutePath = '/api';
static merge = <T, U>(t: T, u: U) => Object.assign({}, t, u);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/app/core.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class CoreApp {
1
);
this.server.use(middlewares);
this.server.use('/api', router);
this.server.use(appConfig.apiRoutePath, router);
if (!this.swaggerSpec && appConfig.enableSwagger) {
this.swaggerSpec = this.apispec.generateSpecification(db, true);
const swaggerSetupMiddleware = swaggerUi.setup(this.swaggerSpec);
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class ServerFactory {
server,
new SwaggerConfig(appConfig.readOnly, appConfig.enableApiKeyAuth),
env.basePath,
appConfig.apiRoutePath,
packageJsonFilePath
);
const core = new coreserver(
Expand Down
6 changes: 5 additions & 1 deletion packages/server/src/specifications/swagger/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ export class Swagger implements ApiSpecification {
private server: express.Express;
private config: SwaggerConfig;
private basePath: string;
private apiRoutePath: string;
constructor(
server: express.Express,
config: SwaggerConfig,
basePath: string,
apiRoutePath: string,
packageJsonFilePath: string
) {
this.server = server;
this.config = config;
this.basePath = basePath;
this.apiRoutePath = apiRoutePath;
this.swaggerSpec = new SwaggerSpec(packageJsonFilePath);
}

Expand All @@ -33,7 +36,8 @@ export class Swagger implements ApiSpecification {
this.server,
{},
this.config.readOnly,
this.basePath
this.basePath,
this.apiRoutePath
);
const auth: ApiKeySecurity = {
type: 'apiKey',
Expand Down
12 changes: 8 additions & 4 deletions packages/server/src/specifications/swagger/swaggerspec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class SwaggerSpec {
return sorted;
}

initSpec(readOnly: boolean, basePath: string) {
initSpec(readOnly: boolean, basePath: string, routePath: string) {
const info = this.updateSpecFromPackage(basePath);
let specification: Spec = {
swagger: '2.0',
Expand All @@ -80,7 +80,10 @@ export class SwaggerSpec {
};
specification.swagger = '2.0';
specification.paths = {};
const excludedRoutes = ['/api/:resource/:id/:nested', '/api/db'];
const excludedRoutes = [
routePath + '/:resource/:id/:nested',
routePath + '/db',
];
const endpoints = listEndpoints(this.app);
endpoints.forEach((endpoint: EndPoint) => {
if (readOnly) {
Expand Down Expand Up @@ -134,11 +137,12 @@ export class SwaggerSpec {
app: express.Express,
predefinedSpec: object,
readOnly: boolean,
basePath: string
basePath: string,
apiRoutePath: string
) => {
this.app = app;
this.predefinedSpec = predefinedSpec;
this.spec = this.initSpec(readOnly, basePath);
this.spec = this.initSpec(readOnly, basePath, apiRoutePath);
return this.spec;
};

Expand Down
1 change: 1 addition & 0 deletions packages/server/tests/handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const swagger = new Swagger(
server,
new SwaggerConfig(appConfig.readOnly, appConfig.enableApiKeyAuth),
environment.basePath,
appConfig.apiRoutePath,
'package.json'
);
const localServer = new TestServer(
Expand Down
1 change: 1 addition & 0 deletions packages/template/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const swagger = new Swagger(
server,
new SwaggerConfig(appConfig.readOnly, appConfig.enableApiKeyAuth),
environment.basePath,
appConfig.apiRoutePath,
'./package.json'
);

Expand Down