Browse Source

Moved in configs.

develop T20170313
Mingcai SHEN 8 years ago
parent
commit
a71d690b5e
2 changed files with 91 additions and 0 deletions
  1. +56
    -0
      configs/options.go
  2. +35
    -0
      configs/types.go

+ 56
- 0
configs/options.go View File

@ -0,0 +1,56 @@
package configs
import (
"io"
"github.com/BurntSushi/toml"
)
type GenericOption struct {
Version string
Tag string
Log_File string
Log_Level string
ListenAddr string
Debug bool
Cache bool
Pool uint
Max_Rows uint
Readonly bool
Deletable bool
JWT_Secret string
}
type PostgresOption struct {
Host string
Port uint
Username string
Password string
Database string
Schema string
}
type RedisOption struct {
Host string
Port uint
Database int64
}
func CheckConfiguration(option interface{}, output io.Writer) {
var _print = func(s string) {
io.WriteString(output, s)
}
_print("Checking options ...\n")
if nil == option {
_print("Invalid configuration!!!\n")
return
}
_print("\n")
toml.NewEncoder(output).Encode(option)
_print("\n")
_print("Configration validated!\n")
}
func LoadConfiguration(filename string, option interface{}) (e error) {
_, e = toml.DecodeFile(filename, option)
return e
}

+ 35
- 0
configs/types.go View File

@ -0,0 +1,35 @@
package configs
import (
"strconv"
"time"
)
type Duration time.Duration
type HexUint32 uint32
type HexUint16 uint16
type HexUint8 uint8
func (self *Duration) UnmarshalText(text []byte) error {
t, err := time.ParseDuration(string(text))
*self = Duration(t)
return err
}
func (self *HexUint32) UnmarshalText(text []byte) error {
t, err := strconv.ParseUint(string(text), 16, 32)
*self = HexUint32(t)
return err
}
func (self *HexUint16) UnmarshalText(text []byte) error {
t, err := strconv.ParseUint(string(text), 16, 16)
*self = HexUint16(t)
return err
}
func (self *HexUint8) UnmarshalText(text []byte) error {
t, err := strconv.ParseUint(string(text), 16, 8)
*self = HexUint8(t)
return err
}

Loading…
Cancel
Save