Skip to content

Instantly share code, notes, and snippets.

@drio
Last active January 20, 2023 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drio/6c2f0eaec5b59462dc54c77a7aa38c25 to your computer and use it in GitHub Desktop.
Save drio/6c2f0eaec5b59462dc54c77a7aa38c25 to your computer and use it in GitHub Desktop.
fly-test
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>Hello from Fly 🚀 v0.0.2</h1>
{{ if .Region }}
<p>I'm running in the {{.Region}} region</p>
{{ else }}
<p>Running locally perhaps?</p>
{{end}}
<p>
LocalData:
{{ if .LocalData }}
{{.LocalData}}
{{ else }}
-
{{end}}
</p>
</body>
</html>
package main
import (
"log"
"net/http"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8181"
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("local-data"))
})
log.Println("listening on", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
package main
import (
"embed"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
)
//go:embed templates/*
var resources embed.FS
var t = template.Must(template.ParseFS(resources, "templates/*"))
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
uri := "http://localhost:8181/"
resp, err := http.Get(uri)
if err != nil {
log.Fatalf("http.Get() failed with '%s'\n", err)
}
defer resp.Body.Close()
d, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("ioutil.ReadAll() failed with '%s'\n", err)
}
data := map[string]string{
"Region": os.Getenv("FLY_REGION"),
"LocalData": string(d),
}
t.ExecuteTemplate(w, "index.html.tmpl", data)
})
log.Println("listening on", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment