Last active
March 6, 2022 08:10
-
-
Save yanc0/1199c9f76c0e2c60ae56162ad3119d2f to your computer and use it in GitHub Desktop.
crabe.life source code
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
FROM golang AS build | |
COPY main.go ./ | |
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o crabe-life main.go | |
FROM scratch AS run | |
COPY --from=build /go/crabe-life /crabe-life | |
EXPOSE 8080 | |
ENTRYPOINT ["/crabe-life"] |
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 ( | |
"flag" | |
"fmt" | |
"net/http" | |
"os" | |
"time" | |
) | |
var html string = ` | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Crab Life !</title> | |
</head> | |
<body> | |
<div id="crab" style="margin: auto; text-align: center; padding: 5em;"> | |
<img src="%s" alt="crab" width="300" /> | |
</div> | |
<div id="crab_story" style="margin: auto; text-align: center; font-size: 2em;"> | |
<div style="color:chocolate; font-weight: bold;">Hi ! I was born on %s</div> | |
<div style="color: cadetblue;">My motherboard's name is <strong>%s</strong></div> | |
<div style="color: coral;font-size: 0.5em;">(This page will be kept in cache for %d seconds)</div> | |
</div> | |
</body> | |
</html> | |
` | |
var crabURL *string | |
var hostname string | |
var ttl *int | |
func crab(w http.ResponseWriter, r *http.Request) { | |
w.Header().Add("Cache-Control", fmt.Sprintf("public, max-age=%d", *ttl)) | |
fmt.Fprintf(w, html, *crabURL, time.Now().Format(time.Stamp), hostname, *ttl) | |
} | |
func main() { | |
var err error | |
// Get TTL value for cache configuration | |
ttl = flag.Int("cache-ttl", 0, "time in second to cache HTTP response") | |
crabURL = flag.String("crab-url", "https://i.gifer.com/3QZn.gif", "url to crab gif") | |
flag.Parse() | |
// Get Hostname of the server running this app | |
hostname, err = os.Hostname() | |
if err != nil { | |
hostname = "unknown" | |
} | |
http.HandleFunc("/", crab) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment