• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang backend.NewHandler函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/russmack/elrepl/backend.NewHandler函数的典型用法代码示例。如果您正苦于以下问题:Golang NewHandler函数的具体用法?Golang NewHandler怎么用?Golang NewHandler使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewHandler函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "help"
	h.CommandPattern = "(help)( )(.*)"
	h.Usage = "help"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		cmdList := ""
		keys := []string{}
		for k, _ := range backend.HandlerRegistry {
			keys = append(keys, k)
		}
		sort.Strings(keys)
		for _, s := range keys {
			//cmdList += s + ": " + backend.HandlerRegistry[s].Usage + "\n"
			cmdList += s + "\n"
		}
		return `
	Help
	----


Write commands require host and port be specified to avoid slip-ups.
Read commands can omit host and port, and session settings will be used.


Commands
--------
` + cmdList + `
`, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:32,代码来源:handlehelp.go


示例2: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "run"
	h.CommandPattern = "(run)( )(.*)"
	h.Usage = "run"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		verb := strings.ToLower(cmd.Tokens[0])
		cmdParser := backend.NewCommandParser()
		newCmd, err := cmdParser.Parse(backend.LoadedRequest.Request)
		if err != nil {
			return "Unable to parse loaded query for run command.", false
		}
		dispatcher := backend.NewDispatcher()
		switch verb {
		case "post":
			resp, ok := dispatcher.Dispatch(newCmd)
			return resp, ok
		case "put":
			resp, ok := dispatcher.Dispatch(newCmd)
			return resp, ok
		case "get":
			resp, ok := dispatcher.Dispatch(newCmd)
			return resp, ok
		}
		return "Unable to run loaded query.", false
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:28,代码来源:handlerun.go


示例3: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "get"
	h.CommandPattern = "(get)( )(.*)"
	h.Usage = "get url"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		//queryHost := cmd.Tokens[1]
		//queryPort := cmd.Tokens[2]
		//queryArgs := cmd.Tokens[3]

		arg := cmd.Tokens[1]
		u := new(url.URL)
		newUrl, err := u.Parse(arg)
		if err != nil {
			return "Unable to parse url: " + err.Error(), false
		}
		fmt.Println("Request: get", newUrl)
		res, err := backend.GetHttpResource(newUrl.String())
		if err != nil {
			return err.Error(), false
		}
		return res, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:25,代码来源:handleget.go


示例4: init

