Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"file.go",
"mysql.go",
"redis.go",
],
importpath = "go-common/app/infra/config/dao/v1",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/infra/config/conf:go_default_library",
"//app/infra/config/model:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,58 @@
package v1
import (
"context"
"time"
"go-common/app/infra/config/conf"
"go-common/library/cache/redis"
"go-common/library/database/sql"
)
// Dao dao.
type Dao struct {
// mysql
db *sql.DB
// redis
redis *redis.Pool
expire time.Duration
// cache
pathCache string
}
// New new a dao.
func New(c *conf.Config) *Dao {
d := &Dao{
// db
db: sql.NewMySQL(c.DB),
// redis
redis: redis.NewPool(c.Redis),
expire: time.Duration(c.PollTimeout),
// cache
pathCache: c.PathCache,
}
return d
}
// BeginTran begin transcation.
func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
return d.db.Begin(c)
}
// Close close resuouces.
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
if d.redis != nil {
d.redis.Close()
}
}
// Ping ping is ok.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.pingRedis(c); err != nil {
return
}
return d.db.Ping(c)
}

View File

@@ -0,0 +1,70 @@
package v1
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"go-common/app/infra/config/model"
"go-common/library/log"
)
// SetFile set config file.
func (d *Dao) SetFile(name string, conf *model.Content) (err error) {
b, err := json.Marshal(conf)
if err != nil {
log.Error("json.Marshal(%v) error(%v)", conf, err)
return
}
p := path.Join(d.pathCache, name)
if err = ioutil.WriteFile(p, b, 0644); err != nil {
log.Error("ioutil.WriteFile(%s) error(%v)", p, err)
}
return
}
// File return config file.
func (d *Dao) File(name string) (res *model.Content, err error) {
p := path.Join(d.pathCache, name)
b, err := ioutil.ReadFile(p)
if err != nil {
log.Error("ioutil.ReadFile(%s) error(%v)", p, err)
return
}
res = &model.Content{}
if err = json.Unmarshal(b, &res); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", b, err)
}
return
}
// DelFile delete file cache.
func (d *Dao) DelFile(name string) (err error) {
p := path.Join(d.pathCache, name)
if err = os.Remove(p); err != nil {
log.Error("os.Remove(%s) error(%v)", p, err)
}
return
}
// SetFileStr save string file.
func (d *Dao) SetFileStr(name string, val string) (err error) {
p := path.Join(d.pathCache, name)
if err = ioutil.WriteFile(p, []byte(val), 0644); err != nil {
log.Error("ioutil.WriteFile(%s) error(%v)", p, err)
}
return
}
// FileStr get string file.
func (d *Dao) FileStr(name string) (file string, err error) {
p := path.Join(d.pathCache, name)
b, err := ioutil.ReadFile(p)
if err != nil {
log.Error("ioutil.ReadFile(%s) error(%v)", p, err)
return
}
file = string(b)
return
}

View File

