diff --git a/.gcloudignore b/.gcloudignore new file mode 100644 index 0000000..603f0b6 --- /dev/null +++ b/.gcloudignore @@ -0,0 +1,19 @@ +# This file specifies files that are *not* uploaded to Google Cloud +# using gcloud. It follows the same syntax as .gitignore, with the addition of +# "#!include" directives (which insert the entries of the given .gitignore-style +# file at that point). +# +# For more information, run: +# $ gcloud topic gcloudignore +# +.gcloudignore +# If you would like to upload your .git directory, .gitignore file or files +# from your .gitignore file, remove the corresponding line +# below: +.git +.gitignore + +# Python pycache: +__pycache__/ +# Ignored by the build system +/setup.cfg \ No newline at end of file diff --git a/app.yaml b/app.yaml index 17e7bfd..9ec526f 100644 --- a/app.yaml +++ b/app.yaml @@ -1,8 +1,7 @@ -runtime: python27 -api_version: 1 -threadsafe: true +runtime: python312 +app_engine_apis: true +entrypoint: gunicorn -b :$PORT main:app -# [START handlers] handlers: - url: /favicon\.ico static_files: favicon.ico @@ -12,13 +11,8 @@ handlers: static_dir: bootstrap - url: /.* - script: guestbook.app -# [END handlers] + script: auto -# [START libraries] -libraries: -- name: webapp2 - version: latest -- name: jinja2 - version: latest -# [END libraries] +automatic_scaling: + min_instances: 0 + max_instances: 1 diff --git a/main.py b/main.py new file mode 100644 index 0000000..09fbb62 --- /dev/null +++ b/main.py @@ -0,0 +1,72 @@ +import os +import urllib.parse + +from flask import Flask, render_template, request, redirect +from google.appengine.api import users, wrap_wsgi_app +from google.cloud import ndb + +app = Flask(__name__) +app.wsgi_app = wrap_wsgi_app(app.wsgi_app) +client = ndb.Client() + +DEFAULT_GUESTBOOK_NAME = 'default_guestbook' + +def guestbook_key(guestbook_name=DEFAULT_GUESTBOOK_NAME): + return ndb.Key('Guestbook', guestbook_name) + +class Author(ndb.Model): + identity = ndb.StringProperty() + email = ndb.StringProperty() + +class Greeting(ndb.Model): + author = ndb.StructuredProperty(Author) + content = ndb.StringProperty() + date = ndb.DateTimeProperty(auto_now_add=True) + +@app.route('/') +def main_page(): + guestbook_name = request.args.get('guestbook_name', DEFAULT_GUESTBOOK_NAME) + + with client.context(): + greetings_query = Greeting.query( + ancestor=guestbook_key(guestbook_name)).order(-Greeting.date) + greetings = greetings_query.fetch(10) + + user = users.get_current_user() + if user: + url = users.create_logout_url(request.url) + url_linktext = 'Logout' + else: + url = users.create_login_url(request.url) + url_linktext = 'Login' + + template_values = { + 'user': user, + 'greetings': greetings, + 'guestbook_name': urllib.parse.quote_plus(guestbook_name), + 'url': url, + 'url_linktext': url_linktext, + } + + return render_template('index.html', **template_values) + +@app.route('/sign', methods=['POST']) +def sign_guestbook(): + guestbook_name = request.args.get('guestbook_name', DEFAULT_GUESTBOOK_NAME) + + with client.context(): + greeting = Greeting(parent=guestbook_key(guestbook_name)) + + user = users.get_current_user() + if user: + greeting.author = Author( + identity=user.user_id(), + email=user.email()) + + greeting.content = request.form.get('content') + greeting.put() + + return redirect('/?' + urllib.parse.urlencode({'guestbook_name': guestbook_name})) + +if __name__ == '__main__': + app.run(host='127.0.0.1', port=8080, debug=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c9aa280 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +Flask==3.0.3 +google-cloud-ndb==2.3.1 +appengine-python-standard==1.1.6 +gunicorn==23.0.0 diff --git a/index.html b/templates/index.html similarity index 100% rename from index.html rename to templates/index.html