Skip to content

Commit

Permalink
Update region tags (#4951)
Browse files Browse the repository at this point in the history
  • Loading branch information
averikitsch authored Nov 9, 2020
1 parent 721c089 commit 03c9ee5
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 83 deletions.
4 changes: 3 additions & 1 deletion run/hello-broken/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START cloudrun_broken_dockerfile]
# [START run_broken_dockerfile]

# Use the official Python image.
Expand All @@ -33,10 +34,11 @@ ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

# Run the web service on container startup.
# Run the web service on container startup.
# Use gunicorn webserver with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app

# [END run_broken_dockerfile]
# [END cloudrun_broken_dockerfile]
8 changes: 8 additions & 0 deletions run/hello-broken/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START cloudrun_broken_service]
# [START run_broken_service]
import json
import os
Expand All @@ -26,22 +27,26 @@
def index():
print("hello: received request.")

# [START cloudrun_broken_service_problem]
# [START run_broken_service_problem]
NAME = os.getenv("NAME")

if not NAME:
print("Environment validation failed.")
raise Exception("Missing required service parameter.")
# [END run_broken_service_problem]
# [END cloudrun_broken_service_problem]

return f"Hello {NAME}"
# [END run_broken_service]
# [END cloudrun_broken_service]


@app.route("/improved", methods=["GET"])
def improved():
print("hello: received request.")

# [START cloudrun_broken_service_upgrade]
# [START run_broken_service_upgrade]
NAME = os.getenv("NAME")

Expand All @@ -53,10 +58,12 @@ def improved():
}
print(json.dumps(error_message))
# [END run_broken_service_upgrade]
# [END cloudrun_broken_service_upgrade]

return f"Hello {NAME}"


# [START cloudrun_broken_service]
# [START run_broken_service]
if __name__ == "__main__":
PORT = int(os.getenv("PORT")) if os.getenv("PORT") else 8080
Expand All @@ -65,3 +72,4 @@ def improved():
# application on Cloud Run. See entrypoint in Dockerfile.
app.run(host="127.0.0.1", port=PORT, debug=True)
# [END run_broken_service]
# [END cloudrun_broken_service]
2 changes: 2 additions & 0 deletions run/helloworld/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START cloudrun_helloworld_dockerfile]
# [START run_helloworld_dockerfile]

# Use the official lightweight Python image.
Expand All @@ -36,3 +37,4 @@ RUN pip install Flask gunicorn
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app

# [END run_helloworld_dockerfile]
# [END cloudrun_helloworld_dockerfile]
2 changes: 2 additions & 0 deletions run/helloworld/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START cloudrun_helloworld_service]
# [START run_helloworld_service]
import os

Expand All @@ -29,3 +30,4 @@ def hello_world():
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))
# [END run_helloworld_service]
# [END cloudrun_helloworld_service]
6 changes: 4 additions & 2 deletions run/image-processing/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ COPY requirements.txt ./
# Install production dependencies.
RUN pip install -r requirements.txt

