Created
August 9, 2022 05:48
-
-
Save percybolmer/5760cbd04aa817164948fa71e322f97b to your computer and use it in GitHub Desktop.
Kuberenetes main with connecting to db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
func main() { | |
// Grab time of startup | |
started := time.Now() | |
// Connect to database, or Crash | |
if err := connectDatabase(); err != nil { | |
log.Fatal(err) | |
} | |
defer databaseConn.Close() | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
// Calculate runtime duration | |
duration := time.Now().Sub(started) | |
if duration.Seconds() > 100 { | |
log.Println("Timeout triggered") | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte(`hello gopher`)) | |
} else { | |
w.WriteHeader(http.StatusOK) | |
w.Write([]byte(`hello gopher`)) | |
} | |
}) | |
http.HandleFunc("/aligator", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hi Mr Aligator") | |
}) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment