Skip to content

Commit

Permalink
Merge pull request #94 from everFinance/feature/add-cli
Browse files Browse the repository at this point in the history
feat(): add arseeding cli
  • Loading branch information
zyjblockchain authored Feb 23, 2024
2 parents 0c91349 + 01c3405 commit 6c8945b
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 28 deletions.
8 changes: 4 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ func (s *Arseeding) runAPI(port string) {
}

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

if err := r.Run(port); err != nil {
panic(err)
Expand Down Expand Up @@ -420,7 +420,7 @@ func (s *Arseeding) postTask(c *gin.Context) {
return
}
tkType := c.Param("taskType")
if !strings.Contains(schema.TaskTypeSync+schema.TaskTypeBroadcast+schema.TaskTypeBroadcastMeta, tkType) {
if !strings.Contains(schema.TaskTypeSync+schema.TaskTypeBroadcast+schema.TaskTypeBroadcastMeta+schema.TaskTypeSyncManifest, tkType) {
errorResponse(c, "tktype not exist")
return
}
Expand Down Expand Up @@ -455,7 +455,7 @@ func (s *Arseeding) killTask(c *gin.Context) {
func (s *Arseeding) getTask(c *gin.Context) {
arid := c.Param("arid")
tktype := c.Param("taskType")
if !strings.Contains(schema.TaskTypeSync+schema.TaskTypeBroadcast+schema.TaskTypeBroadcastMeta, tktype) {
if !strings.Contains(schema.TaskTypeSync+schema.TaskTypeBroadcast+schema.TaskTypeBroadcastMeta+schema.TaskTypeSyncManifest, tktype) {
errorResponse(c, "tktype not exist")
return
}
Expand Down
1 change: 1 addition & 0 deletions arseeding.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func (s *Arseeding) Close() {
for _, k := range s.KWriters {
k.Close()
}
log.Warn("arseeding closed")
}

func (s *Arseeding) GetPerFee(tokenSymbol string) *schema.Fee {
Expand Down
77 changes: 77 additions & 0 deletions cli/arseeding/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"github.com/everFinance/arseeding/schema"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string
var cfg schema.Config

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "arseeding",
Short: "arseeding",
Long: `arseeding`,
Version: "v1.2.3",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "cfg", "", "cfg file (default is $./arseeding.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in cfg file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use cfg file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Search cfg in home directory with name ".arseeding" (without extension).
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
viper.SetConfigName("arseeding")
}

viper.AutomaticEnv() // read in environment variables that match

// If a cfg file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
} else {
fmt.Println("can not find config file")
panic(err)
}

if err := viper.Unmarshal(&cfg); err != nil {
panic(err)
}
}
121 changes: 121 additions & 0 deletions cli/arseeding/cmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"encoding/json"
"fmt"
"github.com/everFinance/arseeding"
"github.com/everFinance/goar/types"
"os"
"os/exec"
"os/signal"
"syscall"
"time"

"github.com/spf13/cobra"
)

const pidFile string = ".arseeding_pid.lock"

var daemon bool

// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "start arseeding",
Long: `start arseeding`,
RunE: func(cmd *cobra.Command, args []string) error {
if daemon {
if _, err := os.Stat(pidFile); err == nil {
fmt.Println("Failed start, PID file exist.running...")
return nil
}

path, err := os.Executable()
if err != nil {
return err
}

command := exec.Command(path, "start")

// add log
logFileName := fmt.Sprintf("arseeding_%d.log", time.Now().Unix())
logFile, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return err
}

command.Stdout = logFile
command.Stderr = logFile

if err := command.Start(); err != nil {
return err
}
err = os.WriteFile(pidFile, []byte(fmt.Sprintf("%d", command.Process.Pid)), 0666)
if err != nil {
return err
}

daemon = false
os.Exit(0)
} else {
runServer()
}
return nil
},
}

func init() {
rootCmd.AddCommand(startCmd)

startCmd.Flags().BoolVarP(&daemon, "deamon", "d", false, "is daemon?")
// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// startCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func runServer() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, os.Kill)

useSqlite := false
sqliteDir := ""

tagJs := cfg.Tags
if len(tagJs) == 0 {
tagJs = `{"Community":"PermaDAO","Website":"permadao.com"}`
}
tagsMap := make(map[string]string)
if err := json.Unmarshal([]byte(tagJs), &tagsMap); err != nil {
panic(err)
}

customTags := make([]types.Tag, 0)
for k, v := range tagsMap {
customTags = append(customTags, types.Tag{
Name: k,
Value: v,
})
}

m := arseeding.New(cfg.BoltDir, cfg.Mysql, sqliteDir, useSqlite,
cfg.RollupKeyPath, cfg.ArNode, cfg.Pay, cfg.NoFee, cfg.Manifest,
cfg.S3KV.UseS3, cfg.S3KV.AccKey, cfg.S3KV.SecretKey, cfg.S3KV.Prefix, cfg.S3KV.Region, cfg.S3KV.Endpoint, cfg.S3KV.User4Ever,
cfg.AliyunKV.UseAliyun, cfg.AliyunKV.Endpoint, cfg.AliyunKV.AccKey, cfg.AliyunKV.SecretKey, cfg.AliyunKV.Prefix,
cfg.MongoDBKV.UseMongoDB, cfg.MongoDBKV.Uri,
cfg.Port, customTags,
cfg.Kafka.Start, cfg.Kafka.Uri)

m.Run(cfg.Port, cfg.BundleInterval)

<-signals
m.Close()
}
49 changes: 49 additions & 0 deletions cli/arseeding/cmd/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"os"
"os/exec"

"github.com/spf13/cobra"
)

// stopCmd represents the stop command
var stopCmd = &cobra.Command{
Use: "stop",
Short: "stop arseeding",
Long: `stop arseeding`,
RunE: func(cmd *cobra.Command, args []string) error {
strb, err := os.ReadFile(pidFile)
if err != nil {
fmt.Println("Stop server failed, err: %v", err)
return nil
}
command := exec.Command("kill", string(strb))
if err := command.Start(); err != nil {
return err
}
if err := os.Remove(pidFile); err != nil {
return err
}
println("arseeding stopped")
return nil
},
}

func init() {
rootCmd.AddCommand(stopCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// stopCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// stopCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
16 changes: 16 additions & 0 deletions cli/arseeding/example_arseeding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rollupKeyPath: ./data/arweave-keyfile-permaweb.json
pay: https://api.everpay.io
arNode: https://arweave.net
mysql: arseeding:7YXMHxKeQmbeCpnD@(127.0.0.1:13306)/arseeding?charset=utf8mb4&parseTime=True&loc=Local
port: :6080
manifest: true
noFee: false
bundleInterval: 120
boltDir: ./data
s3KV:
useS3: true
user4Ever: false
accKey: AKIAR63DMM2P4DGMWZU5
secretKey: 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
prefix: public-arseed
region: ap-southeast-1
10 changes: 10 additions & 0 deletions cli/arseeding/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package main

import "github.com/everFinance/arseeding/cli/arseeding/cmd"

func main() {
cmd.Execute()
}
Loading

0 comments on commit 6c8945b

Please sign in to comment.