@@ -0,0 +1,224 @@
package v1
import (
"bytes"
"context"
"go-common/app/infra/config/model"
"go-common/library/database/sql"
"go-common/library/ecode"
"go-common/library/log"
)
const (
_getToken = "SELECT token FROM service_name WHERE name=? AND environment=?"
_getBuildVersion = "SELECT b.config_id FROM service_name s, build_version b WHERE b.version=? AND s.name=? And s.environment=? AND s.id=b.service_id AND b.state=2"
_getVersions = "SELECT c.id,c.remark FROM service_config c, service_name s WHERE s.name=? AND s.environment=? AND s.id =c.service_id AND c.state=? ORDER BY c.id DESC"
_getNamespace = "SELECT id,namespace FROM service_namespace WHERE config_id=?"
_getValue = "SELECT config_id,namespace_id,name,config FROM service_config_value WHERE config_id=?"
_getFile = "SELECT config FROM service_config_value WHERE config_id=? AND name =?"
_getBuilds = "SELECT b.version FROM service_name s ,build_version b WHERE s.name=? AND s.environment=? AND s.id=b.service_id AND b.state=2 ORDER BY b.id DESC"
_getServiceID = "SELECT id FROM service_name where name=? AND environment =?"
_insertVersion = "INSERT INTO service_config(service_id,state,operator) VALUES (?,?,?)"
_insertConfigs = "INSERT INTO service_config_value(config_id,name,config,operator) VALUES "
_updateConfigs = "UPDATE service_config_value SET config=?,operator=? WHERE config_id = ? AND name = ?"
_insertLog = "INSERT INTO log(username,business,info) VALUES (?,?,?)"
)
// Token return a Secret from mysql.
func (d *Dao) Token(c context.Context, svr, env string) (token string, err error) {
row := d.db.QueryRow(c, _getToken, svr, env)
if err = row.Scan(&token); err != nil {
log.Error("row.Scan error(%v) svrName(%v)", err, svr)
if err == sql.ErrNoRows {
err = nil
return
}
}
return
}
// BuildVersion return service build version from mysql.
func (d *Dao) BuildVersion(c context.Context, svr, bver, env string) (version int64, err error) {
row := d.db.QueryRow(c, _getBuildVersion, bver, svr, env)
if err = row.Scan(&version); err != nil {
if err == sql.ErrNoRows {
version = model.UnknownVersion
err = nil
return
}
log.Error("row.Scan error(%v)", err)
}
return
}
// Values return values from mysql.
func (d *Dao) Values(c context.Context, ver int64) (rs []*model.NSValue, err error) {
rows, err := d.db.Query(c, _getValue, ver)
if err != nil {
log.Error("db.Query(%d) error(%v)", ver, err)
return
}
defer rows.Close()
for rows.Next() {
var r model.NSValue
if err = rows.Scan(&r.ConfigID, &r.NamespaceID, &r.Name, &r.Config); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
rs = append(rs, &r)
}
return
}
// Namespaces return namespaces from mysql
func (d *Dao) Namespaces(c context.Context, ver int64) (rs map[int64]string, err error) {
rows, err := d.db.Query(c, _getNamespace, ver)
if err != nil {
log.Error("db.Query(%d) error(%v)", err)
return
}
rs = make(map[int64]string)
defer rows.Close()
for rows.Next() {
var id int64
var name string
if err = rows.Scan(&id, &name); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
rs[id] = name
}
return
}
//Versions return versions from mysql
func (d *Dao) Versions(c context.Context, svr, env string, state int8) (rs []*model.ReVer, err error) {
rows, err := d.db.Query(c, _getVersions, svr, env, state)
if err != nil {
log.Error("db.Query(%s) error(%v)", svr, err)
return
}
defer rows.Close()
for rows.Next() {
var r model.ReVer
if err = rows.Scan(&r.Version, &r.Remark); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
rs = append(rs, &r)
}
return
}
// Value return values from mysql.
func (d *Dao) Value(c context.Context, fname string, ver int64) (file string, err error) {
row := d.db.QueryRow(c, _getFile, ver, fname)
if err = row.Scan(&file); err != nil {
log.Error("row.Scan error(%v)", err)
if err == sql.ErrNoRows {
err = ecode.NothingFound
return
}
}
return
}
// Builds get service builds.
func (d *Dao) Builds(c context.Context, svr, env string) (rs []string, err error) {
rows, err := d.db.Query(c, _getBuilds, svr, env)
if err != nil {
log.Error("db.Query(%s) error(%v)", svr, err)
return
}
defer rows.Close()
for rows.Next() {
var r string
if err = rows.Scan(&r); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
rs = append(rs, r)
}
return
}
// ServiceID get ServiceID.
func (d *Dao) ServiceID(c context.Context, svr, env string) (ID int64, err error) {
row := d.db.QueryRow(c, _getServiceID, svr, env)
if err != nil {
log.Error("db.Query(%s) error(%v)", svr, err)
return
}
if err = row.Scan(&ID); err != nil {
log.Error("row.Scan error(%v)", err)
if err == sql.ErrNoRows {
err = ecode.NothingFound
return
}
}
return
}
// TxInsertVer insert version.
func (d *Dao) TxInsertVer(tx *sql.Tx, svrID int64, user string) (verID int64, err error) {
row, err := tx.Exec(_insertVersion, svrID, model.ConfigIng, user)
if err != nil {
log.Error("db.insert(%d) error(%v)", svrID, err)
return
}
return row.LastInsertId()
}
// TxInsertValues insert config values.
func (d *Dao) TxInsertValues(c context.Context, tx *sql.Tx, verID int64, user string, data map[string]string) (err error) {
var (
buffer bytes.Buffer
insertTp string
stmt *sql.Stmt
is []interface{}
)
buffer.WriteString(_insertConfigs)
insertTp = "(?,?,?,?),"
for key, val := range data {
buffer.WriteString(insertTp)
is = append(is, verID)
is = append(is, key)
is = append(is, val)
is = append(is, user)
}
buffer.Truncate(buffer.Len() - 1)
if stmt, err = tx.Prepare(buffer.String()); err != nil {
log.Error("d.insert() error(%v)", err)
return
}
_, err = stmt.Exec(c, is...)
if err != nil {
log.Error("d.insert() error(%v)", err)
}
return
}
// TxUpdateValues update config values.
func (d *Dao) TxUpdateValues(tx *sql.Tx, verID int64, user string, data map[string]string) (err error) {
for key, val := range data {
if _, err = tx.Exec(_updateConfigs, val, user, verID, key); err != nil {
log.Error("db.UpdateValues(%d) error(%v)", user, err)
break
}
}
if err != nil {
log.Error("d.insert() error(%v)", err)
}
return
}
// InsertLog insert log.
func (d *Dao) InsertLog(c context.Context, user, business, info string) (err error) {
_, err = d.db.Exec(c, _insertLog, user, business, info)
if err != nil {
log.Error("db.InsertLog(%d) error(%v)", user, err)
return
}
return
}

