Skip to content

Commit

Permalink
perf: take out from inside
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Feb 15, 2023
1 parent 44c47ba commit f008c0c
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 75 deletions.
3 changes: 1 addition & 2 deletions internal/message/bark.go → bark.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package message
import (
"bytes"
"encoding/json"
"github.com/zggsong/message/internal/model"
"net/http"
)

Expand All @@ -14,7 +13,7 @@ type Bark struct {

func (b *Bark) Send(body Body) error {
//log.Printf("[bark] sending message...")
var reqBody = model.BarkRequest{
var reqBody = BarkRequest{
DeviceKey: b.key,
Title: body.Title,
Body: body.Content,
Expand Down
14 changes: 6 additions & 8 deletions internal/config/config.go → config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package config
package message

import (
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"github.com/zggsong/message/internal/global"
"github.com/zggsong/message/internal/model"
"os"
)

Expand All @@ -13,7 +11,7 @@ import (
// @Description: 初始化配置
// @return model.Config
// @return error
func InitConfig() (model.Config, error) {
func InitConfig() (Config, error) {
workDir, _ := os.Getwd()
viper.SetConfigName("config")
viper.SetConfigType("yml")
Expand All @@ -23,12 +21,12 @@ func InitConfig() (model.Config, error) {
viper.AddConfigPath("../../../config")
viper.AddConfigPath(workDir + "/config")
viper.OnConfigChange(func(e fsnotify.Event) {
global.GLO_CONF_CH <- updateConfig()
GLO_CONF_CH <- updateConfig()
})
viper.WatchConfig()
err := viper.ReadInConfig()
if err != nil {
return model.Config{}, err
return Config{}, err
}
return updateConfig(), nil
}
Expand All @@ -37,8 +35,8 @@ func InitConfig() (model.Config, error) {
//
// @Description: 更新配置
// @return model.Config
func updateConfig() model.Config {
var config model.Config
func updateConfig() Config {
var config Config
config.MsgEnabled = viper.GetBool("message.enabled")
config.MsgType = viper.GetString("message.type")
config.BarkUrl = viper.GetString("message.bark.url")
Expand Down
File renamed without changes.
8 changes: 3 additions & 5 deletions internal/config/config_test.go → config_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package config
package message

import (
"github.com/zggsong/message/internal/global"
"github.com/zggsong/message/internal/model"
"testing"
)

func TestConfig(t *testing.T) {
global.GLO_CONF_CH = make(chan model.Config)
GLO_CONF_CH = make(chan Config)
config, err := InitConfig()
if err != nil {
t.Fatalf("Failed to initialize config: %v", err)
Expand All @@ -19,7 +17,7 @@ func TestConfig(t *testing.T) {
if count > 5 {
return
}
config = <-global.GLO_CONF_CH
config = <-GLO_CONF_CH
count++
t.Log(config)
}
Expand Down
6 changes: 6 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package message

var (
GLO_CONF_CH chan Config
GLO_CONF Config
)
8 changes: 0 additions & 8 deletions internal/global/global.go

This file was deleted.

36 changes: 0 additions & 36 deletions internal/message/message.go

This file was deleted.

File renamed without changes.
34 changes: 34 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package message

type Message interface {
Send(body Body) error
}

type Body struct {
Title string
Content string
}

func GetType() Message {
switch GLO_CONF.MsgType {
case "bark":
return &Bark{
url: GLO_CONF.BarkUrl,
key: GLO_CONF.BarkKey,
}
case "mail":
return &Mail{
Host: GLO_CONF.MailHost,
Port: GLO_CONF.MailPort,
Username: GLO_CONF.MailUser,
Password: GLO_CONF.MailPwd,
FromName: GLO_CONF.MailFromName,
To: GLO_CONF.MailTo,
}
}
return nil
}

func Enabled() bool {
return GLO_CONF.MsgEnabled
}
6 changes: 2 additions & 4 deletions internal/message/message_test.go → message_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package message

import (
"github.com/zggsong/message/internal/config"
"github.com/zggsong/message/internal/global"
"log"
"testing"
"time"
)

func TestSend(t *testing.T) {
conf, err := config.InitConfig()
conf, err := InitConfig()
if err != nil {
log.Println("[test] failed to initialize config: ", err)
}
global.GLO_CONF = conf
GLO_CONF = conf

m := GetType()
if !Enabled() || m == nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/model/model.go → model.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package message

// Config
// @Description: 配置
Expand Down
9 changes: 4 additions & 5 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package message

import (
"errors"
"github.com/zggsong/message/internal/message"
)

type Service struct {
Body message.Body
Body Body
}

func (s Service) Run() error {
return send(s.Body)
}

func send(body message.Body) error {
m := message.GetType()
if m == nil || !message.Enabled() {
func send(body Body) error {
m := GetType()
if m == nil || !Enabled() {
return errors.New("config message type error or not enabled")
}
if err := m.Send(body); err != nil {
Expand Down
9 changes: 3 additions & 6 deletions service_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package message

import (
"github.com/zggsong/message/internal/config"
"github.com/zggsong/message/internal/global"
"github.com/zggsong/message/internal/message"
"testing"
"time"
)

func TestService(t *testing.T) {
conf, err := config.InitConfig()
conf, err := InitConfig()
if err != nil {
t.Fatalf("[test] failed to initialize config: %v", err)
}
global.GLO_CONF = conf
GLO_CONF = conf

var s = Service{Body: message.Body{
var s = Service{Body: Body{
Title: "test title",
Content: "this is content, time is " + time.Now().Format("2006-01-02 15:04:05"),
}}
Expand Down

0 comments on commit f008c0c

Please sign in to comment.