當前位置: 首頁>>代碼示例>>Golang>>正文


Golang backend.NewHandler函數代碼示例

本文整理匯總了Golang中github.com/russmack/elrepl/backend.NewHandler函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewHandler函數的具體用法?Golang NewHandler怎麽用?Golang NewHandler使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewHandler函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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


注:本文中的github.com/russmack/elrepl/backend.NewHandler函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。