-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM golang:1.22.3-alpine AS build | ||
WORKDIR /app | ||
|
||
COPY main.go . | ||
RUN CGO_ENABLED=0 GOOS=linux go build -o myapp /app/main.go | ||
|
||
FROM alpine:edge | ||
WORKDIR /app | ||
|
||
COPY tpl tpl | ||
COPY --from=build /app/myapp . | ||
|
||
# Set the timezone and install CA certificates | ||
RUN apk --no-cache add ca-certificates tzdata | ||
|
||
ENTRYPOINT ["/app/myapp"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
var tmpl *template.Template | ||
|
||
func init() { | ||
// Parse the template from a separate file | ||
var err error | ||
tmpl, err = template.ParseFiles("tpl/index.html") | ||
if err != nil { | ||
log.Fatal("error parsing template", err) | ||
} | ||
} | ||
|
||
type indexTplData struct { | ||
Inst string | ||
ForwardedFor string | ||
} | ||
|
||
func indexHandler(w http.ResponseWriter, r *http.Request) { | ||
err := tmpl.Execute(w, indexTplData{ | ||
Inst: r.Header.Get("X-Tuc-Inst"), | ||
ForwardedFor: r.Header.Get("X-Tuc-Fwd-For"), | ||
}) | ||
if err != nil { | ||
log.Println("failed to execute template", err) | ||
} | ||
} | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", indexHandler) | ||
|
||
port := os.Getenv("PORT") | ||
if port == "" { | ||
log.Fatal("must provide PORT environment variable") | ||
} | ||
srv := &http.Server{ | ||
Addr: fmt.Sprintf(":%s", port), | ||
Handler: mux, | ||
} | ||
|
||
// Graceful shutdown | ||
done := make(chan os.Signal) | ||
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
go func() { | ||
<-done | ||
log.Println("shutting down server...") | ||
if err := srv.Shutdown(context.Background()); err != nil { | ||
log.Fatal("error during shutdown", err) | ||
} | ||
}() | ||
|
||
log.Printf("(%d) server listening at port %s", os.Getpid(), port) | ||
srv.ListenAndServe() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Tucano HTML Test</title> | ||
</head> | ||
<body> | ||
<h1>Hello, Tucano!</h1> | ||
<h2>Instance: <code>{{.Inst}}</code></h2> | ||
<h2>Client IP: <code>{{.ForwardedFor}}</code></h2> | ||
<hr /> | ||
<a href="https://github.com/esfericos/tucano"> | ||
https://github.com/esfericos/tucano | ||
</a> | ||
</body> | ||
</html> |