MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / createCommonRouter

Function createCommonRouter

rest/routing.go:40–120  ·  view source on GitHub ↗

createCommonRouter returns a Gorilla mux router containing the basic HTTP handlers for a server. This is the common functionality of the public and admin ports. The 'privs' parameter specifies the authentication the handler will use.

(sc *ServerContext, privs handlerPrivs, serverType serverType)

Source from the content-addressed store, hash-verified

38// This is the common functionality of the public and admin ports.
39// The 'privs' parameter specifies the authentication the handler will use.
40func createCommonRouter(sc *ServerContext, privs handlerPrivs, serverType serverType) (root, db, keyspace *mux.Router) {
41 root = NewRouter(sc, serverType)
42
43 // Global operations:
44 root.Handle("/", makeHandlerWithOptions(sc, privs, nil, nil, (*handler).handleRoot, handlerOptions{sgcollect: true})).Methods("GET", "HEAD")
45
46 // Operations on databases:
47 root.Handle("/{db:"+dbRegex+"}/", makeOfflineHandler(sc, privs, []Permission{PermDevOps, PermGetDb}, nil, (*handler).handleGetDB)).Methods("GET", "HEAD")
48 root.Handle("/{keyspace:"+dbRegex+"}/", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handlePostDoc)).Methods("POST")
49
50 // Keyspace operations (i.e. collection-specific):
51 keyspace = root.PathPrefix("/{keyspace:" + dbRegex + "}/").Subrouter()
52 keyspace.StrictSlash(true)
53 keyspace.Handle("/{docid:"+docRegex+"}", makeHandler(sc, privs, []Permission{PermReadAppData}, nil, (*handler).handleGetDoc)).Methods("GET", "HEAD")
54 keyspace.Handle("/{docid:"+docRegex+"}", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handlePutDoc)).Methods("PUT")
55 keyspace.Handle("/{docid:"+docRegex+"}", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handleDeleteDoc)).Methods("DELETE")
56
57 keyspace.Handle("/{docid:"+docRegex+"}/{attach}", makeHandler(sc, privs, []Permission{PermReadAppData}, nil, (*handler).handleGetAttachment)).Methods("GET", "HEAD")
58 keyspace.Handle("/{docid:"+docRegex+"}/{attach}", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handlePutAttachment)).Methods("PUT")
59 keyspace.Handle("/{docid:"+docRegex+"}/{attach}", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handleDeleteAttachment)).Methods("DELETE")
60
61 keyspace.Handle("/_local/{docid}", makeHandler(sc, privs, []Permission{PermReadAppData}, nil, (*handler).handleGetLocalDoc)).Methods("GET", "HEAD")
62 keyspace.Handle("/_local/{docid}", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handlePutLocalDoc)).Methods("PUT")
63 keyspace.Handle("/_local/{docid}", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handleDelLocalDoc)).Methods("DELETE")
64
65 keyspace.Handle("/_bulk_docs", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handleBulkDocs)).Methods("POST")
66 keyspace.Handle("/_bulk_get", makeHandler(sc, privs, []Permission{PermReadAppData}, nil, (*handler).handleBulkGet)).Methods("POST")
67 keyspace.Handle("/_revs_diff", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handleRevsDiff)).Methods("POST")
68 keyspace.Handle("/_changes", makeHandler(sc, privs, []Permission{PermReadAppData}, nil, (*handler).handleChanges)).Methods("GET", "HEAD", "POST")
69 keyspace.Handle("/_all_docs", makeHandler(sc, privs, []Permission{PermReadAppData}, nil, (*handler).handleAllDocs)).Methods("GET", "HEAD", "POST")
70
71 // Database operations (i.e. multi-collection):
72 dbr := root.PathPrefix("/{db:" + dbRegex + "}/").Subrouter()
73 dbr.StrictSlash(true)
74 dbr.Handle("/_design/{ddoc}", makeHandler(sc, privs, []Permission{PermReadAppData}, nil, (*handler).handleGetDesignDoc)).Methods("GET", "HEAD")
75 dbr.Handle("/_design/{ddoc}", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handlePutDesignDoc)).Methods("PUT")
76 dbr.Handle("/_design/{ddoc}", makeHandler(sc, privs, []Permission{PermWriteAppData}, nil, (*handler).handleDeleteDesignDoc)).Methods("DELETE")
77 dbr.Handle("/_design/{ddoc}/_view/{view}", makeHandler(sc, privs, []Permission{PermReadAppData}, nil, (*handler).handleView)).Methods("GET")
78 dbr.Handle("/_ensure_full_commit", makeHandler(sc, privs, []Permission{PermReadAppData}, nil, (*handler).handleEFC)).Methods("POST")
79
80 // Session/login URLs are per-database (unlike in CouchDB)
81 // These have public privileges so that they can be called without being logged in already
82 dbr.Handle("/_session", makeHandler(sc, publicPrivs, nil, nil, (*handler).handleSessionGET)).Methods("GET", "HEAD")
83
84 if sc.Config.DeprecatedConfig != nil {
85 if sc.Config.DeprecatedConfig.Facebook != nil {
86 dbr.Handle("/_facebook", makeHandler(sc, publicPrivs, nil, nil, (*handler).handleFacebookPOST)).Methods("POST")
87 }
88 if sc.Config.DeprecatedConfig.Google != nil {
89 dbr.Handle("/_google", makeHandler(sc, publicPrivs, nil, nil, (*handler).handleGooglePOST)).Methods("POST")
90 }
91 }
92
93 // OpenID Connect endpoints
94 dbr.Handle("/_oidc", makeHandler(sc, publicPrivs, nil, nil, (*handler).handleOIDC)).Methods("GET")
95 dbr.Handle("/_oidc_callback", makeHandler(sc, publicPrivs, nil, nil, (*handler).handleOIDCCallback)).Methods("GET")
96 dbr.Handle("/_oidc_refresh", makeHandler(sc, publicPrivs, nil, nil, (*handler).handleOIDCRefresh)).Methods("GET")
97 dbr.Handle("/_oidc_challenge", makeHandler(sc, publicPrivs, nil, nil, (*handler).handleOIDCChallenge)).Methods("GET")

Callers 2

CreatePublicHandlerFunction · 0.85
CreateAdminRouterFunction · 0.85

Calls 4

NewRouterFunction · 0.85
makeHandlerWithOptionsFunction · 0.85
makeOfflineHandlerFunction · 0.85
makeHandlerFunction · 0.85

Tested by

no test coverage detected