-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#41: Exposed the basic HTTP server configs
- Loading branch information
1 parent
d4e6c84
commit e1bceef
Showing
2 changed files
with
46 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,65 @@ | ||
package http | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/cloudwego/hertz/pkg/common/config" | ||
|
||
"github.com/cloudwego/hertz/pkg/app/server" | ||
"github.com/cloudwego/hertz/pkg/network/netpoll" | ||
) | ||
|
||
type ServerConfig struct { | ||
HostPort string | ||
Host string `yaml:"host"` | ||
Port int `yaml:"port"` | ||
ReadTimeout *time.Duration `yaml:"read_timeout"` | ||
WriteTimeout *time.Duration `yaml:"write_timeout"` | ||
IdleTimeout *time.Duration `yaml:"idle_timeout"` | ||
MaxRequestBodySize *int `yaml:"max_request_body_size"` | ||
} | ||
|
||
func DefaultServerConfig() *ServerConfig { | ||
maxReqBodySize := 4 * 1024 * 1024 | ||
readTimeout := 3 * time.Second | ||
writeTimeout := 3 * time.Second | ||
idleTimeout := 1 * time.Second | ||
|
||
return &ServerConfig{ | ||
HostPort: "127.0.0.1:9099", | ||
Host: "127.0.0.1", | ||
Port: 9099, | ||
IdleTimeout: &idleTimeout, | ||
ReadTimeout: &readTimeout, | ||
WriteTimeout: &writeTimeout, | ||
MaxRequestBodySize: &maxReqBodySize, | ||
} | ||
} | ||
|
||
func (cfg *ServerConfig) Address() string { | ||
return fmt.Sprintf("%s:%v", cfg.Host, cfg.Port) | ||
} | ||
|
||
func (cfg *ServerConfig) ToServer() *server.Hertz { | ||
// TODO: do real server build based on provided config | ||
return server.Default( | ||
server.WithIdleTimeout(1*time.Second), | ||
server.WithHostPorts(cfg.HostPort), | ||
server.WithMaxRequestBodySize(20<<20), | ||
serverOptions := []config.Option{ | ||
server.WithHostPorts(cfg.Address()), | ||
server.WithTransport(netpoll.NewTransporter), | ||
) | ||
} | ||
|
||
if cfg.IdleTimeout != nil { | ||
serverOptions = append(serverOptions, server.WithIdleTimeout(*cfg.IdleTimeout)) | ||
} | ||
|
||
if cfg.ReadTimeout != nil { | ||
serverOptions = append(serverOptions, server.WithReadTimeout(*cfg.ReadTimeout)) | ||
} | ||
|
||
if cfg.WriteTimeout != nil { | ||
serverOptions = append(serverOptions, server.WithWriteTimeout(*cfg.WriteTimeout)) | ||
} | ||
|
||
if cfg.MaxRequestBodySize != nil { | ||
serverOptions = append(serverOptions, server.WithMaxRequestBodySize(*cfg.MaxRequestBodySize)) | ||
} | ||
|
||
return server.Default(serverOptions...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters