Ver Fonte

Step 01.1: Introducing Flask: Our First Route

Frédéric G. MARAND há 6 meses atrás
pai
commit
c278ee8e3e
3 ficheiros alterados com 74 adições e 0 exclusões
  1. 20 0
      .idea/aws.xml
  2. 36 0
      contactsapp/app.go
  3. 18 0
      contactsapp/app.py

+ 20 - 0
.idea/aws.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="accountSettings">
+    <option name="activeProfile" value="profile:default" />
+    <option name="activeRegion" value="eu-west-3" />
+    <option name="recentlyUsedProfiles">
+      <list>
+        <option value="profile:default" />
+      </list>
+    </option>
+    <option name="recentlyUsedRegions">
+      <list>
+        <option value="eu-west-3" />
+      </list>
+    </option>
+  </component>
+  <component name="connectionManager">
+    <option name="activeConnectionId" value="sso;us-east-1;https://view.awsapps.com/start" />
+  </component>
+</project>

+ 36 - 0
contactsapp/app.go

@@ -0,0 +1,36 @@
+package main
+
+import (
+	"errors"
+	"log"
+	"net/http"
+)
+
+const (
+	addr = ":8080"
+)
+
+/*
+@app.route("/")
+def index():
+*/
+func RouteIndex(w http.ResponseWriter, r *http.Request) {
+	w.Write([]byte("Hello World!"))
+}
+
+func setupRoutes(mux *http.ServeMux) {
+	mux.HandleFunc("/", RouteIndex)
+}
+
+func main() {
+	mux := http.NewServeMux()
+	setupRoutes(mux)
+	log.Printf("Listening on http://localhost%s", addr)
+	if err := http.ListenAndServe(addr, mux); err != nil {
+		if !errors.Is(err, http.ErrServerClosed) {
+			log.Printf("Server error: %v\n", err)
+			return
+		}
+	}
+	log.Println("Server shutdown")
+}

+ 18 - 0
contactsapp/app.py

@@ -0,0 +1,18 @@
+from flask import (
+    Flask, redirect, render_template, request, flash, jsonify, send_file
+)
+
+from contacts_model import Contact, Archiver
+
+# ========================================================
+# Flask App
+# ========================================================
+
+app = Flask(__name__)
+
+app.secret_key = b'hypermedia rocks'
+
+
+@app.route("/")
+def index():
+    return redirect("/contacts")