Beispiel #1
0
// Creates the HTTP handler for the PRIVATE admin API of a gateway server.
func CreateAdminHandler(sc *ServerContext) http.Handler {
	r, dbr := createHandler(sc, adminPrivs)

	r.PathPrefix("/_admin/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if sc.config.AdminUI != nil {
			http.ServeFile(w, r, *sc.config.AdminUI)
		} else {
			w.Write(sync_gateway_admin_ui.Admin_bundle_html())
		}
	})

	dbr.Handle("/_session",
		makeHandler(sc, adminPrivs, (*handler).createUserSession)).Methods("POST")

	dbr.Handle("/_raw/{docid:"+docRegex+"}",
		makeHandler(sc, adminPrivs, (*handler).handleGetRawDoc)).Methods("GET", "HEAD")

	dbr.Handle("/_user/",
		makeHandler(sc, adminPrivs, (*handler).getUsers)).Methods("GET", "HEAD")
	dbr.Handle("/_user/",
		makeHandler(sc, adminPrivs, (*handler).putUser)).Methods("POST")
	dbr.Handle("/_user/{name}",
		makeHandler(sc, adminPrivs, (*handler).getUserInfo)).Methods("GET", "HEAD")
	dbr.Handle("/_user/{name}",
		makeHandler(sc, adminPrivs, (*handler).putUser)).Methods("PUT")
	dbr.Handle("/_user/{name}",
		makeHandler(sc, adminPrivs, (*handler).deleteUser)).Methods("DELETE")

	dbr.Handle("/_role/",
		makeHandler(sc, adminPrivs, (*handler).getRoles)).Methods("GET", "HEAD")
	dbr.Handle("/_role/",
		makeHandler(sc, adminPrivs, (*handler).putRole)).Methods("POST")
	dbr.Handle("/_role/{name}",
		makeHandler(sc, adminPrivs, (*handler).getRoleInfo)).Methods("GET", "HEAD")
	dbr.Handle("/_role/{name}",
		makeHandler(sc, adminPrivs, (*handler).putRole)).Methods("PUT")
	dbr.Handle("/_role/{name}",
		makeHandler(sc, adminPrivs, (*handler).deleteRole)).Methods("DELETE")

	r.Handle("/_profile/{name}",
		makeHandler(sc, adminPrivs, (*handler).handleProfiling)).Methods("POST")
	r.Handle("/_profile",
		makeHandler(sc, adminPrivs, (*handler).handleProfiling)).Methods("POST")
	r.Handle("/_heap",
		makeHandler(sc, adminPrivs, (*handler).handleHeapProfiling)).Methods("POST")
	r.Handle("/_stats",
		makeHandler(sc, adminPrivs, (*handler).handleStats)).Methods("GET")
	r.Handle(kDebugURLPathPrefix,
		makeHandler(sc, adminPrivs, (*handler).handleExpvar)).Methods("GET")

	dbr.Handle("/_config",
		makeHandler(sc, adminPrivs, (*handler).handleGetDbConfig)).Methods("GET")
	dbr.Handle("/_vacuum",
		makeHandler(sc, adminPrivs, (*handler).handleVacuum)).Methods("POST")
	dbr.Handle("/_dump/{view}",
		makeHandler(sc, adminPrivs, (*handler).handleDump)).Methods("GET")
	dbr.Handle("/_view/{view}",
		makeHandler(sc, adminPrivs, (*handler).handleView)).Methods("GET")
	dbr.Handle("/_dumpchannel/{channel}",
		makeHandler(sc, adminPrivs, (*handler).handleDumpChannel)).Methods("GET")

	// The routes below are part of the CouchDB REST API but should only be available to admins,
	// so the handlers are moved to the admin port.
	r.Handle("/{newdb:"+dbRegex+"}/",
		makeHandler(sc, adminPrivs, (*handler).handleCreateDB)).Methods("PUT")
	r.Handle("/{db:"+dbRegex+"}/",
		makeHandler(sc, adminPrivs, (*handler).handleDeleteDB)).Methods("DELETE")

	r.Handle("/_all_dbs",
		makeHandler(sc, adminPrivs, (*handler).handleAllDbs)).Methods("GET", "HEAD")
	dbr.Handle("/_compact",
		makeHandler(sc, adminPrivs, (*handler).handleCompact)).Methods("POST")

	return wrapRouter(sc, adminPrivs, r)
}
Beispiel #2
0
// Creates the HTTP handler for the PRIVATE admin API of a gateway server.
func CreateAdminHandler(sc *ServerContext) http.Handler {
	r, dbr := createHandler(sc, adminPrivs)

	r.PathPrefix("/_admin/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if sc.config.AdminUI != nil {
			http.ServeFile(w, r, *sc.config.AdminUI)
		} else {
			w.Write(sync_gateway_admin_ui.Admin_bundle_html())
		}
	})

	dbr.Handle("/_session",
		makeHandler(sc, adminPrivs, (*handler).createUserSession)).Methods("POST")

	dbr.Handle("/_session/{sessionid}",
		makeHandler(sc, adminPrivs, (*handler).getUserSession)).Methods("GET")

	dbr.Handle("/_session/{sessionid}",
		makeHandler(sc, adminPrivs, (*handler).deleteUserSession)).Methods("DELETE")

	dbr.Handle("/_raw/{docid:"+docRegex+"}",
		makeHandler(sc, adminPrivs, (*handler).handleGetRawDoc)).Methods("GET", "HEAD")

	dbr.Handle("/_user/",
		makeHandler(sc, adminPrivs, (*handler).getUsers)).Methods("GET", "HEAD")
	dbr.Handle("/_user/",
		makeHandler(sc, adminPrivs, (*handler).putUser)).Methods("POST")
	dbr.Handle("/_user/{name}",
		makeHandler(sc, adminPrivs, (*handler).getUserInfo)).Methods("GET", "HEAD")
	dbr.Handle("/_user/{name}",
		makeHandler(sc, adminPrivs, (*handler).putUser)).Methods("PUT")
	dbr.Handle("/_user/{name}",
		makeHandler(sc, adminPrivs, (*handler).deleteUser)).Methods("DELETE")

	dbr.Handle("/_user/{name}/_session",
		makeHandler(sc, adminPrivs, (*handler).deleteUserSessions)).Methods("DELETE")
	dbr.Handle("/_user/{name}/_session/{sessionid}",
		makeHandler(sc, adminPrivs, (*handler).deleteUserSession)).Methods("DELETE")

	dbr.Handle("/_role/",
		makeHandler(sc, adminPrivs, (*handler).getRoles)).Methods("GET", "HEAD")
	dbr.Handle("/_role/",
		makeHandler(sc, adminPrivs, (*handler).putRole)).Methods("POST")
	dbr.Handle("/_role/{name}",
		makeHandler(sc, adminPrivs, (*handler).getRoleInfo)).Methods("GET", "HEAD")
	dbr.Handle("/_role/{name}",
		makeHandler(sc, adminPrivs, (*handler).putRole)).Methods("PUT")
	dbr.Handle("/_role/{name}",
		makeHandler(sc, adminPrivs, (*handler).deleteRole)).Methods("DELETE")

	r.Handle("/_logging",
		makeHandler(sc, adminPrivs, (*handler).handleGetLogging)).Methods("GET")
	r.Handle("/_logging",
		makeHandler(sc, adminPrivs, (*handler).handleSetLogging)).Methods("PUT", "POST")
	r.Handle("/_profile/{name}",
		makeHandler(sc, adminPrivs, (*handler).handleProfiling)).Methods("POST")
	r.Handle("/_profile",
		makeHandler(sc, adminPrivs, (*handler).handleProfiling)).Methods("POST")
	r.Handle("/_heap",
		makeHandler(sc, adminPrivs, (*handler).handleHeapProfiling)).Methods("POST")
	r.Handle("/_stats",
		makeHandler(sc, adminPrivs, (*handler).handleStats)).Methods("GET")
	r.Handle(kDebugURLPathPrefix,
		makeHandler(sc, adminPrivs, (*handler).handleExpvar)).Methods("GET")

	// Debugging handlers
	r.Handle("/_debug/pprof/goroutine",
		makeHandler(sc, adminPrivs, (*handler).handlePprofGoroutine)).Methods("GET", "POST")
	r.Handle("/_debug/pprof/cmdline",
		makeHandler(sc, adminPrivs, (*handler).handlePprofCmdline)).Methods("GET", "POST")
	r.Handle("/_debug/pprof/symbol",
		makeHandler(sc, adminPrivs, (*handler).handlePprofSymbol)).Methods("GET", "POST")
	r.Handle("/_debug/pprof/heap",
		makeHandler(sc, adminPrivs, (*handler).handlePprofHeap)).Methods("GET", "POST")
	r.Handle("/_debug/pprof/profile",
		makeHandler(sc, adminPrivs, (*handler).handlePprofProfile)).Methods("GET", "POST")
	r.Handle("/_debug/pprof/block",
		makeHandler(sc, adminPrivs, (*handler).handlePprofBlock)).Methods("GET", "POST")
	r.Handle("/_debug/pprof/threadcreate",
		makeHandler(sc, adminPrivs, (*handler).handlePprofThreadcreate)).Methods("GET", "POST")
	r.Handle("/_debug/pprof/trace",
		makeHandler(sc, adminPrivs, (*handler).handlePprofTrace)).Methods("GET", "POST")

	// Database-relative handlers:
	dbr.Handle("/_config",
		makeHandler(sc, adminPrivs, (*handler).handleGetDbConfig)).Methods("GET")
	dbr.Handle("/_config",
		makeOfflineHandler(sc, adminPrivs, (*handler).handlePutDbConfig)).Methods("PUT")
	dbr.Handle("/_resync",
		makeOfflineHandler(sc, adminPrivs, (*handler).handleResync)).Methods("POST")
	dbr.Handle("/_vacuum",
		makeHandler(sc, adminPrivs, (*handler).handleVacuum)).Methods("POST")
	dbr.Handle("/_purge",
		makeHandler(sc, adminPrivs, (*handler).handlePurge)).Methods("POST")
	dbr.Handle("/_flush",
		makeHandler(sc, adminPrivs, (*handler).handleFlush)).Methods("POST")
	dbr.Handle("/_online",
		makeOfflineHandler(sc, adminPrivs, (*handler).handleDbOnline)).Methods("POST")
	dbr.Handle("/_offline",
		makeOfflineHandler(sc, adminPrivs, (*handler).handleDbOffline)).Methods("POST")
	dbr.Handle("/_dump/{view}",
		makeHandler(sc, adminPrivs, (*handler).handleDump)).Methods("GET")
	dbr.Handle("/_view/{view}", // redundant; just for backward compatibility with 1.0
		makeHandler(sc, adminPrivs, (*handler).handleView)).Methods("GET")
	dbr.Handle("/_dumpchannel/{channel}",
		makeHandler(sc, adminPrivs, (*handler).handleDumpChannel)).Methods("GET")
	dbr.Handle("/_index/channel/{channel}",
		makeHandler(sc, adminPrivs, (*handler).handleIndexChannel)).Methods("GET")
	dbr.Handle("/_index/channels",
		makeHandler(sc, adminPrivs, (*handler).handleIndexAllChannels)).Methods("GET")

	// The routes below are part of the CouchDB REST API but should only be available to admins,
	// so the handlers are moved to the admin port.
	r.Handle("/{newdb:"+dbRegex+"}/",
		makeHandler(sc, adminPrivs, (*handler).handleCreateDB)).Methods("PUT")
	r.Handle("/{db:"+dbRegex+"}/",
		makeOfflineHandler(sc, adminPrivs, (*handler).handleDeleteDB)).Methods("DELETE")

	r.Handle("/_all_dbs",
		makeHandler(sc, adminPrivs, (*handler).handleAllDbs)).Methods("GET", "HEAD")
	dbr.Handle("/_compact",
		makeHandler(sc, adminPrivs, (*handler).handleCompact)).Methods("POST")

	// Add CBGT REST api if it's enabled
	if err := addCbgtRoutes(r, sc); err != nil {
		base.Warn("CBGT routes will not be available due to error: %v", err)
	}

	return wrapRouter(sc, adminPrivs, r)
}