Skip to content

Commit d7f6d25

Browse files
committed
feat: Adding renderer for favicon and static
Signed-off-by: Vincent Boutour <[email protected]>
1 parent dcd0f35 commit d7f6d25

18 files changed

+127
-25
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,14 @@ Usage of api:
8989
[prometheus] Shutdown Timeout {API_PROMETHEUS_SHUTDOWN_TIMEOUT} (default "5s")
9090
-prometheusWriteTimeout string
9191
[prometheus] Write Timeout {API_PROMETHEUS_WRITE_TIMEOUT} (default "10s")
92+
-publicURL string
93+
Public URL {API_PUBLIC_URL} (default "https://api.vibioh.fr")
9294
-readTimeout string
9395
[server] Read Timeout {API_READ_TIMEOUT} (default "5s")
9496
-shutdownTimeout string
9597
[server] Shutdown Timeout {API_SHUTDOWN_TIMEOUT} (default "10s")
98+
-title string
99+
Application title {API_TITLE} (default "I'm a teapot 🫖")
96100
-url string
97101
[alcotest] URL to check {API_URL}
98102
-userAgent string

cmd/goweb/api.go

+12-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"embed"
45
"flag"
56
"net/http"
67
"os"
@@ -17,6 +18,7 @@ import (
1718
"github.com/ViBiOh/httputils/v4/pkg/logger"
1819
"github.com/ViBiOh/httputils/v4/pkg/owasp"
1920
"github.com/ViBiOh/httputils/v4/pkg/prometheus"
21+
"github.com/ViBiOh/httputils/v4/pkg/renderer"
2022
"github.com/ViBiOh/httputils/v4/pkg/server"
2123
)
2224

@@ -26,27 +28,8 @@ const (
2628
delayPath = "/delay"
2729
)
2830

29-
var content = `
30-
<!doctype html>
31-
<html lang="en">
32-
<head>
33-
<meta charset="utf-8" />
34-
<meta name="format-detection" content="telephone=no">
35-
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
36-
37-
<title>I'm a teapot 🫖</title>
38-
<meta name="description" content="I'm a teapot 🫖">
39-
<meta property="og:title" content="I'm a teapot 🫖" />
40-
<meta property="og:description" content="I'm a teapot 🫖" />
41-
<meta property="og:type" content="website" />
42-
<meta property="og:url" content="https://api.vibioh.fr" />
43-
</head>
44-
45-
<body>
46-
<h1>I'm a teapot 🫖</h1>
47-
</body>
48-
</html>
49-
`
31+
//go:embed templates static
32+
var content embed.FS
5033

