2021-09-22 06:47:51 +02:00
|
|
|
package web
|
2020-11-20 23:40:57 +01:00
|
|
|
|
|
|
|
import (
|
2023-04-22 18:12:56 +02:00
|
|
|
"crypto/tls"
|
2023-04-22 21:22:09 +02:00
|
|
|
"errors"
|
2020-11-20 23:40:57 +01:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2021-09-22 06:47:51 +02:00
|
|
|
const (
|
2021-10-23 22:47:12 +02:00
|
|
|
// DefaultAddress is the default address the application will bind to
|
2021-09-22 06:47:51 +02:00
|
|
|
DefaultAddress = "0.0.0.0"
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// DefaultPort is the default port the application will listen on
|
2021-09-22 06:47:51 +02:00
|
|
|
DefaultPort = 8080
|
2024-02-08 00:54:30 +01:00
|
|
|
|
|
|
|
// DefaultReadBufferSize is the default value for ReadBufferSize
|
|
|
|
DefaultReadBufferSize = 8192
|
|
|
|
|
|
|
|
// MinimumReadBufferSize is the minimum value for ReadBufferSize, and also the default value set
|
|
|
|
// for fiber.Config.ReadBufferSize
|
|
|
|
MinimumReadBufferSize = 4096
|
2021-09-22 06:47:51 +02:00
|
|
|
)
|
|
|
|
|
2024-02-08 00:54:30 +01:00
|
|
|
// Config is the structure which supports the configuration of the server listening to requests
|
2021-09-22 06:47:51 +02:00
|
|
|
type Config struct {
|
2020-11-20 23:40:57 +01:00
|
|
|
// Address to listen on (defaults to 0.0.0.0 specified by DefaultAddress)
|
|
|
|
Address string `yaml:"address"`
|
|
|
|
|
|
|
|
// Port to listen on (default to 8080 specified by DefaultPort)
|
|
|
|
Port int `yaml:"port"`
|
2023-04-22 18:12:56 +02:00
|
|
|
|
2024-02-08 00:54:30 +01:00
|
|
|
// ReadBufferSize sets fiber.Config.ReadBufferSize, which is the buffer size for reading requests coming from a
|
|
|
|
// single connection and also acts as a limit for the maximum header size.
|
|
|
|
//
|
|
|
|
// If you're getting occasional "Request Header Fields Too Large", you may want to try increasing this value.
|
|
|
|
//
|
|
|
|
// Defaults to DefaultReadBufferSize
|
|
|
|
ReadBufferSize int `yaml:"read-buffer-size,omitempty"`
|
|
|
|
|
2023-04-22 21:22:09 +02:00
|
|
|
// TLS configuration (optional)
|
|
|
|
TLS *TLSConfig `yaml:"tls,omitempty"`
|
2023-04-22 18:12:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type TLSConfig struct {
|
2023-04-22 21:22:09 +02:00
|
|
|
// CertificateFile is the public certificate for TLS in PEM format.
|
2023-04-22 18:12:56 +02:00
|
|
|
CertificateFile string `yaml:"certificate-file,omitempty"`
|
|
|
|
|
2023-04-22 21:22:09 +02:00
|
|
|
// PrivateKeyFile is the private key file for TLS in PEM format.
|
2023-04-22 18:12:56 +02:00
|
|
|
PrivateKeyFile string `yaml:"private-key-file,omitempty"`
|
2020-11-20 23:40:57 +01:00
|
|
|
}
|
|
|
|
|
2021-09-22 06:47:51 +02:00
|
|
|
// GetDefaultConfig returns a Config struct with the default values
|
|
|
|
func GetDefaultConfig() *Config {
|
2024-02-08 00:54:30 +01:00
|
|
|
return &Config{
|
|
|
|
Address: DefaultAddress,
|
|
|
|
Port: DefaultPort,
|
|
|
|
ReadBufferSize: DefaultReadBufferSize,
|
|
|
|
}
|
2021-09-11 07:51:14 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 06:47:51 +02:00
|
|
|
// ValidateAndSetDefaults validates the web configuration and sets the default values if necessary.
|
|
|
|
func (web *Config) ValidateAndSetDefaults() error {
|
2020-11-21 23:35:08 +01:00
|
|
|
// Validate the Address
|
2020-11-20 23:40:57 +01:00
|
|
|
if len(web.Address) == 0 {
|
|
|
|
web.Address = DefaultAddress
|
|
|
|
}
|
2020-11-21 23:35:08 +01:00
|
|
|
// Validate the Port
|
2020-11-20 23:40:57 +01:00
|
|
|
if web.Port == 0 {
|
|
|
|
web.Port = DefaultPort
|
|
|
|
} else if web.Port < 0 || web.Port > math.MaxUint16 {
|
2021-05-19 04:29:15 +02:00
|
|
|
return fmt.Errorf("invalid port: value should be between %d and %d", 0, math.MaxUint16)
|
2020-11-20 23:40:57 +01:00
|
|
|
}
|
2024-02-08 00:54:30 +01:00
|
|
|
// Validate ReadBufferSize
|
|
|
|
if web.ReadBufferSize == 0 {
|
|
|
|
web.ReadBufferSize = DefaultReadBufferSize // Not set? Use the default value.
|
|
|
|
} else if web.ReadBufferSize < MinimumReadBufferSize {
|
|
|
|
web.ReadBufferSize = MinimumReadBufferSize // Below the minimum? Use the minimum value.
|
|
|
|
}
|
2023-04-22 18:12:56 +02:00
|
|
|
// Try to load the TLS certificates
|
2023-04-22 21:22:09 +02:00
|
|
|
if web.TLS != nil {
|
2023-07-21 01:02:34 +02:00
|
|
|
if err := web.TLS.isValid(); err != nil {
|
2023-04-22 21:22:09 +02:00
|
|
|
return fmt.Errorf("invalid tls config: %w", err)
|
|
|
|
}
|
2023-04-22 18:12:56 +02:00
|
|
|
}
|
2021-05-19 04:29:15 +02:00
|
|
|
return nil
|
2020-11-20 23:40:57 +01:00
|
|
|
}
|
|
|
|
|
2023-07-21 01:02:34 +02:00
|
|
|
func (web *Config) HasTLS() bool {
|
|
|
|
return web.TLS != nil && len(web.TLS.CertificateFile) > 0 && len(web.TLS.PrivateKeyFile) > 0
|
|
|
|
}
|
|
|
|
|
2020-11-20 23:40:57 +01:00
|
|
|
// SocketAddress returns the combination of the Address and the Port
|
2021-09-22 06:47:51 +02:00
|
|
|
func (web *Config) SocketAddress() string {
|
2020-11-20 23:40:57 +01:00
|
|
|
return fmt.Sprintf("%s:%d", web.Address, web.Port)
|
|
|
|
}
|
2023-04-22 18:12:56 +02:00
|
|
|
|
2023-07-21 01:02:34 +02:00
|
|
|
func (t *TLSConfig) isValid() error {
|
2023-04-22 21:22:09 +02:00
|
|
|
if len(t.CertificateFile) > 0 && len(t.PrivateKeyFile) > 0 {
|
2023-07-21 01:02:34 +02:00
|
|
|
_, err := tls.LoadX509KeyPair(t.CertificateFile, t.PrivateKeyFile)
|
2023-04-22 21:22:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-04-22 18:12:56 +02:00
|
|
|
}
|
2023-04-22 21:22:09 +02:00
|
|
|
return errors.New("certificate-file and private-key-file must be specified")
|
2023-04-22 18:12:56 +02:00
|
|
|
}
|