success write tokens to config with toml (not finish)

This commit is contained in:
2023-09-15 19:09:20 +08:00
parent 7e09934024
commit 856bb3e27b
8 changed files with 39 additions and 33 deletions

View File

@@ -355,11 +355,14 @@ func (c *HandleController) MakeAddTokenFunc() func(context *gin.Context) {
return
}
c.Tokens[info.User] = info
tokenFile, err := os.Create(c.TokensFile)
if err != nil {
log.Printf("error to crate file %v: %v", c.TokensFile, err)
}
if err = toml.NewEncoder(tokenFile).Encode(c.Tokens); err != nil {
if err = toml.NewEncoder(tokenFile).Encode(c.TokensList()); err != nil {
log.Printf("error to encode tokens: %v", err)
}
if err = tokenFile.Close(); err != nil {

View File

@@ -39,7 +39,7 @@ type HTTPError struct {
type Config struct {
Common CommonInfo
Tokens []TokenInfo
Tokens
}
type CommonInfo struct {
@@ -58,6 +58,10 @@ type CommonInfo struct {
DashboardTls bool
}
type Tokens struct {
Tokens map[string]TokenInfo `toml:"tokens"`
}
type TokenInfo struct {
User string `toml:"user" json:"user" form:"user"`
Token string `toml:"token" json:"token" form:"token"`
@@ -112,3 +116,9 @@ type TokenEnable struct {
func (e *HTTPError) Error() string {
return e.Err.Error()
}
func (c *HandleController) TokensList() Tokens {
return Tokens{
c.Tokens,
}
}