Skip to content

Commit c35b5a0

Browse files
committed
refactor: add helper func for generation Server url
1 parent 4291929 commit c35b5a0

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

openapi.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func declareAllTagsFromOperations(s *Server) {
9696
// To modify its behavior, use the [WithOpenAPIConfig] option.
9797
func (s *Server) OutputOpenAPISpec() openapi3.T {
9898
s.OpenAPI.Description().Servers = append(s.OpenAPI.Description().Servers, &openapi3.Server{
99-
URL: s.proto() + "://" + s.Addr,
99+
URL: s.url(),
100100
Description: "local server",
101101
})
102102

@@ -164,7 +164,7 @@ func (s *Server) registerOpenAPIRoutes(jsonSpec []byte) {
164164
w.Header().Set("Content-Type", "application/json")
165165
_, _ = w.Write(jsonSpec)
166166
})
167-
s.printOpenAPIMessage(fmt.Sprintf("JSON spec: %s://%s%s", s.proto(), s.Server.Addr, s.OpenAPIConfig.JsonUrl))
167+
s.printOpenAPIMessage(fmt.Sprintf("JSON spec: %s%s", s.url(), s.OpenAPIConfig.JsonUrl))
168168

169169
if !s.OpenAPIConfig.DisableSwaggerUI {
170170
Register(s, Route[any, any]{
@@ -173,7 +173,7 @@ func (s *Server) registerOpenAPIRoutes(jsonSpec []byte) {
173173
Path: s.OpenAPIConfig.SwaggerUrl + "/",
174174
},
175175
}, s.OpenAPIConfig.UIHandler(s.OpenAPIConfig.JsonUrl))
176-
s.printOpenAPIMessage(fmt.Sprintf("OpenAPI UI: %s://%s%s/index.html", s.proto(), s.Server.Addr, s.OpenAPIConfig.SwaggerUrl))
176+
s.printOpenAPIMessage(fmt.Sprintf("OpenAPI UI: %s%s/index.html", s.url(), s.OpenAPIConfig.SwaggerUrl))
177177
}
178178
}
179179

serve.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (s *Server) printStartupMessage() {
4343
if !s.disableStartupMessages {
4444
elapsed := time.Since(s.startTime)
4545
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")
46-
slog.Info("Server running ✅ on "+s.proto()+"://"+s.Server.Addr, "started in", elapsed.String())
46+
slog.Info("Server running ✅ on "+s.url(), "started in", elapsed.String())
4747
}
4848
}
4949

@@ -54,6 +54,10 @@ func (s *Server) proto() string {
5454
return "http"
5555
}
5656

57+
func (s *Server) url() string {
58+
return s.proto() + "://" + s.Server.Addr
59+
}
60+
5761
// initializes any Context type with the base ContextNoBody context.
5862
//
5963
// var ctx ContextWithBody[any] // does not work because it will create a ContextWithBody[any] with a nil value

0 commit comments

Comments
 (0)