Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created August 9, 2022 05:48
Show Gist options
  • Save percybolmer/5760cbd04aa817164948fa71e322f97b to your computer and use it in GitHub Desktop.
Save percybolmer/5760cbd04aa817164948fa71e322f97b to your computer and use it in GitHub Desktop.
Kuberenetes main with connecting to db
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