From 5adde814c9d1a31d57328ff042f778a568864166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Fri, 18 Oct 2019 22:55:26 +0200 Subject: [PATCH] fix python isprime --- hello-world-go/helloworld.go | 3 --- isprime-go/isprime.go | 3 --- isprime-python/Dockerfile | 4 ++-- isprime-python/app.py | 8 +++++++- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/hello-world-go/helloworld.go b/hello-world-go/helloworld.go index dbe5c7c..7c7e019 100644 --- a/hello-world-go/helloworld.go +++ b/hello-world-go/helloworld.go @@ -8,7 +8,6 @@ import ( ) func handler(w http.ResponseWriter, r *http.Request) { - log.Print("Hello world received a request.") target := os.Getenv("TARGET") if target == "" { target = "World" @@ -17,8 +16,6 @@ func handler(w http.ResponseWriter, r *http.Request) { } func main() { - log.Print("Hello world sample started.") - http.HandleFunc("/", handler) port := os.Getenv("PORT") diff --git a/isprime-go/isprime.go b/isprime-go/isprime.go index 036df70..0aec48e 100644 --- a/isprime-go/isprime.go +++ b/isprime-go/isprime.go @@ -15,7 +15,6 @@ func handler(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Errorf("Failed to parse %s as int! %v", target, err) } - log.Print("Checking if %s is prime", target) if num <= 1 { fmt.Sprintf("%d is not prime", num) } @@ -28,8 +27,6 @@ func handler(w http.ResponseWriter, r *http.Request) { } func main() { - log.Print("Hello world sample started.") - http.HandleFunc("/", handler) port := os.Getenv("PORT") diff --git a/isprime-python/Dockerfile b/isprime-python/Dockerfile index a1bc15d..de8c556 100644 --- a/isprime-python/Dockerfile +++ b/isprime-python/Dockerfile @@ -5,11 +5,11 @@ FROM python:3.7-slim # Copy local code to the container image. ENV APP_HOME /app WORKDIR $APP_HOME -COPY . ./ # Install production dependencies. -RUN pip install Flask gunicorn +RUN pip install --no-cache-dir Flask gunicorn +COPY . ./ # Run the web service on container startup. Here we use the gunicorn # webserver, with one worker process and 8 threads. # For environments with multiple CPU cores, increase the number of workers diff --git a/isprime-python/app.py b/isprime-python/app.py index 560a05d..d593993 100644 --- a/isprime-python/app.py +++ b/isprime-python/app.py @@ -4,10 +4,16 @@ from flask import Flask app = Flask(__name__) +def safe_cast(val, to_type, default=107107): + try: + return to_type(val) + except (ValueError, TypeError): + return default @app.route('/') def isprime(): - num = os.environ.get('TARGET', '107107') + num = safe_cast(os.environ.get('TARGET', 107107),int) + if num > 1: for i in range(2, num): if (num % i) == 0: