zitadel/internal/cache/config/config.go
Livio Amstutz 389eb4a27a
feat: run on a single port (#3163)
* start v2

* start

* run

* some cleanup

* remove v2 pkg again

* simplify

* webauthn

* remove unused config

* fix login path in Dockerfile

* fix asset_generator.go

* health handler

* fix grpc web

* refactor

* merge

* build new main.go

* run new main.go

* update logging pkg

* fix error msg

* update logging

* cleanup

* cleanup

* go mod tidy

* change localDevMode

* fix customEndpoints

* update logging

* comments

* change local flag to external configs

* fix location generated go code

* fix

Co-authored-by: fforootd <florian@caos.ch>
2022-02-14 17:22:30 +01:00

60 lines
1.3 KiB
Go

package config
import (
"encoding/json"
"github.com/caos/zitadel/internal/cache"
"github.com/caos/zitadel/internal/cache/bigcache"
"github.com/caos/zitadel/internal/cache/fastcache"
"github.com/caos/zitadel/internal/errors"
)
type CacheConfig struct {
Type string
Config cache.Config
}
var caches = map[string]func() cache.Config{
"bigcache": func() cache.Config { return &bigcache.Config{} },
"fastcache": func() cache.Config { return &fastcache.Config{} },
}
func (c *CacheConfig) UnmarshalJSON(data []byte) error {
var rc struct {
Type string
Config json.RawMessage
}
if err := json.Unmarshal(data, &rc); err != nil {
return errors.ThrowInternal(err, "CONFI-98ejs", "unable to unmarshal config")
}
c.Type = rc.Type
var err error
c.Config, err = newCacheConfig(c.Type, rc.Config)
if err != nil {
return errors.ThrowInternal(err, "CONFI-do9es", "unable create config")
}
return nil
}
func newCacheConfig(cacheType string, configData []byte) (cache.Config, error) {
t, ok := caches[cacheType]
if !ok {
return nil, errors.ThrowInternal(nil, "CONFI-di328s", "no config")
}
cacheConfig := t()
if len(configData) == 0 {
return cacheConfig, nil
}
if err := json.Unmarshal(configData, cacheConfig); err != nil {
return nil, errors.ThrowInternal(nil, "CONFI-skei3", "could not read config")
}
return cacheConfig, nil
}