dndex/config/config.go

68 lines
2.0 KiB
Go

package config
import (
"io/ioutil"
"local/args"
"os"
"strings"
"time"
)
type Config struct {
Port int
Database string
Driver []string
FilePrefix string
APIPrefix string
FileRoot string
Auth bool
AuthLifetime time.Duration
MaxFileSize int64
RPS int
SysRPS int
Delay time.Duration
}
func New() Config {
as := args.NewArgSet()
f, err := ioutil.TempFile(os.TempDir(), "bolt.db.*")
if err != nil {
panic(err)
}
f.Close()
as.Append(args.INT, "p", "port to listen on", 18114)
as.Append(args.STRING, "fileprefix", "path prefix for file service", "/__files__")
as.Append(args.STRING, "api-prefix", "path prefix for api", "api")
as.Append(args.STRING, "fileroot", "path to file hosting root", "/tmp/")
as.Append(args.STRING, "database", "database name to use", "db")
as.Append(args.STRING, "driver", "database driver args to use, like [local/storage.Type,arg1,arg2...] or [/path/to/boltdb]", "map")
as.Append(args.BOOL, "auth", "check for authorized access", false)
as.Append(args.DURATION, "authlifetime", "duration auth is valid for", time.Hour)
as.Append(args.DURATION, "delay", "time to delay requests", time.Duration(0))
as.Append(args.INT, "max-file-size", "max file size for uploads in bytes", 50*(1<<20))
as.Append(args.INT, "rps", "rps per namespace", 5)
as.Append(args.INT, "sys-rps", "rps for the sys", 10)
if err := as.Parse(); err != nil {
os.Remove(f.Name())
panic(err)
}
return Config{
Port: as.GetInt("p"),
FilePrefix: as.GetString("fileprefix"),
FileRoot: as.GetString("fileroot"),
Database: as.GetString("database"),
Driver: strings.Split(as.GetString("driver"), ","),
Auth: as.GetBool("auth"),
AuthLifetime: as.GetDuration("authlifetime"),
Delay: as.GetDuration("delay"),
MaxFileSize: int64(as.GetInt("max-file-size")),
RPS: as.GetInt("rps"),
SysRPS: as.GetInt("sys-rps"),
APIPrefix: strings.TrimPrefix(as.GetString("api-prefix"), "/"),
}
}