diff --git a/server/controllers/rule.go b/server/controllers/rule.go index fe60af0..b07550c 100644 --- a/server/controllers/rule.go +++ b/server/controllers/rule.go @@ -9,10 +9,12 @@ import ( "pmail/dto" "pmail/dto/response" "pmail/i18n" + "pmail/models" "pmail/services/rule" "pmail/utils/address" "pmail/utils/array" "pmail/utils/context" + "pmail/utils/errors" ) func GetRule(ctx *context.Context, w http.ResponseWriter, req *http.Request) { @@ -48,7 +50,7 @@ func UpsertRule(ctx *context.Context, w http.ResponseWriter, req *http.Request) } } - err = data.Encode().Save(ctx) + err = save(ctx, data.Encode()) if err != nil { response.NewErrorResponse(response.ServerError, "server error", err).FPrint(w) return @@ -56,6 +58,24 @@ func UpsertRule(ctx *context.Context, w http.ResponseWriter, req *http.Request) response.NewSuccessResponse("succ").FPrint(w) } +func save(ctx *context.Context, p *models.Rule) error { + + if p.Id > 0 { + _, err := db.Instance.Exec(db.WithContext(ctx, "update rule set name=? ,value = ? ,action = ?,params = ?,sort = ? where id = ?"), p.Name, p.Value, p.Action, p.Params, p.Sort, p.Id) + if err != nil { + return errors.Wrap(err) + } + return nil + } else { + _, err := db.Instance.Exec(db.WithContext(ctx, "insert into rule (name,value,user_id,action,params,sort) values (?,?,?,?,?,?)"), p.Name, p.Value, ctx.UserID, p.Action, p.Params, p.Sort) + if err != nil { + return errors.Wrap(err) + } + return nil + } + +} + type delRuleReq struct { Id int `json:"id"` } diff --git a/server/db/init.go b/server/db/init.go index 6f496ba..8ba9ba2 100644 --- a/server/db/init.go +++ b/server/db/init.go @@ -5,6 +5,7 @@ import ( _ "github.com/go-sql-driver/mysql" _ "modernc.org/sqlite" "pmail/config" + "pmail/models" "pmail/utils/context" "pmail/utils/errors" "xorm.io/xorm" @@ -30,6 +31,9 @@ func Init() error { Instance.SetMaxOpenConns(100) Instance.SetMaxIdleConns(10) + // 同步表结构 + syncTables() + return nil } @@ -40,3 +44,31 @@ func WithContext(ctx *context.Context, sql string) string { } return sql } + +func syncTables() { + err := Instance.Sync2(&models.User{}) + if err != nil { + panic(err) + } + err = Instance.Sync2(&models.Email{}) + if err != nil { + panic(err) + } + err = Instance.Sync2(&models.Group{}) + if err != nil { + panic(err) + } + err = Instance.Sync2(&models.Rule{}) + if err != nil { + panic(err) + } + err = Instance.Sync2(&models.UserAuth{}) + if err != nil { + panic(err) + } + err = Instance.Sync2(&models.Sessions{}) + if err != nil { + panic(err) + } + +} diff --git a/server/dto/parsemail/email_test.go b/server/dto/parsemail/email_test.go index 7cc5473..0f52bd4 100644 --- a/server/dto/parsemail/email_test.go +++ b/server/dto/parsemail/email_test.go @@ -4,75 +4,10 @@ import ( "bytes" "fmt" "github.com/emersion/go-message" - log "github.com/sirupsen/logrus" "io" - "os" - "pmail/config" - "pmail/db" - "pmail/session" "testing" - "time" ) -func testInit() { - // 设置日志格式为json格式 - //log.SetFormatter(&log.JSONFormatter{}) - - log.SetReportCaller(true) - log.SetFormatter(&log.TextFormatter{ - //以下设置只是为了使输出更美观 - DisableColors: true, - TimestampFormat: "2006-01-02 15:03:04", - }) - - // 设置将日志输出到标准输出(默认的输出为stderr,标准错误) - // 日志消息输出可以是任意的io.writer类型 - log.SetOutput(os.Stdout) - - // 设置日志级别为warn以上 - log.SetLevel(log.TraceLevel) - - var cst, _ = time.LoadLocation("Asia/Shanghai") - time.Local = cst - - config.Init() - config.Instance.DkimPrivateKeyPath = "../../config/dkim/dkim.priv" - config.Instance.DbType = config.DBTypeSQLite - config.Instance.DbDSN = "../../config/pmail_temp.db" - - Init() - db.Init() - session.Init() - -} -func TestEmail_domainMatch(t *testing.T) { - //e := &Email{} - //dnsNames := []string{ - // "*.mail.qq.com", - // "993.dav.qq.com", - // "993.eas.qq.com", - // "993.imap.qq.com", - // "993.pop.qq.com", - // "993.smtp.qq.com", - // "imap.qq.com", - // "mx1.qq.com", - // "mx2.qq.com", - // "mx3.qq.com", - // "pop.qq.com", - // "smtp.qq.com", - // "mail.qq.com", - //} - // - //fmt.Println(e.domainMatch("", dnsNames)) - //fmt.Println(e.domainMatch("xjiangwei.cn", dnsNames)) - //fmt.Println(e.domainMatch("qq.com", dnsNames)) - //fmt.Println(e.domainMatch("test.aaa.mail.qq.com", dnsNames)) - //fmt.Println(e.domainMatch("smtp.qq.com", dnsNames)) - //fmt.Println(e.domainMatch("pop.qq.com", dnsNames)) - //fmt.Println(e.domainMatch("test.mail.qq.com", dnsNames)) - -} - func Test_buildUser(t *testing.T) { u := buildUser("Jinnrry N ") if u.EmailAddress != "jiangwei1995910@gmail.com" { @@ -130,8 +65,6 @@ func TestEmailBuidlers(t *testing.T) { } func TestEmail_builder(t *testing.T) { - testInit() - e := Email{ From: buildUser("i@test.com"), To: buildUsers([]string{"to@test.com"}), diff --git a/server/models/base.go b/server/models/base.go index b25e340..2640e7f 100644 --- a/server/models/base.go +++ b/server/models/base.go @@ -1,30 +1 @@ package models - -import "pmail/db" - -func SyncTables() { - err := db.Instance.Sync2(&User{}) - if err != nil { - panic(err) - } - err = db.Instance.Sync2(&Email{}) - if err != nil { - panic(err) - } - err = db.Instance.Sync2(&Group{}) - if err != nil { - panic(err) - } - err = db.Instance.Sync2(&Rule{}) - if err != nil { - panic(err) - } - err = db.Instance.Sync2(&UserAuth{}) - if err != nil { - panic(err) - } - err = db.Instance.Sync2(&Sessions{}) - if err != nil { - panic(err) - } -} diff --git a/server/models/rule.go b/server/models/rule.go index d32f3c7..463218e 100644 --- a/server/models/rule.go +++ b/server/models/rule.go @@ -1,11 +1,5 @@ package models -import ( - "pmail/db" - "pmail/utils/context" - "pmail/utils/errors" -) - type Rule struct { Id int `xorm:"id int unsigned not null pk autoincr" json:"id"` UserId int `xorm:"user_id notnull default(0) comment('用户id')" json:"user_id"` @@ -19,21 +13,3 @@ type Rule struct { func (p *Rule) TableName() string { return "rule" } - -func (p *Rule) Save(ctx *context.Context) error { - - if p.Id > 0 { - _, err := db.Instance.Exec(db.WithContext(ctx, "update rule set name=? ,value = ? ,action = ?,params = ?,sort = ? where id = ?"), p.Name, p.Value, p.Action, p.Params, p.Sort, p.Id) - if err != nil { - return errors.Wrap(err) - } - return nil - } else { - _, err := db.Instance.Exec(db.WithContext(ctx, "insert into rule (name,value,user_id,action,params,sort) values (?,?,?,?,?,?)"), p.Name, p.Value, ctx.UserID, p.Action, p.Params, p.Sort) - if err != nil { - return errors.Wrap(err) - } - return nil - } - -} diff --git a/server/res_init/init.go b/server/res_init/init.go index cd83c56..bce2132 100644 --- a/server/res_init/init.go +++ b/server/res_init/init.go @@ -9,7 +9,6 @@ import ( "pmail/dto/parsemail" "pmail/hooks" "pmail/http_server" - "pmail/models" "pmail/pop3_server" "pmail/services/setup/ssl" "pmail/session" @@ -37,7 +36,6 @@ func Init(serverVersion string) { if err != nil { panic(err) } - models.SyncTables() session.Init() hooks.Init(serverVersion) // smtp server start diff --git a/server/services/setup/db.go b/server/services/setup/db.go index 90b6735..d327bcc 100644 --- a/server/services/setup/db.go +++ b/server/services/setup/db.go @@ -85,7 +85,6 @@ func SetDatabaseSettings(ctx *context.Context, dbType, dbDSN string) error { if err != nil { return errors.Wrap(err) } - models.SyncTables() return nil }