feat: add config

This commit is contained in:
weloe
2023-05-02 15:04:50 +08:00
parent 9be7ea8ceb
commit 35c206716d
4 changed files with 145 additions and 0 deletions

19
config/cookie.go Normal file
View File

@@ -0,0 +1,19 @@
package config
type cookieConfig struct {
Domain string
Path string
Secure bool
HttpOnly bool
SameSite string
}
func DefaultCookieConfig() *cookieConfig {
return &cookieConfig{
Domain: "",
Path: "",
Secure: false,
HttpOnly: false,
SameSite: "",
}
}

37
config/reader.go Normal file
View File

@@ -0,0 +1,37 @@
package config
import "reflect"
type ConfigInterface interface {
LoadTokenConfig(conf string) (*tokenConfig, error)
}
var _ ConfigInterface = (*FileConfig)(nil)
type FileConfig struct {
TokenConfig *tokenConfig
}
func (c *FileConfig) LoadTokenConfig(conf string) (*tokenConfig, error) {
//TODO implement me
return &tokenConfig{}, nil
}
func (c *FileConfig) parse(confName string) (err error) {
c.TokenConfig, err = c.LoadTokenConfig(confName)
if err != nil {
return err
}
if reflect.DeepEqual(c.TokenConfig, &tokenConfig{}) {
c.TokenConfig = DefaultTokenConfig()
}
return err
}
// NewConfig create from file.
func NewConfig(confName string) (ConfigInterface, error) {
c := &FileConfig{}
err := c.parse(confName)
return c, err
}

21
config/reader_test.go Normal file
View File

@@ -0,0 +1,21 @@
package config
import "testing"
func TestNewConfig(t *testing.T) {
config, err := NewConfig("")
if err != nil {
t.Fatalf("read error: %v", err)
}
t.Log(config)
}
func TestDefaultCookieConfig(t *testing.T) {
config := DefaultTokenConfig()
t.Log(config)
}
func TestDefaultTokenConfig(t *testing.T) {
config := DefaultCookieConfig()
t.Log(config)
}

68
config/token.go Normal file
View File

@@ -0,0 +1,68 @@
package config
type tokenConfig struct {
// TokenName prefix
TokenStyle string
TokenPrefix string
TokenName string
Timeout int64
ActivityTimeout int64
// Data clean period
DataRefreshPeriod int64
// Auto refresh token
AutoRenew bool
// Allow multi login
IsConcurrent bool
IsShare bool
// If (IsConcurrent == true && IsShare == false), support MaxLoginCount
MaxLoginCount int16
// Read token method
// Set to true to read token from these method before login.
IsReadBody bool
IsReadHeader bool
IsReadCookie bool
// Write token to response header.
// Set to true to write after login.
IsWriteHeader bool
TokenSessionCheckLogin bool
JwtSecretKey string
CurDomain string
SameTokenTimeout int64
CheckSameToken bool
CookieConfig *cookieConfig
}
func DefaultTokenConfig() *tokenConfig {
return &tokenConfig{
TokenStyle: "uuid",
TokenPrefix: "",
TokenName: "tokenGo",
Timeout: 60 * 60 * 24 * 30,
ActivityTimeout: -1,
DataRefreshPeriod: 30,
AutoRenew: true,
IsConcurrent: true,
IsShare: true,
MaxLoginCount: 12,
IsReadBody: true,
IsReadHeader: true,
IsReadCookie: true,
IsWriteHeader: false,
TokenSessionCheckLogin: true,
JwtSecretKey: "",
CurDomain: "",
SameTokenTimeout: 60 * 60 * 24,
CheckSameToken: false,
CookieConfig: DefaultCookieConfig(),
}
}