func MainStart(cfg cbgt.Cfg, uuid string, tags []string, container string, weight int, extras, bindHttp, dataDir, staticDir, staticETag, server string, register string, mr *cbgt.MsgRing, options map[string]string) ( *mux.Router, error) { if server == "" { return nil, fmt.Errorf("error: server URL required (-server)") } err := InitOptions(options) if err != nil { return nil, err } err = InitMossOptions(options) if err != nil { return nil, err } err = InitBleveOptions(options) if err != nil { return nil, err } if options["logStatsEvery"] != "" { logStatsEvery, err := strconv.Atoi(options["logStatsEvery"]) if err != nil { return nil, err } if logStatsEvery >= 0 { cbft.LogEveryNStats = logStatsEvery } } err = cbft.InitResultCacheOptions(options) if err != nil { return nil, err } err = cbft.InitBleveResultCacheOptions(options) if err != nil { return nil, err } exitCode := MainTool(cfg, uuid, tags, flags, options) if exitCode >= 0 { os.Exit(exitCode) } if server != "." && options["startCheckServer"] != "skip" { _, err := couchbase.Connect(server) if err != nil { if !strings.HasPrefix(server, "http://") && !strings.HasPrefix(server, "https://") { return nil, fmt.Errorf("error: not a URL, server: %q\n"+ " Please check that your -server parameter"+ " is a valid URL\n"+ " (http://HOST:PORT),"+ " such as \"http://*****:*****@HOST:PORT)", server, err, server) } } meh := &MainHandlers{} mgr := cbgt.NewManagerEx(cbgt.VERSION, cfg, uuid, tags, container, weight, extras, bindHttp, dataDir, server, meh, options) meh.mgr = mgr err = mgr.Start(register) if err != nil { return nil, err } var adtSvc *audit.AuditSvc if options["cbaudit"] == "true" { adtSvc, _ = audit.NewAuditSvc(server) } router, _, err := cbft.NewRESTRouter(VERSION, mgr, staticDir, staticETag, mr, adtSvc) // register handlers needed by ns_server prefix := mgr.Options()["urlPrefix"] router.Handle(prefix+"/api/nsstats", cbft.NewNsStatsHandler(mgr)) nsStatusHandler, err := cbft.NewNsStatusHandler(mgr, server) if err != nil { return nil, err } router.Handle(prefix+"/api/nsstatus", nsStatusHandler) nsSearchResultRedirectHandler, err := cbft.NsSearchResultRedirctHandler(mgr) if err != nil { return nil, err } router.Handle(prefix+"/api/nsSearchResultRedirect/{pIndexName}/{docID}", nsSearchResultRedirectHandler) cbAuthBasicLoginHadler, err := cbft.CBAuthBasicLoginHandler(mgr) if err != nil { return nil, err } router.Handle(prefix+"/login", cbAuthBasicLoginHadler) // ------------------------------------------------ tagsMap := mgr.TagsMap() if tagsMap != nil && tagsMap["cbauth_service"] { dryRun := false dryRunV := mgr.Options()["cbauth_service.dryRun"] if dryRunV != "" { dryRun, err = strconv.ParseBool(dryRunV) if err != nil { return nil, err } } waitForMemberNodes := 30 // In seconds. waitForMemberNodesV := mgr.Options()["cbauth_service.waitForMemberNodes"] if waitForMemberNodesV != "" { waitForMemberNodes, err = strconv.Atoi(waitForMemberNodesV) if err != nil { return nil, err } } verbose := DefaultCtlVerbose verboseV := mgr.Options()["cbauth_service.verbose"] if verboseV != "" { verbose, err = strconv.Atoi(verboseV) if err != nil { return nil, err } } log.Printf("main: cbauth_service ctl starting,"+ " dryRun: %v, waitForMemberNodes: %d, verbose: %d", dryRun, waitForMemberNodes, verbose) c, err := ctl.StartCtl(cfg, server, mgr.Options(), ctl.CtlOptions{ DryRun: dryRun, Verbose: verbose, FavorMinNodes: false, WaitForMemberNodes: waitForMemberNodes, }) if err != nil { return nil, fmt.Errorf("main: ctl.StartCtl, err: %v", err) } nodeInfo := &service.NodeInfo{ NodeID: service.NodeID(uuid), } err = cfg.Refresh() if err != nil { return nil, err } ctlMgr := ctl.NewCtlMgr(nodeInfo, c) if ctlMgr != nil { go func() { log.Printf("main: cbauth_service registering...") err := service.RegisterManager(ctlMgr, nil) if err != nil { log.Printf("main: cbauth_service register err: %v", err) return } log.Printf("main: cbauth_service registering... done") }() } } // ------------------------------------------------ return router, err }
func MainStart(cfg cbgt.Cfg, uuid string, tags []string, container string, weight int, extras, bindHttp, dataDir, staticDir, staticETag, server string, register string, mr *cbgt.MsgRing, optionKVs string) ( *mux.Router, error) { if server == "" { return nil, fmt.Errorf("error: server URL required (-server)") } if server != "." { _, err := couchbase.Connect(server) if err != nil { if !strings.HasPrefix(server, "http://") && !strings.HasPrefix(server, "https://") { return nil, fmt.Errorf("error: not a URL, server: %q\n"+ " Please check that your -server parameter"+ " is a valid URL\n"+ " (http://HOST:PORT),"+ " such as \"http://*****:*****@HOST:PORT)", server, err, server) } } options := map[string]string{} if optionKVs != "" { for _, kv := range strings.Split(optionKVs, ",") { a := strings.Split(kv, "=") if len(a) >= 2 { options[a[0]] = a[1] } } } mgr := cbgt.NewManagerEx(cbgt.VERSION, cfg, uuid, tags, container, weight, extras, bindHttp, dataDir, server, &MainHandlers{}, options) err := mgr.Start(register) if err != nil { return nil, err } router, _, err := cbft.NewRESTRouter(VERSION, mgr, staticDir, staticETag, mr) // register handlers needed by ns_server router.Handle("/api/nsstats", cbft.NewNsStatsHandler(mgr)) nsStatusHandler, err := cbft.NewNsStatusHandler(mgr, server) if err != nil { return nil, err } router.Handle("/api/nsstatus", nsStatusHandler) return router, err }
// Emits markdown docs of cbft's REST API. func main() { rand.Seed(0) dataDir, _ := ioutil.TempDir("./tmp", "data") defer os.RemoveAll(dataDir) cfg := cbgt.NewCfgMem() tags := []string(nil) container := "" weight := 1 extras := "" bindHttp := "0.0.0.0:8095" mgr := cbgt.NewManager(cbgt.VERSION, cfg, cbgt.NewUUID(), tags, container, weight, extras, bindHttp, dataDir, "http://localhost:8091", nil) sourceType := "nil" sourceName := "" sourceUUID := "" sourceParams := "" indexType := "blackhole" indexName := "myFirstIndex" indexParams := "" prevIndexUUID := "" mgr.Start("wanted") err := mgr.CreateIndex( sourceType, sourceName, sourceUUID, sourceParams, indexType, indexName, indexParams, cbgt.PlanParams{}, prevIndexUUID) if err != nil { log.Fatalf("could not create myFirstIndex, err: %v", err) } staticDir := "" staticETag := "" mr, _ := cbgt.NewMsgRing(ioutil.Discard, 1) router, meta, err := cbft.NewRESTRouter(VERSION, mgr, staticDir, staticETag, mr) if err != nil { log.Panic(err) } mainCategoriesMap := map[string]bool{} mainCategories := []string(nil) subCategoriesMap := map[string]bool{} subCategories := []string(nil) paths := []string(nil) for path := range meta { paths = append(paths, path) m := meta[path] if m.Opts != nil { category := m.Opts["_category"] mainCategory, _, subCategory, _ := categoryParse(category) if !mainCategoriesMap[mainCategory] { mainCategoriesMap[mainCategory] = true mainCategories = append(mainCategories, mainCategory) } if !subCategoriesMap[subCategory] { subCategoriesMap[subCategory] = true subCategories = append(subCategories, subCategory) } } } sort.Strings(mainCategories) sort.Strings(subCategories) sort.Strings(paths) fmt.Printf("# API Reference\n\n") for _, mainCategory := range mainCategories { mainCategoryFirst := true for _, subCategory := range subCategories { subCategoryFirst := true for _, path := range paths { m := meta[path] category := "" if m.Opts != nil { category = m.Opts["_category"] } mc, mcVis, sc, scVis := categoryParse(category) if mc != mainCategory || sc != subCategory { continue } if m.Opts != nil && m.Opts["_status"] == "private" { continue } if mainCategoryFirst { fmt.Printf("---\n\n# %s\n\n", mcVis) } mainCategoryFirst = false if subCategoryFirst { fmt.Printf("---\n\n## %s\n\n", scVis) } subCategoryFirst = false pathParts := strings.Split(path, " ") fmt.Printf("---\n\n") fmt.Printf("%s `%s`\n\n", m.Method, pathParts[0]) if m.Opts != nil && m.Opts["_about"] != "" { fmt.Printf("%s\n\n", m.Opts["_about"]) } optNames := []string(nil) for optName := range m.Opts { if optName != "" && !strings.HasPrefix(optName, "_") { optNames = append(optNames, optName) } } sort.Strings(optNames) for _, optName := range optNames { fmt.Printf("**%s**: %s\n\n", optName, m.Opts[optName]) } if m.Opts != nil && m.Opts[""] != "" { fmt.Printf("%s\n\n", m.Opts[""]) } if m.Method == "GET" && !skipSampleResponses[pathParts[0]] { url := strings.Replace(pathParts[0], "{indexName}", "myFirstIndex", 1) res := sampleRequest(router, m.Method, url, nil) if res != nil { var j map[string]interface{} err = json.Unmarshal(res, &j) if err == nil { s, err := json.MarshalIndent(j, " ", " ") if err == nil { fmt.Printf("Sample response:\n\n %s\n\n", s) } } } } } } } fmt.Printf("---\n\nCopyright (c) 2015 Couchbase, Inc.\n") }