Skip to content

Commit 6f3a397

Browse files
dylanhittEwenQuim
authored andcommitted
chore: refactor server Runs to use .setup(), add test for middleware clause
1 parent 79d2c63 commit 6f3a397

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

openapi.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,8 @@ func (s *Server) registerOpenAPIRoutes(jsonSpec []byte) {
120120
Path: s.OpenAPIConfig.SwaggerUrl + "/",
121121
}, s.OpenAPIConfig.UIHandler(s.OpenAPIConfig.JsonUrl))
122122

123-
proto := "http"
124-
if s.isTLS {
125-
proto = "https"
126-
}
127-
s.printOpenAPIMessage(fmt.Sprintf("JSON spec: %s://%s%s", proto, s.Server.Addr, s.OpenAPIConfig.JsonUrl))
128-
s.printOpenAPIMessage(fmt.Sprintf("OpenAPI UI: %s://%s%s/index.html", proto, s.Server.Addr, s.OpenAPIConfig.SwaggerUrl))
123+
s.printOpenAPIMessage(fmt.Sprintf("JSON spec: %s://%s%s", s.proto(), s.Server.Addr, s.OpenAPIConfig.JsonUrl))
124+
s.printOpenAPIMessage(fmt.Sprintf("OpenAPI UI: %s://%s%s/index.html", s.proto(), s.Server.Addr, s.OpenAPIConfig.SwaggerUrl))
129125
}
130126

131127
func (s *Server) printOpenAPIMessage(msg string) {

serve.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,7 @@ import (
1414
// It returns an error if the server could not start (it could not bind to the port for example).
1515
// It also generates the OpenAPI spec and outputs it to a file, the UI, and a handler (if enabled).
1616
func (s *Server) Run() error {
17-
go s.OutputOpenAPISpec()
18-
19-
s.printStartupMessage()
20-
21-
s.Server.Handler = s.Mux
22-
if s.corsMiddleware != nil {
23-
s.Server.Handler = s.corsMiddleware(s.Server.Handler)
24-
}
25-
17+
s.setup()
2618
return s.Server.ListenAndServe()
2719
}
2820

@@ -32,28 +24,34 @@ func (s *Server) Run() error {
3224
// It also generates the OpenAPI spec and outputs it to a file, the UI, and a handler (if enabled).
3325
func (s *Server) RunTLS(certFile, keyFile string) error {
3426
s.isTLS = true
35-
go s.OutputOpenAPISpec()
3627

28+
s.setup()
29+
return s.Server.ListenAndServeTLS(certFile, keyFile)
30+
}
31+
32+
func (s *Server) setup() {
33+
go s.OutputOpenAPISpec()
3734
s.printStartupMessage()
3835

3936
s.Server.Handler = s.Mux
4037
if s.corsMiddleware != nil {
4138
s.Server.Handler = s.corsMiddleware(s.Server.Handler)
4239
}
43-
44-
return s.Server.ListenAndServeTLS(certFile, keyFile)
4540
}
4641

4742
func (s *Server) printStartupMessage() {
4843
if !s.disableStartupMessages {
4944
elapsed := time.Since(s.startTime)
5045
slog.Debug("Server started in "+elapsed.String(), "info", "time between since server creation (fuego.NewServer) and server startup (fuego.Run). Depending on your implementation, there might be things that do not depend on fuego slowing start time")
51-
proto := "http"
52-
if s.isTLS {
53-
proto = "https"
54-
}
55-
slog.Info("Server running ✅ on "+proto+"://"+s.Server.Addr, "started in", elapsed.String())
46+
slog.Info("Server running ✅ on "+s.proto()+"://"+s.Server.Addr, "started in", elapsed.String())
47+
}
48+
}
49+
50+
func (s *Server) proto() string {
51+
if s.isTLS {
52+
return "https"
5653
}
54+
return "http"
5755
}
5856

5957
// initializes any Context type with the base ContextNoBody context.

serve_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020
"time"
2121

22+
"github.com/rs/cors"
2223
"github.com/stretchr/testify/require"
2324
)
2425

@@ -391,6 +392,10 @@ func TestServer_Run(t *testing.T) {
391392
t.Run("can run server", func(t *testing.T) {
392393
s := NewServer(
393394
WithoutLogger(),
395+
WithCorsMiddleware(cors.New(cors.Options{
396+
AllowedOrigins: []string{"*"},
397+
AllowedMethods: []string{"GET"},
398+
}).Handler),
394399
)
395400

396401
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {

0 commit comments

Comments
 (0)