Skip to content

Commit

Permalink
feat(tests): add html-app (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
lffg authored Jun 21, 2024
1 parent 4342858 commit cecca6a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/containers/html/Dockerfile
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"]
67 changes: 67 additions & 0 deletions tests/containers/html/main.go
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()
}
15 changes: 15 additions & 0 deletions tests/containers/html/tpl/index.html
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>

0 comments on commit cecca6a

Please sign in to comment.