forked from dreamans/syncd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
2,440 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2019 syncd Author. All Rights Reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package syncd | ||
|
||
type ( | ||
Config struct { | ||
Serve *ServeConfig | ||
Db *DbConfig | ||
Log *LogConfig | ||
Syncd *SyncdConfig | ||
Mail *MailConfig | ||
} | ||
|
||
SyncdConfig struct { | ||
LocalSpace string | ||
RemoteSpace string | ||
Cipher string | ||
} | ||
|
||
LogConfig struct { | ||
Path string | ||
} | ||
|
||
MailConfig struct { | ||
Enable int | ||
Smtp string | ||
Port int | ||
User string | ||
Pass string | ||
} | ||
|
||
ServeConfig struct { | ||
Addr string | ||
ReadTimeout int | ||
WriteTimeout int | ||
IdleTimeout int | ||
} | ||
|
||
DbConfig struct { | ||
Unix string | ||
Host string | ||
Port int | ||
Charset string | ||
User string | ||
Pass string | ||
DbName string | ||
TablePrefix string | ||
MaxIdleConns int | ||
MaxOpenConns int | ||
ConnMaxLifeTime int | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2019 syncd Author. All Rights Reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package syncd | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
"errors" | ||
|
||
"github.com/jinzhu/gorm" | ||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
type DB struct { | ||
DbHandler *gorm.DB | ||
cfg *DbConfig | ||
} | ||
|
||
func NewDatabase(cfg *DbConfig) *DB { | ||
return &DB{ | ||
cfg: cfg, | ||
} | ||
} | ||
|
||
func (db *DB) Open() error { | ||
c, err := gorm.Open("mysql", db.parseConnConfig()) | ||
if err != nil { | ||
return errors.New(fmt.Sprintf("mysql connect failed, %s", err.Error())) | ||
} | ||
|
||
c.LogMode(true) | ||
c.DB().SetMaxIdleConns(db.cfg.MaxIdleConns) | ||
c.DB().SetMaxOpenConns(db.cfg.MaxOpenConns) | ||
c.DB().SetConnMaxLifetime(time.Second * time.Duration(db.cfg.ConnMaxLifeTime)) | ||
|
||
db.DbHandler = c | ||
return nil | ||
} | ||
|
||
func (db *DB) Close() { | ||
db.DbHandler.Close() | ||
} | ||
|
||
func (db *DB) parseConnConfig() string { | ||
var connHost string | ||
if db.cfg.Unix != "" { | ||
connHost = fmt.Sprintf("unix(%s)", db.cfg.Unix) | ||
} else { | ||
connHost = fmt.Sprintf("tcp(%s:%d)", db.cfg.Host, db.cfg.Port) | ||
} | ||
s := fmt.Sprintf("%s:%s@%s/%s?charset=%s&parseTime=True&loc=Local", db.cfg.User, db.cfg.Pass, connHost, db.cfg.DbName, db.cfg.Charset) | ||
println(s) | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright 2019 syncd Author. All Rights Reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package model | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/jinzhu/gorm" | ||
"github.com/dreamans/syncd" | ||
) | ||
|
||
type WhereParam struct { | ||
Field string | ||
Tag string | ||
Prepare interface{} | ||
} | ||
|
||
type QueryParam struct { | ||
Fields string | ||
Offset int | ||
Limit int | ||
Order string | ||
Where []WhereParam | ||
} | ||
|
||
func Create(model interface{}) bool { | ||
db := syncd.App.DB.DbHandler.Create(model) | ||
if err := db.Error; err != nil { | ||
syncd.App.Logger.Warning("mysql execute error: %s, sql [%v]", err.Error(), db.QueryExpr()) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func GetMulti(model interface{}, query QueryParam) bool { | ||
db := syncd.App.DB.DbHandler.Offset(query.Offset) | ||
if query.Limit > 0 { | ||
db = db.Limit(query.Limit) | ||
} | ||
if query.Fields != "" { | ||
db = db.Select(query.Fields) | ||
} | ||
if query.Order != "" { | ||
db = db.Order(query.Order) | ||
} | ||
db = parseWhereParam(db, query.Where) | ||
db.Find(model) | ||
if err := db.Error; err != nil { | ||
syncd.App.Logger.Warning("mysql query error: %s, sql[%v]", err.Error(), db.QueryExpr()) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func Count(model interface{}, count *int, query QueryParam) bool { | ||
db := syncd.App.DB.DbHandler.Model(model) | ||
db = parseWhereParam(db, query.Where) | ||
db = db.Count(count) | ||
if err := db.Error; err != nil { | ||
syncd.App.Logger.Warning("mysql query error: %s, sql[%v]", err.Error(), db.QueryExpr()) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func Delete(model interface{}, query QueryParam) bool { | ||
if len(query.Where) == 0 { | ||
syncd.App.Logger.Warning("mysql query error: delete failed, where conditions cannot be empty") | ||
return false | ||
} | ||
db := syncd.App.DB.DbHandler.Model(model) | ||
db = parseWhereParam(db, query.Where) | ||
db = db.Delete(model) | ||
if err := db.Error; err != nil { | ||
syncd.App.Logger.Warning("mysql query error: %s, sql[%v]", err.Error(), db.QueryExpr()) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func DeleteByPk(model interface{}) bool { | ||
db := syncd.App.DB.DbHandler.Model(model) | ||
db.Delete(model) | ||
if err := db.Error; err != nil { | ||
syncd.App.Logger.Warning("mysql query error: %s, sql[%v]", err.Error(), db.QueryExpr()) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func GetOne(model interface{}, query QueryParam) bool { | ||
db := syncd.App.DB.DbHandler.Model(model) | ||
if query.Fields != "" { | ||
db = db.Select(query.Fields) | ||
} | ||
db = parseWhereParam(db, query.Where) | ||
db = db.First(model) | ||
if err := db.Error; err != nil && !db.RecordNotFound() { | ||
syncd.App.Logger.Warning("mysql query error: %s, sql[%v]", err.Error(), db.QueryExpr()) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func GetByPk(model interface{}, id interface{}) bool { | ||
db := syncd.App.DB.DbHandler.Model(model) | ||
db.First(model, id) | ||
if err := db.Error; err != nil && !db.RecordNotFound() { | ||
syncd.App.Logger.Warning("mysql query error: %s sql[%v]", err.Error(), db.QueryExpr()) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func UpdateByPk(model interface{}) bool { | ||
db := syncd.App.DB.DbHandler.Model(model) | ||
db = db.Updates(model) | ||
if err := db.Error; err != nil { | ||
syncd.App.Logger.Warning("mysql query error: %s, sql[%v]", err.Error(), db.QueryExpr()) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func parseWhereParam(db *gorm.DB, where []WhereParam) *gorm.DB { | ||
if len(where) == 0 { | ||
return db | ||
} | ||
var ( | ||
plain []string | ||
prepare []interface{} | ||
) | ||
for _, w := range where { | ||
tag := w.Tag | ||
if tag == "" { | ||
tag = "=" | ||
} | ||
var plainFmt string | ||
switch tag { | ||
case "IN": | ||
plainFmt = fmt.Sprintf("%s IN (?)", w.Field) | ||
default: | ||
plainFmt = fmt.Sprintf("%s %s ?", w.Field, tag) | ||
} | ||
plain = append(plain, plainFmt) | ||
prepare = append(prepare, w.Prepare) | ||
} | ||
return db.Where(strings.Join(plain, " AND "), prepare...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2019 syncd Author. All Rights Reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package model | ||
|
||
import( | ||
"time" | ||
) | ||
|
||
type Server struct { | ||
ID int `gorm:"primary_key"` | ||
GroupId int `gorm:"type:int(11);not null;default:0"` | ||
Name string `gorm:"type:varchar(100);not null;default:''"` | ||
Ip string `gorm:"type:varchar(100);not null;default:''"` | ||
SSHPort int `gorm:"type:int(11);not null;default:0"` | ||
Ctime int `gorm:"type:int(11);not null;default:0"` | ||
} | ||
|
||
func (m *Server) TableName() string { | ||
return "syd_server" | ||
} | ||
|
||
func (m *Server) Create() bool { | ||
m.Ctime = int(time.Now().Unix()) | ||
return Create(m) | ||
} | ||
|
||
func (m *Server) Update() bool { | ||
return UpdateByPk(m) | ||
} | ||
|
||
func (m *Server) List(query QueryParam) ([]Server, bool) { | ||
var data []Server | ||
ok := GetMulti(&data, query) | ||
return data, ok | ||
} | ||
|
||
func (m *Server) Count(query QueryParam) (int, bool) { | ||
var count int | ||
ok := Count(m, &count, query) | ||
return count, ok | ||
} | ||
|
||
func (s *Server) Delete() bool { | ||
return DeleteByPk(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2019 syncd Author. All Rights Reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package model | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type ServerGroup struct { | ||
ID int `gorm:"primary_key"` | ||
Name string `gorm:"type:varchar(100);not null;default:''"` | ||
Ctime int `gorm:"type:int(11);not null;default:0"` | ||
} | ||
|
||
func (m *ServerGroup) TableName() string { | ||
return "syd_server_group" | ||
} | ||
|
||
func (m *ServerGroup) Create() bool { | ||
m.Ctime = int(time.Now().Unix()) | ||
return Create(m) | ||
} | ||
|
||
func (m *ServerGroup) Update() bool { | ||
return UpdateByPk(m) | ||
} | ||
|
||
func (m *ServerGroup) List(query QueryParam) ([]ServerGroup, bool) { | ||
var data []ServerGroup | ||
ok := GetMulti(&data, query) | ||
return data, ok | ||
} | ||
|
||
func (m *ServerGroup) Count(query QueryParam) (int, bool) { | ||
var count int | ||
ok := Count(m, &count, query) | ||
return count, ok | ||
} | ||
|
||
func (m *ServerGroup) Delete() bool { | ||
return DeleteByPk(m) | ||
} | ||
|
||
func (m *ServerGroup) Get(id int) bool { | ||
return GetByPk(m, id) | ||
} |
Oops, something went wrong.