Skip to content

Commit 6c8945b

Browse files
Merge pull request #94 from everFinance/feature/add-cli
feat(): add arseeding cli
2 parents 0c91349 + 01c3405 commit 6c8945b

File tree

13 files changed

+394
-28
lines changed

13 files changed

+394
-28
lines changed

api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ func (s *Arseeding) runAPI(port string) {
114114
}
115115

116116
go func() {
117-
gLog.Fatal(http.ListenAndServe(":8081", handlers.CompressHandler(http.DefaultServeMux)))
117+
gLog.Fatal(http.ListenAndServe(":8061", handlers.CompressHandler(http.DefaultServeMux)))
118118
}()
119-
gLog.Printf("you can now open http://localhost:8080/debug/charts/ in your browser")
119+
gLog.Printf("you can now open http://localhost:8061/debug/charts/ in your browser")
120120

121121
if err := r.Run(port); err != nil {
122122
panic(err)
@@ -420,7 +420,7 @@ func (s *Arseeding) postTask(c *gin.Context) {
420420
return
421421
}
422422
tkType := c.Param("taskType")
423-
if !strings.Contains(schema.TaskTypeSync+schema.TaskTypeBroadcast+schema.TaskTypeBroadcastMeta, tkType) {
423+
if !strings.Contains(schema.TaskTypeSync+schema.TaskTypeBroadcast+schema.TaskTypeBroadcastMeta+schema.TaskTypeSyncManifest, tkType) {
424424
errorResponse(c, "tktype not exist")
425425
return
426426
}
@@ -455,7 +455,7 @@ func (s *Arseeding) killTask(c *gin.Context) {
455455
func (s *Arseeding) getTask(c *gin.Context) {
456456
arid := c.Param("arid")
457457
tktype := c.Param("taskType")
458-
if !strings.Contains(schema.TaskTypeSync+schema.TaskTypeBroadcast+schema.TaskTypeBroadcastMeta, tktype) {
458+
if !strings.Contains(schema.TaskTypeSync+schema.TaskTypeBroadcast+schema.TaskTypeBroadcastMeta+schema.TaskTypeSyncManifest, tktype) {
459459
errorResponse(c, "tktype not exist")
460460
return
461461
}

arseeding.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func (s *Arseeding) Close() {
171171
for _, k := range s.KWriters {
172172
k.Close()
173173
}
174+
log.Warn("arseeding closed")
174175
}
175176

176177
func (s *Arseeding) GetPerFee(tokenSymbol string) *schema.Fee {

cli/arseeding/cmd/root.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
"github.com/everFinance/arseeding/schema"
9+
"os"
10+
11+
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
13+
)
14+
15+
var cfgFile string
16+
var cfg schema.Config
17+
18+
// rootCmd represents the base command when called without any subcommands
19+
var rootCmd = &cobra.Command{
20+
Use: "arseeding",
21+
Short: "arseeding",
22+
Long: `arseeding`,
23+
Version: "v1.2.3",
24+
// Uncomment the following line if your bare application
25+
// has an action associated with it:
26+
// Run: func(cmd *cobra.Command, args []string) { },
27+
}
28+
29+
// Execute adds all child commands to the root command and sets flags appropriately.
30+
// This is called by main.main(). It only needs to happen once to the rootCmd.
31+
func Execute() {
32+
err := rootCmd.Execute()
33+
if err != nil {
34+
os.Exit(1)
35+
}
36+
}
37+
38+
func init() {
39+
cobra.OnInitialize(initConfig)
40+
41+
// Here you will define your flags and configuration settings.
42+
// Cobra supports persistent flags, which, if defined here,
43+
// will be global for your application.
44+
45+
rootCmd.PersistentFlags().StringVar(&cfgFile, "cfg", "", "cfg file (default is $./arseeding.yaml)")
46+
47+
// Cobra also supports local flags, which will only run
48+
// when this action is called directly.
49+
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
50+
}
51+
52+
// initConfig reads in cfg file and ENV variables if set.
53+
func initConfig() {
54+
if cfgFile != "" {
55+
// Use cfg file from the flag.
56+
viper.SetConfigFile(cfgFile)
57+
} else {
58+
// Search cfg in home directory with name ".arseeding" (without extension).
59+
viper.AddConfigPath(".")
60+
viper.SetConfigType("yaml")
61+
viper.SetConfigName("arseeding")
62+
}
63+
64+
viper.AutomaticEnv() // read in environment variables that match
65+
66+
// If a cfg file is found, read it in.
67+
if err := viper.ReadInConfig(); err == nil {
68+
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
69+
} else {
70+
fmt.Println("can not find config file")
71+
panic(err)
72+
}
73+
74+
if err := viper.Unmarshal(&cfg); err != nil {
75+
panic(err)
76+
}
77+
}

cli/arseeding/cmd/start.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}

cli/arseeding/cmd/stop.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"os/exec"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
// stopCmd represents the stop command
15+
var stopCmd = &cobra.Command{
16+
Use: "stop",
17+
Short: "stop arseeding",
18+
Long: `stop arseeding`,
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
strb, err := os.ReadFile(pidFile)
21+
if err != nil {
22+
fmt.Println("Stop server failed, err: %v", err)
23+
return nil
24+
}
25+
command := exec.Command("kill", string(strb))
26+
if err := command.Start(); err != nil {
27+
return err
28+
}
29+
if err := os.Remove(pidFile); err != nil {
30+
return err
31+
}
32+
println("arseeding stopped")
33+
return nil
34+
},
35+
}
36+
37+
func init() {
38+
rootCmd.AddCommand(stopCmd)
39+
40+
// Here you will define your flags and configuration settings.
41+
42+
// Cobra supports Persistent Flags which will work for this command
43+
// and all subcommands, e.g.:
44+
// stopCmd.PersistentFlags().String("foo", "", "A help for foo")
45+
46+
// Cobra supports local flags which will only run when this command
47+
// is called directly, e.g.:
48+
// stopCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
49+
}

cli/arseeding/example_arseeding.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
rollupKeyPath: ./data/arweave-keyfile-permaweb.json
2+
pay: https://api.everpay.io
3+
arNode: https://arweave.net
4+
mysql: arseeding:7YXMHxKeQmbeCpnD@(127.0.0.1:13306)/arseeding?charset=utf8mb4&parseTime=True&loc=Local
5+
port: :6080
6+
manifest: true
7+
noFee: false
8+
bundleInterval: 120
9+
boltDir: ./data
10+
s3KV:
11+
useS3: true
12+
user4Ever: false
13+
accKey: AKIAR63DMM2P4DGMWZU5
14+
secretKey: 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
15+
prefix: public-arseed
16+
region: ap-southeast-1

cli/arseeding/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package main
5+
6+
import "github.com/everFinance/arseeding/cli/arseeding/cmd"
7+
8+
func main() {
9+
cmd.Execute()
10+
}

0 commit comments

Comments
 (0)