116 lines
2.7 KiB
Go

package main
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/lilfade/Activity-Relay/app/api"
"github.com/lilfade/Activity-Relay/app/control"
"github.com/lilfade/Activity-Relay/app/deliver"
"github.com/lilfade/Activity-Relay/app/models"
)
var (
version string
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 file.")
app.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Show debug log in stdout.")
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)
fmt.Println("DEBUG VIEW")
}
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())
}
}