forked from yukimochi/Activity-Relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (130 loc) · 3.6 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Yet another powerful customizable ActivityPub relay server written in Go.
# Run Activity-Relay
API Server
./Activity-Relay --config /path/to/config.yml server
Job Worker
./Activity-Relay --config /path/to/config.yml worker
CLI Management Utility
./Activity-Relay --config /path/to/config.yml control
# Config
YAML Format
ACTOR_PEM: /var/lib/relay/actor.pem
REDIS_URL: redis://localhost:6379
RELAY_BIND: 0.0.0.0:8080
RELAY_DOMAIN: relay.toot.yukimochi.jp
RELAY_SERVICENAME: YUKIMOCHI Toot Relay Service
JOB_CONCURRENCY: 50
RELAY_SUMMARY: |
YUKIMOCHI Toot Relay Service is Running by Activity-Relay
RELAY_ICON: https://example.com/example_icon.png
RELAY_IMAGE: https://example.com/example_image.png
# Environment Variable
This is Optional : When config file not exist, use environment variables.
- ACTOR_PEM
- REDIS_URL
- RELAY_BIND
- RELAY_DOMAIN
- RELAY_SERVICENAME
- JOB_CONCURRENCY
- RELAY_SUMMARY
- RELAY_ICON
- RELAY_IMAGE
*/
package main
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/yukimochi/Activity-Relay/api"
"github.com/yukimochi/Activity-Relay/control"
"github.com/yukimochi/Activity-Relay/deliver"
"github.com/yukimochi/Activity-Relay/models"
)
var (
version = "devel"
verbose bool
GlobalConfig *models.RelayConfig
)
func main() {
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
})
var app = buildCommand()
app.PersistentFlags().StringP("config", "c", "config.yml", "Path of config")
app.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Show debug log")
app.Execute()
}
func buildCommand() *cobra.Command {
var server = &cobra.Command{
Use: "server",
Short: "Activity-Relay API Server",
Long: "Activity-Relay API Server is providing WebFinger API, ActivityPub inbox",
RunE: func(cmd *cobra.Command, args []string) error {
initConfig(cmd)
fmt.Println(GlobalConfig.DumpWelcomeMessage("API Server", version))
err := api.Entrypoint(GlobalConfig, version)
if err != nil {
logrus.Fatal(err.Error())
}
return nil
},
}
var worker = &cobra.Command{
Use: "worker",
Short: "Activity-Relay Job Worker",
Long: "Activity-Relay Job Worker is providing ActivityPub Activity deliverer",
RunE: func(cmd *cobra.Command, args []string) error {
initConfig(cmd)
fmt.Println(GlobalConfig.DumpWelcomeMessage("Job Worker", version))
err := deliver.Entrypoint(GlobalConfig, version)
if err != nil {
logrus.Fatal(err.Error())
}
return nil
},
}
var command = &cobra.Command{
Use: "control",
Short: "Activity-Relay CLI",
Long: "Activity-Relay CLI Management Utility",
}
control.BuildCommand(command)
var app = &cobra.Command{
Short: "YUKIMOCHI Activity-Relay",
Long: "YUKIMOCHI Activity-Relay - ActivityPub Relay Server",
}
app.AddCommand(server)
app.AddCommand(worker)
app.AddCommand(command)
return app
}
func initConfig(cmd *cobra.Command) {
if verbose {
logrus.SetLevel(logrus.DebugLevel)
}
configPath := cmd.Flag("config").Value.String()
file, err := os.Open(configPath)
defer file.Close()
if err == nil {
viper.SetConfigType("yaml")
viper.ReadConfig(file)
} else {
logrus.Warn("Config file not exist. Use environment variables.")
viper.BindEnv("ACTOR_PEM")
viper.BindEnv("REDIS_URL")
viper.BindEnv("RELAY_BIND")
viper.BindEnv("RELAY_DOMAIN")
viper.BindEnv("RELAY_SERVICENAME")
viper.BindEnv("JOB_CONCURRENCY")
viper.BindEnv("RELAY_SUMMARY")
viper.BindEnv("RELAY_ICON")
viper.BindEnv("RELAY_IMAGE")
}
GlobalConfig, err = models.NewRelayConfig()
if err != nil {
logrus.Fatal(err.Error())
}
}