110 lines
3.4 KiB
Go
110 lines
3.4 KiB
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"net/url"
|
||
|
"time"
|
||
|
|
||
|
"git.maronato.dev/maronato/goshort/internal/errs"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
DBTypeMemory = "memory"
|
||
|
DBTypeSQLite = "sqlite"
|
||
|
DBTypePostgres = "postgres"
|
||
|
)
|
||
|
|
||
|
var DBTypes = [...]string{
|
||
|
DBTypeMemory,
|
||
|
DBTypeSQLite,
|
||
|
DBTypePostgres,
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
// DefaultRedisURL is the default connection string for redis.
|
||
|
DefaultRedisURL = "redis://localhost:6379"
|
||
|
// DefaultDBType is the default type of database to use.
|
||
|
DefaultDBType = DBTypeSQLite
|
||
|
// DefaultDBURL is the default connection string for the database.
|
||
|
DefaultDBURL = "goshort.db"
|
||
|
// DefaultPort is the default port to listen on.
|
||
|
DefaultPort = "8080"
|
||
|
// DefaultHost is the default host to listen on.
|
||
|
DefaultHost = "0.0.0.0"
|
||
|
// DefaultUIPort is the default port to listen on for the UI.
|
||
|
DefaultUIPort = "3000"
|
||
|
// DefaultDebug is the default debug mode.
|
||
|
DefaultDebug = false
|
||
|
// DefaultSessionDuration is the default session duration.
|
||
|
DefaultSessionDuration = 7 * 24 * time.Hour
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
// ReadTimeout is the maximum duration for reading the entire
|
||
|
// request, including the body.
|
||
|
ReadTimeout = 5 * time.Second
|
||
|
// WriteTimeout is the maximum duration before timing out
|
||
|
// writes of the response.
|
||
|
WriteTimeout = 10 * time.Second
|
||
|
// IdleTimeout is the maximum amount of time to wait for the
|
||
|
// next request when keep-alives are enabled.
|
||
|
IdleTimeout = 30 * time.Second
|
||
|
// ReadHeaderTimeout is the amount of time allowed to read
|
||
|
// request headers.
|
||
|
ReadHeaderTimeout = 2 * time.Second
|
||
|
// RequestTimeout is the maximum duration for the entire
|
||
|
// request.
|
||
|
RequestTimeout = 7 * 24 * time.Hour
|
||
|
)
|
||
|
|
||
|
// Config defines the default configuration for the backend.
|
||
|
type Config struct {
|
||
|
// Prod is a flag that indicates if the server is running in production mode.
|
||
|
Prod bool
|
||
|
// Debug is a flag that indicates if the server is running in debug mode.
|
||
|
Debug bool `default:"false" mapstructure:"debug"`
|
||
|
// Host is the host to listen on.
|
||
|
Host string `default:"localhost" mapstructure:"host"`
|
||
|
// Port is the port to listen on.
|
||
|
Port string `default:"8080" mapstructure:"port"`
|
||
|
// UiPort is the port to listen on for the UI.
|
||
|
UIPort string `default:"3000" mapstructure:"ui-port"`
|
||
|
// RedisURL is the connection string for redis.
|
||
|
RedisURL string `default:"redis://localhost:6379" mapstructure:"redis-url"`
|
||
|
// DBType is the type of database to use.
|
||
|
DBType string `default:"sqlite" mapstructure:"db-type"`
|
||
|
// DBURL is the connection string for the database.
|
||
|
DBURL string `default:"shortener.db" mapstructure:"db-url"`
|
||
|
// SessionDuration is the duration of the session.
|
||
|
SessionDuration time.Duration `default:"168h" mapstructure:"session-duration"`
|
||
|
}
|
||
|
|
||
|
func Validate(cfg *Config) error {
|
||
|
// Host and port have to be valid.
|
||
|
if _, err := url.ParseRequestURI("http://" + net.JoinHostPort(cfg.Host, cfg.Port)); err != nil {
|
||
|
return errs.Error(errs.ErrInvalidConfig, fmt.Sprintf("invalid host and/or port: %s", err))
|
||
|
}
|
||
|
// UI port has to be valid.
|
||
|
if cfg.UIPort != "" {
|
||
|
if _, err := url.ParseRequestURI("http://" + net.JoinHostPort(cfg.Host, cfg.UIPort)); err != nil {
|
||
|
return errs.Error(errs.ErrInvalidConfig, fmt.Sprintf("invalid UI port: %s", err))
|
||
|
}
|
||
|
}
|
||
|
if cfg.DBType != "" {
|
||
|
// DB type has to be valid.
|
||
|
valid := false
|
||
|
for _, dbType := range DBTypes {
|
||
|
if cfg.DBType == dbType {
|
||
|
valid = true
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if !valid {
|
||
|
return errs.Error(errs.ErrInvalidConfig, fmt.Sprintf("invalid database type: %s", cfg.DBType))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|