goshort/internal/config/config.go
2023-08-26 11:47:46 -03:00

117 lines
3.2 KiB
Go

package config
import (
"fmt"
"net"
"net/url"
"time"
"git.maronato.dev/maronato/goshort/internal/errs"
)
const (
DBTypeMemory = "memory"
DBTypeSQLite = "sqlite"
DBTypePostgres = "postgres"
)
func GetDBTypeList() []string {
return []string{
DBTypeMemory,
DBTypeSQLite,
}
}
const (
// DefaultProd is the default mode.
DefaultProd = false
// 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. This is set to 0.0.0.0 when running in Docker.
DefaultHost = "localhost"
// 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
// DefaultDisableRegistration is the default value for diable registration.
DefaultDisableRegistration = false
)
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
// Host is the host to listen on.
Host string
// Port is the port to listen on.
Port string
// UiPort is the port to listen on for the UI.
UIPort string
// DBType is the type of database to use.
DBType string
// DBURL is the connection string for the database.
DBURL string
// SessionDuration is the duration of the session.
SessionDuration time.Duration
// DisableRegistration defines whether or not registration are disabled.
DisableRegistration bool
}
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.Errorf(fmt.Sprintf("invalid host and/or port: %s", err), errs.ErrInvalidConfig)
}
// UI port has to be valid.
if cfg.UIPort != "" {
if _, err := url.ParseRequestURI("http://" + net.JoinHostPort(cfg.Host, cfg.UIPort)); err != nil {
return errs.Errorf(fmt.Sprintf("invalid UI port: %s", err), errs.ErrInvalidConfig)
}
}
if cfg.DBType != "" {
// DB type has to be valid.
valid := false
for _, dbType := range GetDBTypeList() {
if cfg.DBType == dbType {
valid = true
break
}
}
if !valid {
return errs.Errorf(fmt.Sprintf("invalid database type: %s", cfg.DBType), errs.ErrInvalidConfig)
}
}
return nil
}