镜像自地址
https://github.com/tuna/tunasync.git
已同步 2025-12-06 06:26:46 +00:00
feature(worker): implemented Worker object, worker side code is almost done
这个提交包含在:
@@ -4,7 +4,7 @@ import "time"
|
||||
|
||||
// A StatusUpdateMsg represents a msg when
|
||||
// a worker has done syncing
|
||||
type StatusUpdateMsg struct {
|
||||
type MirrorStatus struct {
|
||||
Name string `json:"name"`
|
||||
Worker string `json:"worker"`
|
||||
IsMaster bool `json:"is_master"`
|
||||
@@ -19,7 +19,9 @@ type StatusUpdateMsg struct {
|
||||
// a worker, and sent from the manager to clients.
|
||||
type WorkerInfoMsg struct {
|
||||
ID string `json:"id"`
|
||||
LastOnline time.Time `json:"last_online"`
|
||||
URL string `json:"url"` // worker url
|
||||
Token string `json:"token"` // session token
|
||||
LastOnline time.Time `json:"last_online"` // last seen
|
||||
}
|
||||
|
||||
type CmdVerb uint8
|
||||
|
||||
79
internal/util.go
普通文件
79
internal/util.go
普通文件
@@ -0,0 +1,79 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// GetTLSConfig generate tls.Config from CAFile
|
||||
func GetTLSConfig(CAFile string) (*tls.Config, error) {
|
||||
caCert, err := ioutil.ReadFile(CAFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
caCertPool := x509.NewCertPool()
|
||||
if ok := caCertPool.AppendCertsFromPEM(caCert); !ok {
|
||||
return nil, errors.New("Failed to add CA to pool")
|
||||
}
|
||||
|
||||
tlsConfig := &tls.Config{
|
||||
RootCAs: caCertPool,
|
||||
}
|
||||
tlsConfig.BuildNameToCertificate()
|
||||
return tlsConfig, nil
|
||||
}
|
||||
|
||||
// PostJSON posts json object to url
|
||||
func PostJSON(url string, obj interface{}, tlsConfig *tls.Config) (*http.Response, error) {
|
||||
var client *http.Client
|
||||
if tlsConfig == nil {
|
||||
client = &http.Client{}
|
||||
} else {
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: tlsConfig,
|
||||
}
|
||||
client = &http.Client{
|
||||
Transport: tr,
|
||||
}
|
||||
}
|
||||
|
||||
b := new(bytes.Buffer)
|
||||
if err := json.NewEncoder(b).Encode(obj); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.Post(url, "application/json; charset=utf-8", b)
|
||||
}
|
||||
|
||||
// GetJSON gets a json response from url
|
||||
func GetJSON(url string, obj interface{}, tlsConfig *tls.Config) (*http.Response, error) {
|
||||
var client *http.Client
|
||||
if tlsConfig == nil {
|
||||
client = &http.Client{}
|
||||
} else {
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: tlsConfig,
|
||||
}
|
||||
client = &http.Client{
|
||||
Transport: tr,
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return resp, errors.New("HTTP status code is not 200")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
return resp, json.Unmarshal(body, obj)
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户