Skip to content

Commit

Permalink
Rename wsgi.handler to wsgi_handler.handler, rename .wsgi_app to
Browse files Browse the repository at this point in the history
.serverless-wsgi (closes #84)
  • Loading branch information
logandk committed Jan 27, 2019
1 parent 99974fe commit bc14fee
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 151 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# 1.7.0

## Features

- Rename `.wsgi_app` to `.serverless-wsgi` (to follow convention from `serverless-rack`)
- Check for werkzeug presence in bundle or issue warning (#80)

## Bugs

- The `wsgi.handler` has been renamed to `wsgi_handler.handler` due to a naming
conflict with an internal AWS wsgi module. A warning and workaround is issued in order
to prevent breaking existing configuration files (#84)

# 1.6.1

## Features
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ def dog(id):
Load the plugin and set the `custom.wsgi.app` configuration in `serverless.yml` to the
module path of your Flask application.

All functions that will use WSGI need to have `wsgi.handler` set as the Lambda handler and
All functions that will use WSGI need to have `wsgi_handler.handler` set as the Lambda handler and
use the default `lambda-proxy` integration for API Gateway. This configuration example treats
API Gateway as a transparent proxy, passing all requests directly to your Flask application,
and letting the application handle errors, 404s etc.

_Note: The WSGI handler was called `wsgi.handler` earlier, but was renamed to `wsgi_handler.handler`
in `1.7.0`. The old name is still supported but using it will cause a deprecation warning._

```yaml
service: example

Expand All @@ -86,7 +89,7 @@ plugins:

functions:
api:
handler: wsgi.handler
handler: wsgi_handler.handler
events:
- http: ANY /
- http: ANY {proxy+}
Expand Down Expand Up @@ -295,7 +298,7 @@ plugins:
functions:
api:
handler: wsgi.handler
handler: wsgi_handler.handler
events:
- http:
path: cats
Expand Down Expand Up @@ -335,7 +338,7 @@ plugins:

functions:
api:
handler: wsgi.handler
handler: wsgi_handler.handler
events:
- http: ANY /
- http: ANY {proxy+}
Expand Down
79 changes: 58 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ class ServerlessWSGI {
);
}

let handlersFixed = false;

_.each(this.serverless.service.functions, func => {
if (func.handler == "wsgi.handler") {
func.handler = "wsgi_handler.handler";
handlersFixed = true;
}
});

if (handlersFixed) {
this.serverless.cli.log(
'Warning: Please change "wsgi.handler" to "wsgi_handler.handler" in serverless.yml'
);
this.serverless.cli.log(
'Warning: Using "wsgi.handler" still works but has been deprecated and will be removed'
);
this.serverless.cli.log(
"Warning: More information at https://github.com/logandk/serverless-wsgi/issues/84"
);
}

resolve();
});
}
Expand All @@ -55,7 +76,7 @@ class ServerlessWSGI {

this.serverless.service.package.include = _.union(
this.serverless.service.package.include,
["wsgi.py", "serverless_wsgi.py", ".wsgi_app"]
["wsgi_handler.py", "serverless_wsgi.py", ".serverless-wsgi"]
);

if (this.enableRequirements) {
Expand Down Expand Up @@ -134,15 +155,15 @@ class ServerlessWSGI {

return BbPromise.all([
fse.copyAsync(
path.resolve(__dirname, "wsgi.py"),
path.join(this.serverless.config.servicePath, "wsgi.py")
path.resolve(__dirname, "wsgi_handler.py"),
path.join(this.serverless.config.servicePath, "wsgi_handler.py")
),
fse.copyAsync(
path.resolve(__dirname, "serverless_wsgi.py"),
path.join(this.serverless.config.servicePath, "serverless_wsgi.py")
),
fse.writeFileAsync(
path.join(this.serverless.config.servicePath, ".wsgi_app"),
path.join(this.serverless.config.servicePath, ".serverless-wsgi"),
JSON.stringify(this.getWsgiHandlerConfiguration())
)
]);
Expand Down Expand Up @@ -289,7 +310,11 @@ class ServerlessWSGI {
}

cleanup() {
const artifacts = ["wsgi.py", "serverless_wsgi.py", ".wsgi_app"];
const artifacts = [
"wsgi_handler.py",
"serverless_wsgi.py",
".serverless-wsgi"
];

return BbPromise.all(
_.map(artifacts, artifact =>
Expand All @@ -307,7 +332,7 @@ class ServerlessWSGI {
_.merge(process.env, providerEnvVars);

_.each(this.serverless.service.functions, func => {
if (func.handler == "wsgi.handler") {
if (func.handler == "wsgi_handler.handler") {
const functionEnvVars = _.omitBy(func.environment || {}, _.isObject);
_.merge(process.env, functionEnvVars);
}
Expand Down Expand Up @@ -358,7 +383,7 @@ class ServerlessWSGI {
findHandler() {
return _.findKey(
this.serverless.service.functions,
fun => fun.handler == "wsgi.handler"
fun => fun.handler == "wsgi_handler.handler"
);
}

Expand All @@ -367,7 +392,7 @@ class ServerlessWSGI {

if (!handlerFunction) {
return BbPromise.reject(
"No functions were found with handler: wsgi.handler"
"No functions were found with handler: wsgi_handler.handler"
);
}

Expand Down Expand Up @@ -570,17 +595,26 @@ class ServerlessWSGI {

"wsgi:install:install": deployBeforeHook,

"wsgi:command:command": () => BbPromise.bind(this).then(this.command),
"wsgi:exec:exec": () => BbPromise.bind(this).then(this.exec),
"wsgi:manage:manage": () => BbPromise.bind(this).then(this.manage),
"wsgi:command:command": () =>
BbPromise.bind(this)
.then(this.validate)
.then(this.command),
"wsgi:exec:exec": () =>
BbPromise.bind(this)
.then(this.validate)
.then(this.exec),
"wsgi:manage:manage": () =>
BbPromise.bind(this)
.then(this.validate)
.then(this.manage),

"wsgi:clean:clean": () => deployAfterHook().then(this.cleanRequirements),

"before:package:createDeploymentArtifacts": deployBeforeHook,
"after:package:createDeploymentArtifacts": deployAfterHook,

"before:deploy:function:packageFunction": () => {
if (this.options.functionObj.handler == "wsgi.handler") {
if (this.options.functionObj.handler == "wsgi_handler.handler") {
return deployBeforeHook();
} else {
return deployBeforeHookWithoutHandler();
Expand All @@ -596,17 +630,20 @@ class ServerlessWSGI {
this.options.function
);

if (functionObj.handler == "wsgi.handler") {
return BbPromise.bind(this)
.then(this.validate)
.then(() => {
return BbPromise.bind(this)
.then(this.validate)
.then(() => {
if (functionObj.handler == "wsgi_handler.handler") {
return this.packWsgiHandler(false);
});
} else {
return BbPromise.resolve();
}
} else {
return BbPromise.resolve();
}
});
},
"after:invoke:local:invoke": () => BbPromise.bind(this).then(this.cleanup)
"after:invoke:local:invoke": () =>
BbPromise.bind(this)
.then(this.validate)
.then(this.cleanup)
};
}
}
Expand Down
Loading

0 comments on commit bc14fee

Please sign in to comment.