golib/gconfig/db.go

57 lines
1.4 KiB
Go
Raw Normal View History

2023-06-15 21:22:51 +08:00
//
// db.go
// Copyright (C) 2022 tiglog <me@tiglog.com>
//
// Distributed under terms of the MIT license.
//
package gconfig
import "fmt"
type DbConfig struct {
Type string `yaml:"type"`
Host string `yaml:"host"`
Username string `yaml:"user"`
Password string `yaml:"pass"`
Port int `yaml:"port"`
Name string `yaml:"name"`
}
type MongoConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"user"`
Password string `yaml:"pass"`
Name string `yaml:"name"`
PoolSize int `yaml:"pool_size"`
}
func (c *DbConfig) GetUri() string {
switch c.Type {
case "postgres":
return fmt.Sprintf("%s://host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", c.Type, c.Host, c.Port, c.Username, c.Password, c.Name)
case "mysql":
return fmt.Sprintf("%s://%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=Local", c.Type, c.Username, c.Password, c.Host, c.Port, c.Name)
}
return ""
}
func (c *MongoConfig) GetUri() string {
if c.Host == "" {
return ""
}
if c.Username == "" {
return fmt.Sprintf("mongodb://%s:%d/%s", c.Host, c.Port, c.Name)
} else {
return fmt.Sprintf("mongodb://%s:%s@%s:%d/%s", c.Username, c.Password, c.Host, c.Port, c.Name)
}
}
type RedisConfig struct {
Addr string `yaml:"addr"`
Username string `yaml:"user"`
Password string `yaml:"pass"`
Database int `yaml:"db"`
}