5134
func main() {
5235
fs := flag.NewFlagSet("api", flag.ExitOnError)
@@ -60,6 +43,7 @@ func main() {
6043
prometheusConfig := prometheus.Flags(fs, "prometheus")
6144
owaspConfig := owasp.Flags(fs, "")
6245
corsConfig := cors.Flags(fs, "cors")
46+
rendererConfig := renderer.Flags(fs, "", flags.NewOverride("PublicURL", "https://api.vibioh.fr"), flags.NewOverride("Title", "I'm a teapot 🫖"))
6347

6448
helloConfig := hello.Flags(fs, "")
6549

@@ -74,9 +58,15 @@ func main() {
7458
prometheusApp := prometheus.New(prometheusConfig)
7559
healthApp := health.New(healthConfig)
7660

61+
rendererApp, err := renderer.New(rendererConfig, content, nil)
62+
logger.Fatal(err)
63+
7764
helloHandler := http.StripPrefix(helloPath, hello.Handler(helloConfig))
7865
dumpHandler := http.StripPrefix(dumpPath, dump.Handler())
7966
delayHandler := http.StripPrefix(delayPath, delay.Handler())
67+
rendererHandler := rendererApp.Handler(func(r *http.Request) (string, int, map[string]interface{}, error) {
68+
return "public", http.StatusTeapot, nil, nil
69+
})
8070

8171
appHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8272
if strings.HasPrefix(r.URL.Path, helloPath) {
@@ -92,10 +82,7 @@ func main() {
9282
return
9383
}
9484

95-
w.WriteHeader(http.StatusTeapot)
96-
if _, err := w.Write([]byte(content)); err != nil {
97-
logger.Error("unable to write teapot: %s", err)
98-
}
85+
rendererHandler.ServeHTTP(w, r)
9986
})
10087

10188
go promServer.Start("prometheus", healthApp.End(), prometheusApp.Handler())
Loading
Loading
6.29 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<browserconfig>
3+
<msapplication>
4+
<tile>
5+
<square150x150logo src="/favicon/mstile-150x150.png?v={{ .Version }}"/>
6+
<TileColor>#ffc40d</TileColor>
7+
</tile>
8+
</msapplication>
9+
</browserconfig>
854 Bytes
Loading
1.46 KB
Loading

cmd/goweb/static/favicon/favicon.ico

14.7 KB
Binary file not shown.
5.97 KB
Loading
5.87 KB
Loading
6.39 KB
Loading
12.9 KB
Loading
4.13 KB
Loading
Loading
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "Teapot",
3+
"short_name": "Teapot",
4+
"icons": [
5+
{
6+
"src": "/favicon/android-chrome-192x192.png?v={{ .Version }}",
7+
"sizes": "192x192",
8+
"type": "image/png"
9+
},
10+
{
11+
"src": "/favicon/android-chrome-512x512.png?v={{ .Version }}",
12+
"sizes": "512x512",
13+
"type": "image/png"
14+
}
15+
],
16+
"theme_color": "#ffffff",
17+
"background_color": "#ffffff",
18+
"display": "standalone"
19+
}

cmd/goweb/templates/index.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{{ define "seo" }}
2+
{{ $description := "I'm a teapot 🫖" }}
3+
4+
<title>{{ .Title }}</title>
5+
<meta name="description" content="{{ $description }}">
6+
<meta property="og:title" content="{{ .Title }}" />
7+
<meta property="og:description" content="{{ $description }}" />
8+
<meta property="og:type" content="website" />
9+
<meta property="og:url" content="{{ .PublicURL }}" />
10+
<meta property="og:image" content="{{ .PublicURL }}/favicon/android-chrome-512x512.png" />
11+
<meta property="og:image:height" content="512" />
12+
<meta property="og:image:width" content="512" />
13+
{{ end }}
14+
15+
{{ define "favicon" }}
16+
<link rel="apple-touch-icon" sizes="180x180" href="/favicon/apple-touch-icon.png?v={{ .Version }}">
17+
<link rel="icon" type="image/png" sizes="32x32" href="/favicon/favicon-32x32.png?v={{ .Version }}">
18+
<link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png?v={{ .Version }}">
19+
<link rel="manifest" href="/favicon/site.webmanifest?v={{ .Version }}">
20+
<link rel="mask-icon" href="/favicon/safari-pinned-tab.svg?v={{ .Version }}" color="#5bbad5">
21+
<meta name="theme-color" content="#f8f8f8">
22+
{{ end}}
23+
24+
{{ define "public" }}
25+
<!doctype html>
26+
<html lang="en">
27+
<head>
28+
<meta charset="utf-8" />
29+
<meta name="format-detection" content="telephone=no">
30+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
31+
32+
{{ template "seo" . }}
33+
{{ template "favicon" . }}
34+
</head>
35+
36+
<body>
37+
<h1>I'm a teapot 🫖</h1>
38+
</body>
39+
</html>
40+
{{ end }}

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,11 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
263263
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
264264
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
265265
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
266+
github.com/tdewolff/minify/v2 v2.9.13 h1:RrwQhgGoYBhKN/ezStGB+crU64wPK1ZE5Jmkl63lif0=
266267
github.com/tdewolff/minify/v2 v2.9.13/go.mod h1:faNOp+awAoo+fhFHD+NAkBOaXBAvJI2X2SDERGKnARo=
268+
github.com/tdewolff/parse/v2 v2.5.10 h1:vj35n+ljq8LuYUx436s4qB18wuwP7thrLv+t1syE39M=
267269
github.com/tdewolff/parse/v2 v2.5.10/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho=
270+
github.com/tdewolff/test v1.0.6 h1:76mzYJQ83Op284kMT+63iCNCI7NEERsIN8dLM+RiKr4=
268271
github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
269272
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
270273
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=

0 commit comments

Comments
 (0)