// TODO: clean up.
func init() {
	h := backend.NewHandler()
	h.CommandName = "doc"
	h.CommandPattern = "(doc)(( )(.*))"
	h.Usage = "doc (get index docId) | (delete index type docId)"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Doc help
			regexp.MustCompile(`^doc /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Get doc
			regexp.MustCompile(`^doc get ([a-zA-Z0-9\.\-_]+) ([a-zA-Z0-9\.\-_]+)$`): func(s []string) (string, bool) {
				fmt.Println("...", s[0])
				fmt.Println("...", s[1])
				fmt.Println("...", s[2])

				d := backend.Resource{
					Endpoint: "_mget",
					Scheme:   "http",
					Host:     backend.Server.Host,
					Port:     backend.Server.Port,
					Index:    s[1],
					Id:       s[2],
				}
				c := DocCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Delete doc
			regexp.MustCompile(`^doc delete ([a-zA-Z0-9\.\-_]+) ([a-zA-Z0-9\.\-_]+) ([a-zA-Z0-9\.\-_]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Endpoint: "_mget",
					Scheme:   "http",
					Host:     backend.Server.Host,
					Port:     backend.Server.Port,
					Index:    s[0],
					Type:     s[1],
					Id:       s[2],
				}
				c := DocCmd{}
				r, ok := c.Delete(d)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:61,代码来源:handledoc.go


示例5: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "count"
	h.CommandPattern = "(count)( )(.*)"
	h.Usage = "count [host port] indexName [type]"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Get count: require: index; optional: type
			regexp.MustCompile(`^count ([a-zA-Z0-9\.\-]+)(( )([a-zA-Z0-9\.\-]+))?$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme:   "http",
					Host:     backend.Server.Host,
					Port:     backend.Server.Port,
					Index:    s[1],
					Type:     s[4],
					Endpoint: "_count",
				}
				c := CountCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Get count: require: host, port, index; optional: type
			regexp.MustCompile(`^count ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+)(( )([a-zA-Z0-9\.\-]+))?$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme:   "http",
					Host:     s[1],
					Port:     s[2],
					Index:    s[3],
					Type:     s[6],
					Endpoint: "_count",
				}
				c := CountCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Count help
			regexp.MustCompile(`^count /\?$`): func(s []string) (string, bool) {
				return "", false
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:55,代码来源:handlecount.go


示例6: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "log"
	h.CommandPattern = "(log)( )(.*)"
	h.Usage = "log"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		backend.LogLevel = 1
		return "Logging level set to: " + strconv.Itoa(backend.LogLevel), true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:11,代码来源:handlelog.go


示例7: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "exit"
	h.CommandPattern = "(exit)( )(.*)"
	h.Usage = "exit"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		fmt.Println("Bye.")
		os.Exit(0)
		return "", true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:12,代码来源:handleexit.go


示例8: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "index"
	h.CommandPattern = "(index)(( )(.*))"
	h.Usage = "index ( (create host port indexName) | (delete host port indexName) )"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Index help
			regexp.MustCompile(`^index /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Create index
			//curl -XPUT "http://localhost:9200/indexname/"
			regexp.MustCompile(`^index create ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme: "http",
					Host:   s[1],
					Port:   s[2],
					Index:  s[3],
				}
				c := IndexCmd{}
				r, ok := c.Create(d)
				return r, ok
			},
			// Delete index
			regexp.MustCompile(`^index delete ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme: "http",
					Host:   s[1],
					Port:   s[2],
					Index:  s[3],
				}
				c := IndexCmd{}
				r, ok := c.Delete(d)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:52,代码来源:handleindex.go


示例9: init

// reindex localhost:9200/srcindex/type localhost:9200/targetindex/routing
func init() {
	h := backend.NewHandler()
	h.CommandName = "reindex"
	h.CommandPattern = "(reindex)( )(.*)"
	h.Usage = "reindex sourceHost sourcePort sourceIndex sourceType targetHost targetPort targetIndex [routing]"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Index help
			regexp.MustCompile(`^reindex /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Reindex
			regexp.MustCompile(`^reindex ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+)?$`): func(s []string) (string, bool) {
				dSource := backend.Resource{
					Scheme: "http",
					Host:   s[1],
					Port:   s[2],
					Index:  s[3],
					Type:   s[4],
				}
				dTarget := backend.Resource{
					Scheme:  "http",
					Host:    s[5],
					Port:    s[6],
					Index:   s[7],
					Routing: s[8],
				}
				c := ReindexCmd{}
				r, ok := c.Reindex(dSource, dTarget)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		fmt.Println("Reindexing...")
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:49,代码来源:handlereindex.go


示例10: init

// curl -XPUT "http://localhost:9200/movies/movie/1" -d'{ ... body ... }''
// becomes
// put movie/1 { "title": "Alien", "director": "Ridley Scott", "year": 1979, "genres": ["Science fiction"] }
// TODO: Currently, must be on single line.
func init() {
	h := backend.NewHandler()
	h.CommandName = "put"
	h.CommandPattern = "(put)( )(.*)"
	h.Usage = `put movie/1 { "title": "Alien", "director": "Ridley Scott", "year": 1979, "genres": ["Science fiction"] }`
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		queryHost := cmd.Tokens[1]
		queryPort := cmd.Tokens[2]
		queryArgs := cmd.Tokens[3]
		url := fmt.Sprintf("http://%s:%s/%s", queryHost, queryPort, queryArgs)
		//url := fmt.Sprintf("http://%s:%s/%s/%s", queryHost, queryPort, backend.Server.Index, queryArgs)
		fmt.Println("Request: put", url)
		res, err := backend.PutHttpResource(url, cmd.Body)
		if err != nil {
			return err.Error(), false
		}
		return res, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:24,代码来源:handleput.go


示例11: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "load"
	h.CommandPattern = "(load)( )(.*)"
	h.Usage = "load filename"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		if len(cmd.Tokens) != 2 {
			return h.Usage, false
		}
		filename := cmd.Tokens[1]
		file, err := ioutil.ReadFile(filename)
		if err != nil {
			return err.Error(), false
		}
		fileText := string(file)
		backend.LoadedRequest.Request = fileText
		return fileText, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:20,代码来源:handleload.go


示例12: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "duplicatescount"
	h.CommandPattern = "(duplicatescount)(( )(.*))"
	h.Usage = "duplicatescount host port index type field"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Duplicatescount help
			regexp.MustCompile(`^duplicatescount /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Count duplicates
			regexp.MustCompile(`^duplicatescount ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Host:  s[1],
					Port:  s[2],
					Index: s[3],
					Type:  s[4],
					Field: s[5],
				}
				c := DuplicatesCountCmd{}
				r, ok := c.Do(d)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		fmt.Println("Finding duplicates...")
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:41,代码来源:handleduplicatescount.go


示例13: init

// curl -XPOST "http://localhost:9200/movies/_search?pretty" -d'{ ... body ... }''
// becomes
// post _search?pretty { "query": { "term": { "director": "scott" } } }
// TODO: Currently, must be on single line.
func init() {
	h := backend.NewHandler()
	h.CommandName = "post"
	h.CommandPattern = "((?i)post(?-i))( )(.*)"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		fmt.Println("...>", cmd.Tokens)

		queryHost := cmd.Tokens[1]
		queryPort := cmd.Tokens[2]
		queryArgs := cmd.Tokens[3]
		url := fmt.Sprintf("http://%s:%s/%s", queryHost, queryPort, queryArgs)
		fmt.Println("Request: post", url)
		fmt.Println("Request: post body", cmd.Body)
		res, err := backend.PostHttpResource(url, cmd.Body)
		if err != nil {
			return err.Error(), false
		}
		return res, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:25,代码来源:handlepost.go


示例14: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "settings"
	h.CommandPattern = "(settings)( )(.*)"
	h.Usage = "settings indexName"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Get settings
			regexp.MustCompile(`^settings ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme:   "http",
					Host:     backend.Server.Host,
					Port:     backend.Server.Port,
					Index:    s[1],
					Endpoint: "_settings",
				}
				c := SettingsCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Settings help
			regexp.MustCompile(`^settings /\?$`): func(s []string) (string, bool) {
				return "", false
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:40,代码来源:handlesettings.go


示例15: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "delete"
	h.CommandPattern = "(delete)( )(.*)"
	// TODO: usage
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		//arg := cmd.Args[0]
		arg := strings.SplitN(cmd.Instruction, " ", 2)[1]
		u := new(url.URL)
		newUrl, err := u.Parse(arg)
		if err != nil {
			return "Unable to parse url: " + err.Error(), false
		}
		fmt.Println("Request:", newUrl)
		res, err := backend.DeleteHttpResource(newUrl.String())
		if err != nil {
			return err.Error(), false
		}
		return res, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:22,代码来源:handledelete.go


示例16: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "version"
	h.CommandPattern = "(version)(( )(.*))"
	h.Usage = "version host port"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Get version
			regexp.MustCompile(`^version ([a-zA-Z0-9\.\-]+) ([0-9]{1,5})$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme: "http",
					Host:   s[1],
					Port:   s[2],
				}
				c := VersionCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Version help
			regexp.MustCompile(`^version /\?$`): func(s []string) (string, bool) {
				return "", false
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:38,代码来源:handleversion.go


示例17: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "dir"
	h.CommandPattern = "(dir)( )(.*)"
	h.Usage = "dir"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		dir := ""
		if len(cmd.Tokens) == 1 {
			dir = "."
		} else {
			dir = cmd.Tokens[1]
		}
		dirFiles, err := ioutil.ReadDir(dir)
		if err != nil {
			return err.Error(), false
		}
		files := ""
		for _, j := range dirFiles {
			files += j.Name() + "\n"
		}
		return files, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:24,代码来源:handledir.go


示例18: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "alias"
	h.CommandPattern = "(alias)( )(.*)"
	h.Usage = "alias\n" +
		"  alias host port\n" +
		"  alias create host port indexName aliasName\n" +
		"  alias remove host port indexName aliasName\n" +
		"  alias move host port fromIndex toIndex aliasName"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// List all aliases on host
			regexp.MustCompile(`^alias$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Endpoint: "_aliases",
					Scheme:   "http",
					Host:     backend.Server.Host,
					Port:     backend.Server.Port,
				}
				c := AliasCmd{}
				r, ok := c.GetAll(d)
				return r, ok
			},
			regexp.MustCompile(`^alias ([a-zA-Z0-9\.\-]+) ([0-9]{1,5})$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Endpoint: "_aliases",
					Scheme:   "http",
					Host:     s[1],
					Port:     s[2],
				}
				c := AliasCmd{}
				r, ok := c.GetAll(d)
				return r, ok
			},
			// Alias help
			regexp.MustCompile(`^alias /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Create alias
			regexp.MustCompile(`^alias create ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Endpoint: "_aliases",
					Host:     s[1],
					Port:     s[2],
					Index:    s[3],
					Alias:    s[4],
				}
				c := AliasCmd{}
				r, ok := c.Create(d)
				return r, ok
			},
			// Remove alias
			regexp.MustCompile(`^alias remove ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Endpoint: "_aliases",
					Host:     s[1],
					Port:     s[2],
					Index:    s[3],
					Alias:    s[4],
				}
				c := AliasCmd{}
				r, ok := c.Remove(d)
				return r, ok
			},
			// Move alias
			regexp.MustCompile(`^alias move ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				dFrom := backend.Resource{
					Endpoint: "_aliases",
					Host:     s[1],
					Port:     s[2],
					Index:    s[3],
					Alias:    s[5],
				}
				dTarget := backend.Resource{
					Index: s[4],
					Alias: s[5],
				}
				c := AliasCmd{}
				r, ok := c.Move(dFrom, dTarget)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:97,代码来源:handlealias.go


示例19: init

// reindex localhost:9200/srcindex/type localhost:9200/targetindex/routing
func init() {
	h := backend.NewHandler()
	h.CommandName = "update"
	h.CommandPattern = "(update)( )(.*)"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		fmt.Println("Updating...")
		//args := strings.TrimPrefix(entry, CommandReindex+" ")
		//args := cmd.Args[0]
		args := strings.SplitN(cmd.Instruction, " ", 2)[1]

		// \w+|"[\w\s]*"
		r, err := regexp.Compile(`^(.*?):(\d+?)/(.*?)/(.*?)/? (.*?):(\d+?)/(.*?)(/(.*?))?$`)
		if err != nil {
			return err.Error(), false
		}
		fmt.Println("Parsing command...")
		matches := r.FindAllStringSubmatch(args, -1)[0]
		fmt.Println("Parsed matches:", len(matches))
		srcHost := matches[1]
		srcPort := matches[2]
		srcIndex := matches[3]
		srcType := matches[4]
		tgtHost := matches[5]
		tgtPort := matches[6]
		tgtIndex := matches[7]
		tgtRouting := matches[9]

		api := elastigo.NewConn()
		api.Domain = srcHost
		api.Port = srcPort

		fmt.Println("Scanning...")
		scanArgs := map[string]interface{}{"search_type": "scan", "scroll": "1m", "size": "1000"}
		scanResult, err := api.SearchUri(srcIndex, srcType, scanArgs)
		if err != nil {
			fmt.Println("Failed getting scan result for index:", srcIndex, "; err:", err)
			return err.Error(), false
		}

		//total := scanResult.Hits.Total

		scrollId := scanResult.ScrollId
		counter := 0
		failures := 0

		fmt.Println("Scrolling...")
		scrollArgs := map[string]interface{}{"scroll": "1m"}
		scrollResult, err := api.Scroll(scrollArgs, scrollId)
		if err != nil {
			fmt.Println("Failed getting scroll result for index:", srcIndex, "; err:", err)
			return err.Error(), false
		}

		fmt.Println("Indexing...")
		var indexArgs map[string]interface{} = nil
		if tgtRouting != "" {
			indexArgs = map[string]interface{}{"routing": tgtRouting}
		}
		for len(scrollResult.Hits.Hits) > 0 {
			fmt.Println("Scroll result hits:", len(scrollResult.Hits.Hits))
			for _, j := range scrollResult.Hits.Hits {
				api.Domain = tgtHost
				api.Port = tgtPort

				_, err := api.Index(tgtIndex, srcType, j.Id, indexArgs, j.Source)
				if err != nil {
					fmt.Println("Failed inserting document, id:", j.Id, "; ", err)
					failures++
					continue
				}
				counter++
			}

			api.Domain = srcHost
			api.Port = srcPort
			// ScrollId changes with every request.
			scrollId = scrollResult.ScrollId
			scrollArgs := map[string]interface{}{"scroll": "1m"}
			scrollResult, err = api.Scroll(scrollArgs, scrollId)
			if err != nil {
				fmt.Println("Failed getting scroll result for index:", srcIndex, "; err:", err)
				return err.Error(), false
			}
		}
		return fmt.Sprintf("Total processed: %d.  %d failed.", counter, failures), true
	}
	// TODO: implement this command and uncomment the registration below.
	//backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:90,代码来源:handleupdate.go


示例20: init

func init() {
	h := backend.NewHandler()
	h.CommandName = "env"
	h.CommandPattern = "(env)(( )(.*))"
	h.Usage = "env [ (host hostname) | (port portnumber) | (index indexname ) ]"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Get env
			regexp.MustCompile(`^env$`): func(s []string) (string, bool) {
				d := backend.Resource{}
				c := EnvCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Env help
			regexp.MustCompile(`^env /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Set env host
			regexp.MustCompile(`^env host ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Host: s[1],
				}
				c := EnvCmd{}
				r, ok := c.SetHost(d)
				return r, ok
			},
			// Set env port
			regexp.MustCompile(`^env port ([0-9]{1,5})$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Port: s[1],
				}
				c := EnvCmd{}
				r, ok := c.SetPort(d)
				return r, ok
			},
			// Set env index
			regexp.MustCompile(`^env index ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Index: s[1],
				}
				c := EnvCmd{}
				r, ok := c.SetIndex(d)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
开发者ID:russmack,项目名称:elrepl,代码行数:61,代码来源:handleenv.go



注:本文中的github.com/russmack/elrepl/backend.NewHandler函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang blackfriday.HtmlRenderer函数代码示例发布时间:2022-05-28
下一篇:
Golang log.Infof函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap