forked from yukimochi/Activity-Relay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
98 lines (87 loc) · 2.37 KB
/
main.go
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"errors"
"io/ioutil"
"os"
"os/signal"
"syscall"
"fmt"
"b612.me/starmap"
"github.com/starainrt/Activity-Relay/cli"
"github.com/starainrt/Activity-Relay/conf"
"github.com/starainrt/Activity-Relay/server"
"github.com/starainrt/Activity-Relay/worker"
"b612.me/starlog"
"b612.me/staros"
"github.com/spf13/cobra"
)
func init() {
cli.BuildNewCmd(cmdStart)
cmdStart.Flags().StringP("config", "c", "./config/config.ini", "Configure Path")
}
func main() {
cmdStart.Execute()
}
var cmdStart = &cobra.Command{
Use: "",
Short: "Just a Small Relay",
Long: "Relay",
RunE: func(cmd *cobra.Command, args []string) error {
configPath, _ := cmd.Flags().GetString("config")
err := loadConfigure(configPath)
if err != nil {
starlog.Errorln(err)
return err
}
stopCtx, stopFn := context.WithCancel(context.Background())
workChan := make(chan int)
err = ioutil.WriteFile("./config/relay.pid", []byte(fmt.Sprint(os.Getpid())), 0755)
if err != nil {
starlog.Errorln("Cannot Write Pid File", err)
return err
}
go server.Run(stopCtx)
go worker.Run(workChan)
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt, os.Kill)
reloadSig := make(chan os.Signal)
signal.Notify(reloadSig, syscall.SIGUSR1)
for {
select {
case <-sig:
stopFn()
<-workChan
return nil
case <-workChan:
panic("quit unexpect!")
case <-reloadSig:
starlog.Infoln("Ok,Recv Reload Cfg Sig,Please Wait")
starlog.Noticeln("Please Note: Only Rules Can be Updated!")
err := loadConfigure(configPath)
if err != nil {
starlog.Errorln(err)
}
starlog.Infoln("Load Config Success!")
}
}
return nil
},
}
func loadConfigure(configPath string) error {
if !staros.Exists(configPath) {
starlog.Criticalln("Cannot Found Config File,Please Check")
return errors.New("Config File Not Exists")
}
starlog.Noticeln("Parsing Config File……")
if err := conf.Parse(configPath); err != nil {
starlog.Criticalln("Cannot Parse Configure File:", err)
return err
}
cfg := starmap.MustGet("config").(conf.RelayConfig)
cfg.Version = "1.0.0"
starmap.Store("ua", fmt.Sprintf("ActivityPub-Relay V%s +https://%s", cfg.Version, cfg.Domain))
starmap.Store("config", cfg)
fmt.Printf("Load Config :%+v\n", cfg)
return nil
}