View File

@@ -0,0 +1,100 @@
package v1
import (
"context"
"encoding/json"
"fmt"
"time"
"go-common/app/infra/config/model"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
expireDuration = 3 * time.Hour
_hostKey = "%s_%s"
)
// Hostkey host cache key
func hostKey(svr, env string) string {
return fmt.Sprintf(_hostKey, svr, env)
}
// Hosts return service hosts from redis.
func (d *Dao) Hosts(c context.Context, svr, env string) (hosts []*model.Host, err error) {
var (
dels []string
now = time.Now()
hostkey = hostKey(svr, env)
conn = d.redis.Get(c)
)
defer conn.Close()
res, err := redis.Strings(conn.Do("HGETALL", hostkey))
if err != nil {
log.Error("conn.Do(HGETALL, %s) error(%v)", hostkey, err)
return
}
for i, r := range res {
if i%2 == 0 {
continue
}
h := &model.Host{}
if err = json.Unmarshal([]byte(r), h); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", r, err)
return
}
if now.Sub(h.HeartbeatTime.Time()) <= d.expire+5 {
h.State = model.HostOnline
hosts = append(hosts, h)
} else if now.Sub(h.HeartbeatTime.Time()) >= expireDuration {
dels = append(dels, h.Name)
} else {
h.State = model.HostOffline
hosts = append(hosts, h)
}
}
if len(dels) > 0 {
if _, err1 := conn.Do("HDEL", hostkey, dels); err1 != nil {
log.Error("conn.Do(HDEL, %s, %v) error(%v)", hostkey, dels, err1)
}
}
return
}
// SetHost add service host to redis.
func (d *Dao) SetHost(c context.Context, host *model.Host, svr, env string) (err error) {
hostkey := hostKey(svr, env)
b, err := json.Marshal(host)
if err != nil {
log.Error("json.Marshal(%s) error(%v)", host, err)
return
}
conn := d.redis.Get(c)
defer conn.Close()
if _, err = conn.Do("HSET", hostkey, host.Name, string(b)); err != nil {
log.Error("conn.Do(SET, %s, %s, %v) error(%v)", hostkey, host.Name, host, err)
}
return
}
// ClearHost clear all hosts.
func (d *Dao) ClearHost(c context.Context, svr, env string) (err error) {
var (
hostkey = hostKey(svr, env)
conn = d.redis.Get(c)
)
defer conn.Close()
if _, err = conn.Do("DEL", hostkey); err != nil {
log.Error("conn.Do(DEL, %s) error(%v)", hostkey, err)
}
return
}
// Ping check Redis connection
func (d *Dao) pingRedis(c context.Context) (err error) {
conn := d.redis.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}