Add Bark and Config

增加Bark推送以及json配置文件

配置文件

配置文件类似如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
  "port":1005,
  "db":{
    "dbPath":"qr.db",
    "timeout":1
  },
  "limit":{
    "maxSingleFileSize":102400,
    "maxFileCount":10,
    "expiredTime":"1h",
    "downloadLimit":-1,
    "canAccessWhenAllExpired":false
  },
  "bark":{
    "server":"http://IP:端口",
    "key":"你的key"
  }

}
json

然后可以使用 https://mholt.github.io/json-to-go/ 等类似的服务转成struct

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
type Config struct {
	Port int `json:"port"`
	Db struct {
		DbPath string `json:"dbPath"`
		Timeout int `json:"timeout"`
	} `json:"db"`
	Limit struct {
		MaxSingleFileSize int `json:"maxSingleFileSize"`
		MaxFileCount int `json:"maxFileCount"`
		ExpiredTime string `json:"expiredTime"`
		DownloadLimit int `json:"downloadLimit"`
		CanAccessWhenAllExpired bool `json:"canAccessWhenAllExpired"`
	} `json:"limit"`
	Bark struct {
		Server string `json:"server"`
		Key string `json:"key"`
	} `json:"bark"`
}
go

再写个方法, 搞个全局变量就能用了

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
var (
	Cfg Config
)

func LoadConfig(file string) (err error) {
	var f *os.File
	f, err = os.Open(file)
	defer f.Close()
	if err != nil {
		return
	}
	jsonParser := json.NewDecoder(f)
	return jsonParser.Decode(&Cfg)
}
go

main.go里读取一下, 用 -c /path/to/config.json运行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var (
	configFile string
)

func init() {
	flag.StringVar(&configFile, "c", "config.json", "config json file")
}

func main()  {
  config.LoadConfig(configFile)
}
go

Bark推送

这个就简单了, 当然要先把服务搭起来能用先

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package utils

import (
	"errors"
	"fmt"
	"net/http"
	"qrcodeTransferBox/config"
)

func SendBarkUrl(title string,url string) error {
	if (title == "" || url == "") {
		return errors.New("param cannot be empty")
	}
	link := fmt.Sprintf("%s/%s/点击下载[%s]?url=%s",config.Cfg.Bark.Server,config.Cfg.Bark.Key,title,url)
	go http.Get(link)
	return nil
}

func SendBarkMsg(content string) error {
	if (content == "") {
		return errors.New("param cannot be empty")
	}
	link := fmt.Sprintf("%s/%s/%s",config.Cfg.Bark.Server,config.Cfg.Bark.Key,content)
	go http.Get(link)
	return nil
}
go

主要实现两种方式, 一个是直接推送文本的消息, 另一个用于文件上传完成之后, 可以在手机上点击直接下载或者跳转到相应的pack网址

记录平时瞎折腾遇到的各种问题, 方便查找
使用 Hugo 构建
主题 Stack 3.29.0Jimmy 设计