|
| 1 | +/* |
| 2 | +Copyright © 2023 NAME HERE <EMAIL ADDRESS> |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "github.com/everFinance/arseeding" |
| 10 | + "github.com/everFinance/goar/types" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "os/signal" |
| 14 | + "syscall" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +const pidFile string = ".arseeding_pid.lock" |
| 21 | + |
| 22 | +var daemon bool |
| 23 | + |
| 24 | +// startCmd represents the start command |
| 25 | +var startCmd = &cobra.Command{ |
| 26 | + Use: "start", |
| 27 | + Short: "start arseeding", |
| 28 | + Long: `start arseeding`, |
| 29 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | + if daemon { |
| 31 | + if _, err := os.Stat(pidFile); err == nil { |
| 32 | + fmt.Println("Failed start, PID file exist.running...") |
| 33 | + return nil |
| 34 | + } |
| 35 | + |
| 36 | + path, err := os.Executable() |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + command := exec.Command(path, "start") |
| 42 | + |
| 43 | + // add log |
| 44 | + logFileName := fmt.Sprintf("arseeding_%d.log", time.Now().Unix()) |
| 45 | + logFile, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + command.Stdout = logFile |
| 51 | + command.Stderr = logFile |
| 52 | + |
| 53 | + if err := command.Start(); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + err = os.WriteFile(pidFile, []byte(fmt.Sprintf("%d", command.Process.Pid)), 0666) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + daemon = false |
| 62 | + os.Exit(0) |
| 63 | + } else { |
| 64 | + runServer() |
| 65 | + } |
| 66 | + return nil |
| 67 | + }, |
| 68 | +} |
| 69 | + |
| 70 | +func init() { |
| 71 | + rootCmd.AddCommand(startCmd) |
| 72 | + |
| 73 | + startCmd.Flags().BoolVarP(&daemon, "deamon", "d", false, "is daemon?") |
| 74 | + // Here you will define your flags and configuration settings. |
| 75 | + |
| 76 | + // Cobra supports Persistent Flags which will work for this command |
| 77 | + // and all subcommands, e.g.: |
| 78 | + // startCmd.PersistentFlags().String("foo", "", "A help for foo") |
| 79 | + |
| 80 | + // Cobra supports local flags which will only run when this command |
| 81 | + // is called directly, e.g.: |
| 82 | + // startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") |
| 83 | +} |
| 84 | + |
| 85 | +func runServer() { |
| 86 | + signals := make(chan os.Signal, 1) |
| 87 | + signal.Notify(signals, os.Interrupt, syscall.SIGTERM, os.Kill) |
| 88 | + |
| 89 | + useSqlite := false |
| 90 | + sqliteDir := "" |
| 91 | + |
| 92 | + tagJs := cfg.Tags |
| 93 | + if len(tagJs) == 0 { |
| 94 | + tagJs = `{"Community":"PermaDAO","Website":"permadao.com"}` |
| 95 | + } |
| 96 | + tagsMap := make(map[string]string) |
| 97 | + if err := json.Unmarshal([]byte(tagJs), &tagsMap); err != nil { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + |
| 101 | + customTags := make([]types.Tag, 0) |
| 102 | + for k, v := range tagsMap { |
| 103 | + customTags = append(customTags, types.Tag{ |
| 104 | + Name: k, |
| 105 | + Value: v, |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + m := arseeding.New(cfg.BoltDir, cfg.Mysql, sqliteDir, useSqlite, |
| 110 | + cfg.RollupKeyPath, cfg.ArNode, cfg.Pay, cfg.NoFee, cfg.Manifest, |
| 111 | + cfg.S3KV.UseS3, cfg.S3KV.AccKey, cfg.S3KV.SecretKey, cfg.S3KV.Prefix, cfg.S3KV.Region, cfg.S3KV.Endpoint, cfg.S3KV.User4Ever, |
| 112 | + cfg.AliyunKV.UseAliyun, cfg.AliyunKV.Endpoint, cfg.AliyunKV.AccKey, cfg.AliyunKV.SecretKey, cfg.AliyunKV.Prefix, |
| 113 | + cfg.MongoDBKV.UseMongoDB, cfg.MongoDBKV.Uri, |
| 114 | + cfg.Port, customTags, |
| 115 | + cfg.Kafka.Start, cfg.Kafka.Uri) |
| 116 | + |
| 117 | + m.Run(cfg.Port, cfg.BundleInterval) |
| 118 | + |
| 119 | + <-signals |
| 120 | + m.Close() |
| 121 | +} |
0 commit comments