Last active
January 20, 2023 17:28
-
-
Save drio/6c2f0eaec5b59462dc54c77a7aa38c25 to your computer and use it in GitHub Desktop.
fly-test
This file contains 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
<!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> | |
This file contains 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 ( | |
"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)) | |
} |
This file contains 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 ( | |
"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