bouncer/internal/store/redis/proxy_item.go

63 lines
1.2 KiB
Go
Raw Normal View History

2023-04-24 20:52:12 +02:00
package redis
import (
"time"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
)
type proxyHeaderItem struct {
Name string `redis:"name"`
Revision int `redis:"revision"`
2023-04-24 20:52:12 +02:00
Weight int `redis:"weight"`
Enabled bool `redis:"enabled"`
CreatedAt *jsonWrapper[time.Time] `redis:"created_at"`
UpdatedAt *jsonWrapper[time.Time] `redis:"updated_at"`
}
func (i *proxyHeaderItem) ToProxyHeader() (*store.ProxyHeader, error) {
proxyHeader := &store.ProxyHeader{
Name: store.ProxyName(i.Name),
Revision: i.Revision,
Weight: i.Weight,
Enabled: i.Enabled,
2023-04-24 20:52:12 +02:00
}
return proxyHeader, nil
}
type proxyItem struct {
proxyHeaderItem
To string `redis:"to"`
From *jsonWrapper[[]string] `redis:"from"`
}
func (i *proxyItem) ToProxy() (*store.Proxy, error) {
proxyHeader, err := i.proxyHeaderItem.ToProxyHeader()
if err != nil {
return nil, errors.WithStack(err)
}
proxy := &store.Proxy{
ProxyHeader: *proxyHeader,
To: i.To,
}
if i.CreatedAt != nil {
proxy.CreatedAt = i.CreatedAt.Value()
}
if i.UpdatedAt != nil {
proxy.UpdatedAt = i.UpdatedAt.Value()
}
if i.From != nil {
proxy.From = i.From.Value()
}
return proxy, nil
}