ketan_patel@cloudshell:~ (new-user-learning)$ mkdir helloworld
ketan_patel@cloudshell:~ (new-user-learning)$ cd helloworld
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ touch main.py
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ vi main.py
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ vi requirements.txt
import flask
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ vi app.yaml
WRITE THE WEBAPP:
Note: This web app is a simple web service responding to HTTP GET requests with the message Hello World!.
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ cat main.py
import flask
# If `entrypoint` is not defined in app.yaml, App Engine will look for an app
# called `app` in `main.py`.
app = flask.Flask(__name__)
@app.get("/")
def hello():
"""Return a friendly HTTP greeting."""
return "Hello World!\n"
if __name__ == "__main__":
# Used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
app.run(host="localhost", port=8080, debug=True)
DEFINE THE DEPENDENCIES:
To specify the dependencies of your web app, go back to the terminal and create a requirements.txt file in the root directory of your project, with the exact version of Flask to use:
Note: The only dependency in your web app is Flask.
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ cat requirements.txt
# https://pypi.org/project/Flask
Flask==2.2.3
CONFIGURE THE DEPLOYMENT:
To deploy your web app to App Engine, you need an app.yaml file. This configuration file defines your web app's settings for App Engine.
Note: With this simple web app, you just need to specify your Python version (e.g. 3.11 here). For more complicated web apps, you can configure additional settings, like scaling, handlers for static files, additional handlers, and other application elements like environment variables and service names. For more information and a list of all supported items, see the app.yaml reference.
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ cat app.yaml
runtime: python311
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ ls
app.yaml main.py requirements.txt
DEPLOY THE WEB APP:
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ gcloud app deploy
Services to deploy:
descriptor: [/home/ketan_patel/helloworld/app.yaml]
source: [/home/ketan_patel/helloworld]
target project: [new-user-learning]
target service: [default]
target version: [20230820t060600]
target url: [https://new-user-learning.uw.r.appspot.com]
target service account: [new-user-learning@appspot.gserviceaccount.com]
Do you want to continue (Y/n)? Y
Beginning deployment of service [default]...
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
Uploading 3 files to Google Cloud Storage
33%
67%
100%
100%
File upload done.
Updating service [default]...done.
Setting traffic split for service [default]...done.
Deployed service [default] to [https://new-user-learning.uw.r.appspot.com]
You can stream logs from the command line by running:
$ gcloud app logs tail -s default
To view your application in the web browser run:
$ gcloud app browse
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ gcloud app browse
Did not detect your browser. Go to this link to view your app:
https://new-user-learning.uw.r.appspot.com
Your web app is now ready to respond to HTTP requests on https://PROJECT_ID.REGION_ID.r.appspot.com.
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ curl https://new-user-learning.uw.r.appspot.com/
Hello World!
8. UPDATE THE WEB APP:
Modify your web app by changing the hello() function body in your main.py file.
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ vi main.py
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ cat main.py
import flask
# If `entrypoint` is not defined in app.yaml, App Engine will look for an app
# called `app` in `main.py`.
app = flask.Flask(__name__)
@app.get("/")
def hello():
"""Return a friendly HTTP greeting."""
#return "Hello World!\n"
who = flask.request.args.get("who", "World")
return f"Hello {who}!\n"
if __name__ == "__main__":
# Used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
app.run(host="localhost", port=8080, debug=True)
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ gcloud app deploy --quiet
Services to deploy:
descriptor: [/home/ketan_patel/helloworld/app.yaml]
source: [/home/ketan_patel/helloworld]
target project: [new-user-learning]
target service: [default]
target version: [20230820t061149]
target url: [https://new-user-learning.uw.r.appspot.com]
target service account: [new-user-learning@appspot.gserviceaccount.com]
Beginning deployment of service [default]...
Uploading 1 file to Google Cloud Storage
100%
100%
File upload done.
Updating service [default]...done.
Setting traffic split for service [default]...done.
Deployed service [default] to [https://new-user-learning.uw.r.appspot.com]
You can stream logs from the command line by running:
$ gcloud app logs tail -s default
To view your application in the web browser run:
$ gcloud app browse
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ gcloud app browse
Did not detect your browser. Go to this link to view your app:
https://new-user-learning.uw.r.appspot.com
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ curl https://new-user-learning.uw.r.appspot.com/
Hello World!
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ curl https://new-user-learning.uw.r.appspot.com?who=Universe
Hello Universe!
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ curl https://new-user-learning.uw.r.appspot.com?who=ketan
Hello ketan!
ketan_patel@cloudshell:~/helloworld (new-user-learning)$
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ APPENGINE_HOSTNAME=$(gcloud app describe --format "value(defaultHostname)")
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ echo $APPENGINE_HOSTNAME
new-user-learning.uw.r.appspot.com
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ curl https://$APPENGINE_HOSTNAME
Hello World!
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ curl https://$APPENGINE_HOSTNAME?who=Universe
Hello Universe!
ketan_patel@cloudshell:~/helloworld (new-user-learning)$ curl https://$APPENGINE_HOSTNAME?who=ketan
Hello ketan!
ketan_patel@cloudshell:~/helloworld (new-user-learning)$
No comments:
Post a Comment