models

package
v0.0.0-...-a2066d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2024 License: Unlicense Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrNoTokenSpecified = ConstantError("no token specified")
	ErrInvalidToken     = ConstantError("invalid token")
	ErrTokenExpired     = ConstantError("token expired")
	ErrInvalidJson      = ConstantError("invalid JSON")
	ErrTokenNotExists   = ConstantError("token does not exist")
	ErrInvalidQuery     = ConstantError("invalid query parameters")
	ErrInvalidPassword  = ConstantError("invalid password")
)

constant errors because i don't like sentinel errors

Variables

This section is empty.

Functions

func ErrFromResponse

func ErrFromResponse(response *http.Response) error

parse error from json {"error":"error"} or error string nil if error is not parsed Function does not close response body

func ErrToMap

func ErrToMap(err errorConverter) map[string]interface{}

func SetupDatabase

func SetupDatabase(config *DatabaseConfig) (*sql.DB, error)

func TruncateDatabase

func TruncateDatabase(db *sql.DB) error

func ValidatePassword

func ValidatePassword(password string, passwordHash, alg string) error

Types

type AccessTokenCfg

type AccessTokenCfg struct {
	LifespanCfg
	Secret string `validate:"required"`
}

type AppConfig

type AppConfig struct {
	Port string `validate:"required"`
	// http.Client timeout
	TimeoutSeconds uint `toml:"timeout_seconds" validate:"required"`
}

type ClientMock

type ClientMock struct {
	Engine   *gin.Engine
	Recorder *httptest.ResponseRecorder
}

func NewClientMock

func NewClientMock() *ClientMock

func (*ClientMock) Get

func (m *ClientMock) Get(url string) (resp *http.Response, err error)

func (*ClientMock) Post

func (m *ClientMock) Post(url string, contentType string, body io.Reader) (*http.Response, error)

type ConstantError

type ConstantError string

func (ConstantError) Error

func (e ConstantError) Error() string

type DatabaseConfig

type DatabaseConfig struct {
	Driver   string `validate:"required"`
	Host     string `validate:"required"`
	User     string `validate:"required"`
	Password string `validate:"required"`
	Name     string `toml:"database" validate:"required"`
	Port     string `validate:"required"`
}

func (*DatabaseConfig) GetUrl

func (c *DatabaseConfig) GetUrl() string

type ExternalServiceError

type ExternalServiceError struct {
	Status int
	Msg    string
}

I want to return the same status code and message as the external service

func NewExternalServiceError

func NewExternalServiceError(status int) *ExternalServiceError

func (*ExternalServiceError) Error

func (e *ExternalServiceError) Error() string

func (*ExternalServiceError) Is

func (e *ExternalServiceError) Is(err error) bool

type ExternalServicesConfig

type ExternalServicesConfig struct {
	Guest GuestService `validate:"required"`
	Login LoginService `validate:"required"`
}

type Guest

type Guest struct {
	Id   int64  `json:"id,omitempty"`
	Name string `json:"name,omitempty"`
}

func RegisterGuest

func RegisterGuest(fullname string, adress url.URL, client HTTPClient) (*Guest, error)

model.ExternalServiceError error returned if error is parsed from external service response

type GuestService

type GuestService struct {
	Host string `validate:"required"`
	Port string `validate:"required"`
	Path string `validate:"required"`
}

type HTTPClient

type HTTPClient interface {
	Post(url string, contentType string, body io.Reader) (*http.Response, error)
	Get(url string) (resp *http.Response, err error)
}

Little interface so i can mock some requests

type LifespanCfg

type LifespanCfg struct {
	LifespanMinute uint `toml:"lifespan_minute"`
	LifespanHour   uint `toml:"lifespan_hour"`
	LifespanDay    uint `toml:"lifespan_day"`
	// contains filtered or unexported fields
}

func (*LifespanCfg) Lifespan

func (c *LifespanCfg) Lifespan() time.Duration

Just calculates time.Now() + lifespan

type LoginRequest

type LoginRequest struct {
	Username string
	Email    string
}

type LoginResponce

type LoginResponce struct {
	Id           *int64   `json:"id" validate:"required"`
	Roles        []string `json:"roles" validate:"required"`
	PasswordHash string   `json:"password" validate:"required"`
	HashAlg      string   `json:"alg" validate:"required"`
}

type LoginService

type LoginService struct {
	Host string `validate:"required"`
	Port string `validate:"required"`
	Path string `validate:"required"`
}

type RefreshTokenCfg

type RefreshTokenCfg struct {
	LifespanCfg
	Secret string `validate:"required"`
}

type TokensCfg

type TokensCfg struct {
	Access  AccessTokenCfg  `validate:"required"`
	Refresh RefreshTokenCfg `validate:"required"`
}

type TomlConfig

type TomlConfig struct {
	App      AppConfig              `validate:"required"`
	Database DatabaseConfig         `validate:"required"`
	Tokens   TokensCfg              `validate:"required"`
	Services ExternalServicesConfig `validate:"required"`
}

func ParseConfig

func ParseConfig(filename string) (*TomlConfig, error)

Parses and validates config

func (*TomlConfig) Validate

func (*TomlConfig) Validate() error

type User

type User struct {
	Id       int64
	Username string
	Password string
	Email    string
	Roles    []string
}

func LoginUser

func LoginUser(client HTTPClient, adress url.URL, username string, password string, email string) (*User, error)

Should return *User struct with all fields filled.

returns an error if there are no roles or ID in the external service response

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL