Issue
I want to make a simple serverless backend, so I'm trying to use google cloud functions with flask routing.
I'm trying to test a simple code, but it's not working. The following source always returns 404 errors.
from flask import Flask, make_response
class Services:
pass
def create_app(test_config = None):
app = Flask(__name__)
services = Services
create_endpoints(app, services)
return app
def create_endpoints(app, services):
@app.route("/test", methods=['GET'])
def test():
return make_response('Test worked!', 200)
function URL : ######.cloudfunctions.net/test1
I tried "######.cloudfunctions.net/test1" and "######.cloudfunctions.net/test1/test", but it always returns 404 error.
can I use flask routing ?
Solution
I think that it is a not fancy way to add a router to a cloud function, but it works.
I used the property "path" of the object "request"( this is a flask.request object) to read the path after the domain in the requested URL
from flask import abort
def coolrouter(request):
path = (request.path)
if (path == "/test"):
return "test page"
elif (path == "/home" or path =="/"):
return "ḧome page"
else:
abort (404)
keep in mind that cloud functions are designed to be a one shot services, that means that is not possible to save session variables or other things since this is service is ephemeral.
If you want upload a complete site I recommend you to use App Engine, which is a Fully managed serverless application platform.
Answered By - Jan Hernandez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.