func main() { flag.Parse() expvars.Set("indexes", bleveHttp.IndexStats()) // open the index tpbIndex, err := bleve.Open(*indexPath) if err == bleve.ErrorIndexPathDoesNotExist { log.Printf("Creating new index...") // create a mapping indexMapping := buildIndexMapping() // use the mapping tpbIndex, err = bleve.New(*indexPath, indexMapping) if err != nil { log.Fatal(err) } // index data in the background go func() { err = indexTPB(tpbIndex) if err != nil { log.Fatal(err) } }() } else if err != nil { log.Fatal(err) } else { log.Printf("Opening existing index...") } // create a router to serve static files router := staticFileRouter() // add the API bleveHttp.RegisterIndexName("tpb", tpbIndex) listIndexesHandler := bleveHttp.NewListIndexesHandler() router.Handle("/api", listIndexesHandler).Methods("GET") getIndexHandler := bleveHttp.NewGetIndexHandler() getIndexHandler.IndexNameLookup = indexNameLookup router.Handle("/api/tpb", getIndexHandler).Methods("GET") searchHandler := bleveHttp.NewSearchHandler("tpb") router.Handle("/api/search", searchHandler).Methods("POST") listFieldsHandler := bleveHttp.NewListFieldsHandler("tpb") router.Handle("/api/fields", listFieldsHandler).Methods("GET") docCountHandler := bleveHttp.NewDocCountHandler("tpb") router.Handle("/api/count", docCountHandler).Methods("GET") docGetHandler := bleveHttp.NewDocGetHandler("tpb") docGetHandler.DocIDLookup = docIDLookup router.Handle("/api/tpb/{docID}", docGetHandler).Methods("GET") debugHandler := bleveHttp.NewDebugDocumentHandler("tpb") debugHandler.DocIDLookup = docIDLookup router.Handle("/api/tpb/{docID}/_debug", debugHandler).Methods("GET") aliasHandler := bleveHttp.NewAliasHandler() router.Handle("/api/_aliases", aliasHandler).Methods("POST") // start the HTTP server http.Handle("/", router) bind = fmt.Sprintf("%s:%d", *host, *port) log.Printf("Listening on http://%v", bind) log.Fatal(http.ListenAndServe(bind, nil)) }
func main() { flag.Parse() if flags.Help { flag.Usage() os.Exit(2) } if flags.Version { fmt.Printf("%s main: %s, data: %s\n", path.Base(os.Args[0]), VERSION, cbgt.VERSION) os.Exit(0) } if os.Getenv("GOMAXPROCS") == "" { runtime.GOMAXPROCS(runtime.NumCPU()) } mr, err := cbgt.NewMsgRing(os.Stderr, 1000) if err != nil { log.Fatalf("main: could not create MsgRing, err: %v", err) } log.SetOutput(mr) log.Printf("main: %s started (%s/%s)", os.Args[0], VERSION, cbgt.VERSION) rand.Seed(time.Now().UTC().UnixNano()) go cmd.DumpOnSignalForPlatform() MainWelcome(flagAliases) s, err := os.Stat(flags.DataDir) if err != nil { if os.IsNotExist(err) { if flags.DataDir == DEFAULT_DATA_DIR { log.Printf("main: creating data directory, dataDir: %s", flags.DataDir) err = os.Mkdir(flags.DataDir, 0700) if err != nil { log.Fatalf("main: could not create data directory,"+ " dataDir: %s, err: %v", flags.DataDir, err) } } else { log.Fatalf("main: data directory does not exist,"+ " dataDir: %s", flags.DataDir) return } } else { log.Fatalf("main: could not access data directory,"+ " dataDir: %s, err: %v", flags.DataDir, err) return } } else { if !s.IsDir() { log.Fatalf("main: not a directory, dataDir: %s", flags.DataDir) return } } wd, err := os.Getwd() if err != nil { log.Fatalf("main: os.Getwd, err: %#v", err) return } log.Printf("main: curr dir: %q", wd) dataDirAbs, err := filepath.Abs(flags.DataDir) if err != nil { log.Fatalf("main: filepath.Abs, err: %#v", err) return } log.Printf("main: data dir: %q", dataDirAbs) // If cfg is down, we error, leaving it to some user-supplied // outside watchdog to backoff and restart/retry. cfg, err := cmd.MainCfg(cmdName, flags.CfgConnect, flags.BindHttp, flags.Register, flags.DataDir) if err != nil { if err == cmd.ErrorBindHttp { log.Fatalf("%v", err) return } log.Fatalf("main: could not start cfg, cfgConnect: %s, err: %v\n"+ " Please check that your -cfg/-cfgConnect parameter (%q)\n"+ " is correct and/or that your configuration provider\n"+ " is available.", flags.CfgConnect, err, flags.CfgConnect) return } uuid := flags.UUID if uuid != "" { uuidPath := flags.DataDir + string(os.PathSeparator) + cmdName + ".uuid" err = ioutil.WriteFile(uuidPath, []byte(uuid), 0600) if err != nil { log.Fatalf("main: could not write uuidPath: %s\n"+ " Please check that your -data/-dataDir parameter (%q)\n"+ " is to a writable directory where %s can persist data.", uuidPath, flags.DataDir, cmdName) return } } if uuid == "" { uuid, err = cmd.MainUUID(cmdName, flags.DataDir) if err != nil { log.Fatalf(fmt.Sprintf("%v", err)) return } } var tagsArr []string if flags.Tags != "" { tagsArr = strings.Split(flags.Tags, ",") } expvars.Set("indexes", bleveHttp.IndexStats()) router, err := MainStart(cfg, uuid, tagsArr, flags.Container, flags.Weight, flags.Extra, flags.BindHttp, flags.DataDir, flags.StaticDir, flags.StaticETag, flags.Server, flags.Register, mr, flags.Options) if err != nil { log.Fatal(err) } if flags.Register == "unknown" { log.Printf("main: unregistered node; now exiting") os.Exit(0) } http.Handle("/", router) log.Printf("main: listening on: %s", flags.BindHttp) u := flags.BindHttp if u[0] == ':' { u = "localhost" + u } if strings.HasPrefix(u, "0.0.0.0:") { u = "localhost" + u[len("0.0.0.0"):] } log.Printf("------------------------------------------------------------") log.Printf("web UI / REST API is available: http://%s", u) log.Printf("------------------------------------------------------------") err = http.ListenAndServe(flags.BindHttp, nil) if err != nil { log.Fatalf("main: listen, err: %v\n"+ " Please check that your -bindHttp parameter (%q)\n"+ " is correct and available.", err, flags.BindHttp) } }
func main() { flag.Parse() // walk the data dir and register index names dirEntries, err := ioutil.ReadDir(*dataDir) if err != nil { log.Fatalf("error reading data dir: %v", err) } expvars.Set("indexes", bleveHttp.IndexStats()) for _, dirInfo := range dirEntries { indexPath := *dataDir + string(os.PathSeparator) + dirInfo.Name() // skip single files in data dir since a valid index is a directory that // contains multiple files if !dirInfo.IsDir() { log.Printf("not registering %s, skipping", indexPath) continue } i, err := bleve.Open(indexPath) if err != nil { log.Printf("error opening index %s: %v", indexPath, err) } else { log.Printf("registered index: %s", dirInfo.Name()) bleveHttp.RegisterIndexName(dirInfo.Name(), i) } } router := mux.NewRouter() router.StrictSlash(true) // first install handlers from bleve/http/mapping, for precedence bleveHttpMappingStatic := http.FileServer(bleveHttpMapping.AssetFS()) staticPathDev := *staticPath + "../../bleve/http/mapping/mapping_static/" fi, err := os.Stat(staticPathDev) if err == nil && fi.IsDir() { log.Printf("using dev static resources from %s", staticPathDev) bleveHttpMappingStatic = http.FileServer(http.Dir(staticPathDev)) } router.PathPrefix("/static/partials/analysis").Handler( http.StripPrefix("/static/", bleveHttpMappingStatic)) router.PathPrefix("/static/partials/mapping").Handler( http.StripPrefix("/static/", bleveHttpMappingStatic)) router.PathPrefix("/static/js/mapping").Handler( http.StripPrefix("/static/", bleveHttpMappingStatic)) bleveHttpMapping.RegisterHandlers(router, "/api") // next, install our static file handlers staticFileRouter(router) // add the API createIndexHandler := bleveHttp.NewCreateIndexHandler(*dataDir) createIndexHandler.IndexNameLookup = indexNameLookup router.Handle("/api/{indexName}", createIndexHandler).Methods("PUT") getIndexHandler := bleveHttp.NewGetIndexHandler() getIndexHandler.IndexNameLookup = indexNameLookup router.Handle("/api/{indexName}", getIndexHandler).Methods("GET") deleteIndexHandler := bleveHttp.NewDeleteIndexHandler(*dataDir) deleteIndexHandler.IndexNameLookup = indexNameLookup router.Handle("/api/{indexName}", deleteIndexHandler).Methods("DELETE") listIndexesHandler := bleveHttp.NewListIndexesHandler() router.Handle("/api", listIndexesHandler).Methods("GET") docIndexHandler := bleveHttp.NewDocIndexHandler("") docIndexHandler.IndexNameLookup = indexNameLookup docIndexHandler.DocIDLookup = docIDLookup router.Handle("/api/{indexName}/{docID}", docIndexHandler).Methods("PUT") docCountHandler := bleveHttp.NewDocCountHandler("") docCountHandler.IndexNameLookup = indexNameLookup router.Handle("/api/{indexName}/_count", docCountHandler).Methods("GET") docGetHandler := bleveHttp.NewDocGetHandler("") docGetHandler.IndexNameLookup = indexNameLookup docGetHandler.DocIDLookup = docIDLookup router.Handle("/api/{indexName}/{docID}", docGetHandler).Methods("GET") docDeleteHandler := bleveHttp.NewDocDeleteHandler("") docDeleteHandler.IndexNameLookup = indexNameLookup docDeleteHandler.DocIDLookup = docIDLookup router.Handle("/api/{indexName}/{docID}", docDeleteHandler).Methods("DELETE") searchHandler := bleveHttp.NewSearchHandler("") searchHandler.IndexNameLookup = indexNameLookup router.Handle("/api/{indexName}/_search", searchHandler).Methods("POST") listFieldsHandler := bleveHttp.NewListFieldsHandler("") listFieldsHandler.IndexNameLookup = indexNameLookup router.Handle("/api/{indexName}/_fields", listFieldsHandler).Methods("GET") debugHandler := bleveHttp.NewDebugDocumentHandler("") debugHandler.IndexNameLookup = indexNameLookup debugHandler.DocIDLookup = docIDLookup router.Handle("/api/{indexName}/{docID}/_debug", debugHandler).Methods("GET") aliasHandler := bleveHttp.NewAliasHandler() router.Handle("/api/_aliases", aliasHandler).Methods("POST") // start the HTTP server http.Handle("/", router) log.Printf("Listening on %v", *bindAddr) log.Fatal(http.ListenAndServe(*bindAddr, nil)) }