diff options
author | Mike Crute <mike@crute.us> | 2021-11-17 15:47:51 -0800 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2021-11-17 15:47:51 -0800 |
commit | 375654659ce828de4c24035c023c9ab551a37410 (patch) | |
tree | 45f4367a4532abb8a883df0842f7cd591e61f795 | |
parent | bcd64c17f857feda40fa4f1ba4e1aa910bbeb567 (diff) | |
download | cloud-identity-broker-375654659ce828de4c24035c023c9ab551a37410.tar.bz2 cloud-identity-broker-375654659ce828de4c24035c023c9ab551a37410.tar.xz cloud-identity-broker-375654659ce828de4c24035c023c9ab551a37410.zip |
Add version to headers
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | app/middleware/server_header.go | 16 | ||||
-rw-r--r-- | cmd/web/server.go | 7 | ||||
-rw-r--r-- | main.go | 4 |
4 files changed, 24 insertions, 5 deletions
@@ -2,7 +2,7 @@ IMAGE=docker.crute.me/aws-access | |||
2 | BINARY=cloud-identity-broker | 2 | BINARY=cloud-identity-broker |
3 | 3 | ||
4 | $(BINARY): main.go $(shell find . -name '*.go') | 4 | $(BINARY): main.go $(shell find . -name '*.go') |
5 | go build -o $@ $< | 5 | go build -ldflags "-X main.appVersion=$(shell git describe --long --tags --dirty --always)" -o $@ $< |
6 | 6 | ||
7 | .PHONY: vet | 7 | .PHONY: vet |
8 | vet: main.go | 8 | vet: main.go |
diff --git a/app/middleware/server_header.go b/app/middleware/server_header.go new file mode 100644 index 0000000..7ccbbda --- /dev/null +++ b/app/middleware/server_header.go | |||
@@ -0,0 +1,16 @@ | |||
1 | package middleware | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | |||
6 | "github.com/labstack/echo/v4" | ||
7 | ) | ||
8 | |||
9 | func AddServerHeader(version string) echo.MiddlewareFunc { | ||
10 | return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
11 | return func(c echo.Context) error { | ||
12 | c.Response().Header().Add("Server", fmt.Sprintf("cloud-identity-broker/%s", version)) | ||
13 | return next(c) | ||
14 | } | ||
15 | } | ||
16 | } | ||
diff --git a/cmd/web/server.go b/cmd/web/server.go index e76b6b2..d13cd58 100644 --- a/cmd/web/server.go +++ b/cmd/web/server.go | |||
@@ -25,12 +25,12 @@ import ( | |||
25 | "golang.org/x/time/rate" | 25 | "golang.org/x/time/rate" |
26 | ) | 26 | ) |
27 | 27 | ||
28 | func Register(root *cobra.Command, embeddedTemplates fs.FS) { | 28 | func Register(root *cobra.Command, embeddedTemplates fs.FS, version string) { |
29 | webCmd := &cobra.Command{ | 29 | webCmd := &cobra.Command{ |
30 | Use: "web [options]", | 30 | Use: "web [options]", |
31 | Short: "Run web server", | 31 | Short: "Run web server", |
32 | Run: func(c *cobra.Command, args []string) { | 32 | Run: func(c *cobra.Command, args []string) { |
33 | webMain(app.NewConfigFromCmd(c), embeddedTemplates) | 33 | webMain(app.NewConfigFromCmd(c), embeddedTemplates, version) |
34 | }, | 34 | }, |
35 | } | 35 | } |
36 | 36 | ||
@@ -53,7 +53,7 @@ func Register(root *cobra.Command, embeddedTemplates fs.FS) { | |||
53 | root.AddCommand(webCmd) | 53 | root.AddCommand(webCmd) |
54 | } | 54 | } |
55 | 55 | ||
56 | func webMain(cfg app.Config, embeddedTemplates fs.FS) { | 56 | func webMain(cfg app.Config, embeddedTemplates fs.FS, version string) { |
57 | ctx := context.Background() | 57 | ctx := context.Background() |
58 | 58 | ||
59 | s, err := glecho.NewDefaultEchoWithConfig(glecho.EchoConfig{ | 59 | s, err := glecho.NewDefaultEchoWithConfig(glecho.EchoConfig{ |
@@ -86,6 +86,7 @@ func webMain(cfg app.Config, embeddedTemplates fs.FS) { | |||
86 | if err = s.Init(); err != nil { | 86 | if err = s.Init(); err != nil { |
87 | log.Fatalf("Error initializing echo: %w", err) | 87 | log.Fatalf("Error initializing echo: %w", err) |
88 | } | 88 | } |
89 | s.Use(middleware.AddServerHeader(version)) | ||
89 | 90 | ||
90 | mongo, err := mongodb.Connect(ctx, cfg.MongoDbUri, cfg.MongodbVaultPath) | 91 | mongo, err := mongodb.Connect(ctx, cfg.MongoDbUri, cfg.MongodbVaultPath) |
91 | if err != nil { | 92 | if err != nil { |
@@ -17,6 +17,8 @@ import ( | |||
17 | //go:embed templates | 17 | //go:embed templates |
18 | var embeddedTemplates embed.FS | 18 | var embeddedTemplates embed.FS |
19 | 19 | ||
20 | var appVersion string | ||
21 | |||
20 | func main() { | 22 | func main() { |
21 | rootCmd := &cobra.Command{ | 23 | rootCmd := &cobra.Command{ |
22 | Use: "cloud-identity-broker", | 24 | Use: "cloud-identity-broker", |
@@ -37,7 +39,7 @@ func main() { | |||
37 | log.Fatalf("Error building sub-fs of embeded fs") | 39 | log.Fatalf("Error building sub-fs of embeded fs") |
38 | } | 40 | } |
39 | 41 | ||
40 | web.Register(rootCmd, templates) | 42 | web.Register(rootCmd, templates, appVersion) |
41 | 43 | ||
42 | if err := rootCmd.Execute(); err != nil { | 44 | if err := rootCmd.Execute(); err != nil { |
43 | log.Fatalf("Error running root command: %s", err) | 45 | log.Fatalf("Error running root command: %s", err) |