本文整理汇总了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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}