-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat(server/v2): Add Swagger UI support for server/v2 #23092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
4ed5650
2ac2555
8930230
c812722
d0c20bc
ac5982a
f96dafe
9859c38
e96e5bb
e733362
34eab36
eec89d7
654cc99
c9d6544
16556c1
2021911
93dc4b6
446c0f8
c038d6c
0938d61
2466ab5
8a042b2
8ccb884
9659ba7
8f3017c
0fa0b6b
35f2fad
44da58f
98f2d85
a3dbbe8
d88e065
54fe590
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package swagger | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "cosmossdk.io/core/server" | ||
| ) | ||
|
|
||
| const ServerName = "swagger" | ||
|
|
||
| // Config defines the configuration for the Swagger UI server | ||
| type Config struct { | ||
| Enable bool `toml:"enable" mapstructure:"enable"` | ||
| Address string `toml:"address" mapstructure:"address"` | ||
| Path string `toml:"path" mapstructure:"path"` | ||
| } | ||
|
|
||
| // DefaultConfig returns the default configuration | ||
| func DefaultConfig() *Config { | ||
| return &Config{ | ||
| Enable: true, | ||
| Address: "localhost:8080", | ||
| Path: "/swagger/", | ||
|
||
| } | ||
| } | ||
|
|
||
| // Validate checks the configuration | ||
| func (c Config) Validate() error { | ||
| if c.Path == "" { | ||
| return fmt.Errorf("swagger path cannot be empty") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // CfgOption defines a function for configuring the settings | ||
| type CfgOption func(*Config) | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,85 @@ | ||||||||||||||||||||||||||||||||||||||
| package swagger | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| "github.com/rakyll/statik/fs" | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Handler returns an HTTP handler for Swagger UI | ||||||||||||||||||||||||||||||||||||||
| func Handler() http.Handler { | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| return &swaggerHandler{} | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| type swaggerHandler struct{} | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| func (h *swaggerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||||||||||||||||||||
| // Set CORS headers | ||||||||||||||||||||||||||||||||||||||
| w.Header().Set("Access-Control-Allow-Origin", "*") | ||||||||||||||||||||||||||||||||||||||
| w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS") | ||||||||||||||||||||||||||||||||||||||
| w.Header().Set("Access-Control-Allow-Headers", "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if r.Method == http.MethodOptions { | ||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Get the static file system | ||||||||||||||||||||||||||||||||||||||
| statikFS, err := fs.New() | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Process the path | ||||||||||||||||||||||||||||||||||||||
| urlPath := strings.TrimPrefix(r.URL.Path, "/swagger") | ||||||||||||||||||||||||||||||||||||||
| if urlPath == "" || urlPath == "/" { | ||||||||||||||||||||||||||||||||||||||
| urlPath = "/index.html" | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Open the file | ||||||||||||||||||||||||||||||||||||||
| file, err := statikFS.Open(urlPath) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| http.Error(w, "File not found", http.StatusNotFound) | ||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| defer file.Close() | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Set the content-type | ||||||||||||||||||||||||||||||||||||||
| ext := filepath.Ext(urlPath) | ||||||||||||||||||||||||||||||||||||||
| if ct := getContentType(ext); ct != "" { | ||||||||||||||||||||||||||||||||||||||
| w.Header().Set("Content-Type", ct) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Set caching headers | ||||||||||||||||||||||||||||||||||||||
| w.Header().Set("Cache-Control", "public, max-age=31536000") | ||||||||||||||||||||||||||||||||||||||
| w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat)) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Serve the file | ||||||||||||||||||||||||||||||||||||||
| http.ServeContent(w, r, urlPath, time.Now(), file.(io.ReadSeeker)) | ||||||||||||||||||||||||||||||||||||||
Check warningCode scanning / CodeQL Calling the system time Warning
Calling the system time may be a possible source of non-determinism
|
||||||||||||||||||||||||||||||||||||||
| // Set caching headers | |
| w.Header().Set("Cache-Control", "public, max-age=31536000") | |
| w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat)) | |
| // Serve the file | |
| http.ServeContent(w, r, urlPath, time.Now(), file.(io.ReadSeeker)) | |
| // Set caching headers | |
| w.Header().Set("Cache-Control", "public, max-age=31536000") | |
| // Use a fixed timestamp for deterministic behavior | |
| w.Header().Set("Last-Modified", "Mon, 01 Jan 2024 00:00:00 GMT") | |
| // Add security headers | |
| w.Header().Set("X-Content-Type-Options", "nosniff") | |
| w.Header().Set("X-Frame-Options", "DENY") | |
| w.Header().Set("Content-Security-Policy", "default-src 'self'") | |
| // Serve the file | |
| http.ServeContent(w, r, urlPath, time.Unix(0, 0), file.(io.ReadSeeker)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package swagger | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "cosmossdk.io/core/server" | ||
| "cosmossdk.io/core/transaction" | ||
| "cosmossdk.io/log" | ||
| serverv2 "cosmossdk.io/server/v2" | ||
| ) | ||
|
|
||
| var ( | ||
| _ serverv2.ServerComponent[transaction.Tx] = (*Server[transaction.Tx])(nil) | ||
| _ serverv2.HasConfig = (*Server[transaction.Tx])(nil) | ||
| ) | ||
|
|
||
| // Server represents a Swagger UI server | ||
| type Server[T transaction.Tx] struct { | ||
| logger log.Logger | ||
| config *Config | ||
| cfgOptions []CfgOption | ||
| server *http.Server | ||
| } | ||
|
|
||
| // New creates a new Swagger UI server | ||
| func New[T transaction.Tx]( | ||
| logger log.Logger, | ||
| cfg server.ConfigMap, | ||
| cfgOptions ...CfgOption, | ||
| ) (*Server[T], error) { | ||
| srv := &Server[T]{ | ||
| logger: logger.With(log.ModuleKey, ServerName), | ||
| cfgOptions: cfgOptions, | ||
| } | ||
|
|
||
| serverCfg := srv.Config().(*Config) | ||
julienrbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if len(cfg) > 0 { | ||
| if err := serverv2.UnmarshalSubConfig(cfg, srv.Name(), &serverCfg); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal config: %w", err) | ||
| } | ||
| } | ||
| srv.config = serverCfg | ||
|
|
||
| if err := srv.config.Validate(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| mux := http.NewServeMux() | ||
| mux.Handle(srv.config.Path, Handler()) | ||
|
|
||
| srv.server = &http.Server{ | ||
| Addr: srv.config.Address, | ||
| Handler: mux, | ||
| } | ||
|
|
||
| return srv, nil | ||
| } | ||
|
|
||
| // NewWithConfigOptions creates a new server with configuration options | ||
| func NewWithConfigOptions[T transaction.Tx](opts ...CfgOption) *Server[T] { | ||
| return &Server[T]{ | ||
| cfgOptions: opts, | ||
| } | ||
| } | ||
|
|
||
| // Name returns the server's name | ||
| func (s *Server[T]) Name() string { | ||
| return ServerName | ||
| } | ||
|
|
||
| // Config returns the server configuration | ||
| func (s *Server[T]) Config() any { | ||
| if s.config == nil || s.config.Address == "" { | ||
| cfg := DefaultConfig() | ||
| for _, opt := range s.cfgOptions { | ||
| opt(cfg) | ||
| } | ||
| return cfg | ||
| } | ||
| return s.config | ||
| } | ||
|
|
||
| // Start starts the server | ||
| func (s *Server[T]) Start(ctx context.Context) error { | ||
| if !s.config.Enable { | ||
| s.logger.Info(fmt.Sprintf("%s server is disabled via config", s.Name())) | ||
| return nil | ||
| } | ||
|
|
||
| s.logger.Info("starting swagger server...", "address", s.config.Address) | ||
| if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
| return fmt.Errorf("failed to start swagger server: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Stop stops the server | ||
| func (s *Server[T]) Stop(ctx context.Context) error { | ||
| if !s.config.Enable { | ||
| return nil | ||
| } | ||
|
|
||
| s.logger.Info("stopping swagger server...", "address", s.config.Address) | ||
| return s.server.Shutdown(ctx) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use something else than 8080 by default