# [START cloudrun_imageproc_dockerfile_imagemagick]
# [START run_imageproc_dockerfile_imagemagick]
# Install Imagemagick into the container image.
# For more on system packages review the system packages tutorial.
Expand All @@ -35,14 +36,15 @@ RUN set -ex; \
apt-get -y install imagemagick; \
rm -rf /var/lib/apt/lists/*
# [END run_imageproc_dockerfile_imagemagick]
# [END cloudrun_imageproc_dockerfile_imagemagick]

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

# Run the web service on container startup.
# Run the web service on container startup.
# Use gunicorn webserver with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
7 changes: 6 additions & 1 deletion run/image-processing/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.


# [START cloudrun_imageproc_handler_setup]
# [START run_imageproc_handler_setup]
import os
import tempfile
Expand All @@ -23,8 +23,10 @@
storage_client = storage.Client()
vision_client = vision.ImageAnnotatorClient()
# [END run_imageproc_handler_setup]
# [END cloudrun_imageproc_handler_setup]


# [START cloudrun_imageproc_handler_analyze]
# [START run_imageproc_handler_analyze]
# Blurs uploaded images that are flagged as Adult or Violence.
def blur_offensive_images(data):
Expand Down Expand Up @@ -56,8 +58,10 @@ def blur_offensive_images(data):


# [END run_imageproc_handler_analyze]
# [END cloudrun_imageproc_handler_analyze]


# [START cloudrun_imageproc_handler_blur]
# [START run_imageproc_handler_blur]
# Blurs the given file using ImageMagick.
def __blur_image(current_blob):
Expand Down Expand Up @@ -89,3 +93,4 @@ def __blur_image(current_blob):


# [END run_imageproc_handler_blur]
# [END cloudrun_imageproc_handler_blur]
2 changes: 2 additions & 0 deletions run/image-processing/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START cloudrun_imageproc_controller]
# [START run_imageproc_controller]
import base64
import json
Expand Down Expand Up @@ -69,6 +70,7 @@ def index():

return ('', 500)
# [END run_imageproc_controller]
# [END cloudrun_imageproc_controller]


if __name__ == '__main__':
Expand Down
2 changes: 2 additions & 0 deletions run/logging-manual/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
def index():
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']

# [START cloudrun_manual_logging]
# [START run_manual_logging]
# Uncomment and populate this variable in your code:
# PROJECT = 'The project ID of your Cloud Run service';
Expand All @@ -50,6 +51,7 @@ def index():

print(json.dumps(entry))
# [END run_manual_logging]
# [END cloudrun_manual_logging]

return 'Hello Logger!'

Expand Down
2 changes: 2 additions & 0 deletions run/markdown-preview/editor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def index():
return render_template("index.html", default=f.read())


# [START cloudrun_secure_request_do]
# [START run_secure_request_do]
@app.route("/render", methods=["POST"])
def render_handler():
Expand All @@ -40,6 +41,7 @@ def render_handler():
parsed_markdown = render.new_request(data)
return parsed_markdown
# [END run_secure_request_do]
# [END cloudrun_secure_request_do]


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions run/markdown-preview/editor/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START cloudrun_secure_request]
# [START run_secure_request]
import os
import urllib
Expand Down Expand Up @@ -43,3 +44,4 @@ def new_request(data):
response = urllib.request.urlopen(req)
return response.read()
# [END run_secure_request]
# [END cloudrun_secure_request]
156 changes: 79 additions & 77 deletions run/markdown-preview/editor/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,94 +13,96 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Markdown Editor</title>
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=">
<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Markdown Editor</title>
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=">
<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>
<body class="mdc-typography">

<header class="mdc-top-app-bar mdc-top-app-bar--fixed">
<div class="mdc-top-app-bar__row">
<section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-start">
<span class="mdc-top-app-bar__title">Markdown Editor</span>
</section>
<section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-end" role="toolbar">
<a href="#code" title="View the code"><i class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-hidden="true">code</i></a>
<a href="#tutorial" title="Read the tutorial"><i class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-hidden="true">assignment</i></a>
</section>
</div>
</header>
<header class="mdc-top-app-bar mdc-top-app-bar--fixed">
<div class="mdc-top-app-bar__row">
<section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-start">
<span class="mdc-top-app-bar__title">Markdown Editor</span>
</section>
<section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-end" role="toolbar">
<a href="#code" title="View the code"><i class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-hidden="true">code</i></a>
<a href="#tutorial" title="Read the tutorial"><i class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-hidden="true">assignment</i></a>
</section>
</div>
</header>

<div role="progressbar" class="mdc-linear-progress mdc-linear-progress--indeterminate mdc-top-app-bar--fixed-adjust" aria-label="Markdown Rendering Progress Bar" aria-valuemin="0" aria-valuemax="1" aria-valuenow="0">
<div class="mdc-linear-progress__bar mdc-linear-progress__primary-bar">
<span class="mdc-linear-progress__bar-inner"></span>
</div>
<div role="progressbar" class="mdc-linear-progress mdc-linear-progress--indeterminate mdc-top-app-bar--fixed-adjust" aria-label="Markdown Rendering Progress Bar" aria-valuemin="0" aria-valuemax="1" aria-valuenow="0">
<div class="mdc-linear-progress__bar mdc-linear-progress__primary-bar">
<span class="mdc-linear-progress__bar-inner"></span>
</div>
</div>

<main class="mdc-layout-grid">
<div class="mdc-layout-grid__inner">
<div class="mdc-layout-grid__cell mdc-layout-grid__cell--span-6">
<h2>Markdown Text</h2>
<section class="mdc-card mdc-card--outlined">
<div class="text-field-container">
<div class="mdc-text-field mdc-text-field--fullwidth md-text-field--no-label mdc-text-field--textarea mdc-ripple-upgraded">
<textarea id="editor" class="mdc-text-field__input" style="height: 36rem;">{{ default }}</textarea>
</div></div>
<main class="mdc-layout-grid">
<div class="mdc-layout-grid__inner">
<div class="mdc-layout-grid__cell mdc-layout-grid__cell--span-6">
<h2>Markdown Text</h2>
<section class="mdc-card mdc-card--outlined">
<div class="text-field-container">
<div class="mdc-text-field mdc-text-field--fullwidth md-text-field--no-label mdc-text-field--textarea mdc-ripple-upgraded">
<textarea id="editor" class="mdc-text-field__input" style="height: 36rem;">{{ default }}</textarea>
</div>
</div>

<div class="mdc-card__actions mdc-card__actions--full-bleed">
<button class="editor-button mdc-button mdc-card__action mdc-card__action--button mdc-ripple-surface">
<span class="mdc-button__label">Preview Rendered Markdown</span>
<i class="material-icons" aria-hidden="true">arrow_forward</i>
</button>
</div>
</section></div>
<div class="mdc-card__actions mdc-card__actions--full-bleed">
<button class="editor-button mdc-button mdc-card__action mdc-card__action--button mdc-ripple-surface">
<span class="mdc-button__label">Preview Rendered Markdown</span>
<i class="material-icons" aria-hidden="true">arrow_forward</i>
</button>
</div>
</section>
</div>

<div class="mdc-layout-grid__cell mdc-layout-grid__cell--span-6">
<h2>Rendered HTML</h2>
<section class="mdc-card mdc-card--outlined">
<div id="preview" style="height: 40rem; padding-left: 10px; padding-right: 10px">Tap "<strong>Preview Rendered Markdown</strong>" below the text entry to see rendered content.</div>
</section></div>
</div>
</div>
</main>
<div class="mdc-layout-grid__cell mdc-layout-grid__cell--span-6">
<h2>Rendered HTML</h2>
<section class="mdc-card mdc-card--outlined">
<div id="preview" style="height: 40rem; padding-left: 10px; padding-right: 10px">Tap "<strong>Preview Rendered Markdown</strong>" below the text entry to see rendered content.</div>
</section>
</div>
</div>
</main>

<script>
const preview = document.getElementById('preview');
const lp = new mdc.linearProgress.MDCLinearProgress(document.querySelector('.mdc-linear-progress'));
async function render(data = {}) {
const response = await fetch('/render', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
<script>
const preview = document.getElementById('preview');
const lp = new mdc.linearProgress.MDCLinearProgress(document.querySelector('.mdc-linear-progress'));
async function render(data = {}) {
const response = await fetch('/render', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});

const text = await response.text();
if (!response.ok) {
console.log('error: Render Text: Received status code: ' + response.status);
}
const text = await response.text();
if (!response.ok) {
console.log('error: Render Text: Received status code: ' + response.status);
}

return text;
}
return text;
}

function listener() {
lp.open();
render({data: document.getElementById('editor').value})
.then((result) => preview.innerHTML = result)
.catch((err) => {
console.log('Render Text: ' + err.message);
preview.innerHTML = '<h3><i aria-hidden="true" class="material-icons">error</i>Render Error</h3>\n<p>' + err.message + '</p>';
})
.finally(() => lp.close())
}
function listener() {
lp.open();
render({data: document.getElementById('editor').value})
.then((result) => preview.innerHTML = result)
.catch((err) => {
console.log('Render Text: ' + err.message);
preview.innerHTML = '<h3><i aria-hidden="true" class="material-icons">error</i>Render Error</h3>\n<p>' + err.message + '</p>';
})
.finally(() => lp.close())
}

document.querySelector('.editor-button').addEventListener('click', listener);
window.addEventListener('load', listener);
</script>
document.querySelector('.editor-button').addEventListener('click', listener);
window.addEventListener('load', listener);
</script>
</body>
</html>
</html>
Loading

0 comments on commit 03c9ee5

Please sign in to comment.