Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 49ea2c7

Browse files
mrnuggetvovakulikov
authored andcommitted
httpapi: put internal API router next to handlers (#57990)
Same thing as in #57988 but for the internal API router. This time there's more code to remove.
1 parent f19c3ea commit 49ea2c7

File tree

8 files changed

+60
-141
lines changed

8 files changed

+60
-141
lines changed

cmd/frontend/internal/cli/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ go_library(
3030
"//cmd/frontend/internal/bg",
3131
"//cmd/frontend/internal/cli/middleware",
3232
"//cmd/frontend/internal/httpapi",
33-
"//cmd/frontend/internal/httpapi/router",
3433
"//cmd/frontend/oneclickexport",
3534
"//internal/actor",
3635
"//internal/adminanalytics",

cmd/frontend/internal/cli/autoupgrade_servers.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/app/assetsutil"
1414
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/httpapi"
15-
apirouter "github.com/sourcegraph/sourcegraph/cmd/frontend/internal/httpapi/router"
1615
"github.com/sourcegraph/sourcegraph/internal/conf"
1716
"github.com/sourcegraph/sourcegraph/internal/conf/conftypes"
1817
"github.com/sourcegraph/sourcegraph/internal/database"
@@ -33,23 +32,23 @@ func serveInternalServer(obsvCtx *observation.Context) (context.CancelFunc, erro
3332

3433
internalRouter := mux.NewRouter().PathPrefix("/.internal").Subrouter()
3534
internalRouter.StrictSlash(true)
36-
internalRouter.Path("/configuration").Methods("POST").Name(apirouter.Configuration)
37-
internalRouter.Get(apirouter.Configuration).Handler(middleware(func(w http.ResponseWriter, r *http.Request) error {
38-
configuration := conf.Unified{
39-
SiteConfiguration: schema.SiteConfiguration{},
40-
ServiceConnectionConfig: conftypes.ServiceConnections{
41-
PostgresDSN: dbconn.MigrationInProgressSentinelDSN,
42-
CodeIntelPostgresDSN: dbconn.MigrationInProgressSentinelDSN,
43-
CodeInsightsDSN: dbconn.MigrationInProgressSentinelDSN,
44-
},
45-
}
46-
b, _ := json.Marshal(configuration.SiteConfiguration)
47-
raw := conftypes.RawUnified{
48-
Site: string(b),
49-
ServiceConnections: configuration.ServiceConnections(),
50-
}
51-
return json.NewEncoder(w).Encode(raw)
52-
}))
35+
internalRouter.Path("/configuration").Methods("POST").
36+
Handler(middleware(func(w http.ResponseWriter, r *http.Request) error {
37+
configuration := conf.Unified{
38+
SiteConfiguration: schema.SiteConfiguration{},
39+
ServiceConnectionConfig: conftypes.ServiceConnections{
40+
PostgresDSN: dbconn.MigrationInProgressSentinelDSN,
41+
CodeIntelPostgresDSN: dbconn.MigrationInProgressSentinelDSN,
42+
CodeInsightsDSN: dbconn.MigrationInProgressSentinelDSN,
43+
},
44+
}
45+
b, _ := json.Marshal(configuration.SiteConfiguration)
46+
raw := conftypes.RawUnified{
47+
Site: string(b),
48+
ServiceConnections: configuration.ServiceConnections(),
49+
}
50+
return json.NewEncoder(w).Encode(raw)
51+
}))
5352

5453
serveMux.Handle("/.internal/", internalRouter)
5554

cmd/frontend/internal/cli/http.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/app/assetsutil"
2222
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/cli/middleware"
2323
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/httpapi"
24-
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/httpapi/router"
2524
"github.com/sourcegraph/sourcegraph/internal/actor"
2625
internalauth "github.com/sourcegraph/sourcegraph/internal/auth"
2726
"github.com/sourcegraph/sourcegraph/internal/conf"
@@ -149,7 +148,7 @@ func newInternalHTTPHandler(
149148
internalMux := http.NewServeMux()
150149
logger := log.Scoped("internal")
151150

152-
internalRouter := router.NewInternal(mux.NewRouter().PathPrefix("/.internal/").Subrouter())
151+
internalRouter := mux.NewRouter().PathPrefix("/.internal/").Subrouter()
153152
httpapi.RegisterInternalServices(
154153
internalRouter,
155154
grpcServer,

cmd/frontend/internal/httpapi/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ go_library(
2626
"//cmd/frontend/graphqlbackend",
2727
"//cmd/frontend/internal/handlerutil",
2828
"//cmd/frontend/internal/httpapi/releasecache",
29-
"//cmd/frontend/internal/httpapi/router",
3029
"//cmd/frontend/internal/httpapi/webhookhandlers",
3130
"//cmd/frontend/internal/routevar",
3231
"//cmd/frontend/internal/search",
@@ -98,7 +97,6 @@ go_test(
9897
"//cmd/frontend/backend",
9998
"//cmd/frontend/enterprise",
10099
"//cmd/frontend/graphqlbackend",
101-
"//cmd/frontend/internal/httpapi/router",
102100
"//internal/actor",
103101
"//internal/api",
104102
"//internal/authz",
@@ -132,6 +130,7 @@ go_test(
132130
"@com_github_stretchr_testify//assert",
133131
"@com_github_stretchr_testify//require",
134132
"@com_github_throttled_throttled_v2//store/memstore",
133+
"@org_golang_google_grpc//:go_default_library",
135134
"@org_golang_google_protobuf//testing/protocmp",
136135
],
137136
)

cmd/frontend/internal/httpapi/httpapi.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/sourcegraph/sourcegraph/cmd/frontend/graphqlbackend"
2525
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/handlerutil"
2626
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/httpapi/releasecache"
27-
apirouter "github.com/sourcegraph/sourcegraph/cmd/frontend/internal/httpapi/router"
2827
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/httpapi/webhookhandlers"
2928
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/routevar"
3029
frontendsearch "github.com/sourcegraph/sourcegraph/cmd/frontend/internal/search"
@@ -110,9 +109,9 @@ func NewHandler(
110109
WriteErrBody: env.InsecureDev,
111110
})
112111

113-
m.PathPrefix("/registry").Methods("GET").Name(apirouter.Registry).Handler(trace.Route(jsonHandler(frontendregistry.HandleRegistry)))
114-
m.PathPrefix("/scim/v2").Methods("GET", "POST", "PUT", "PATCH", "DELETE").Name(apirouter.SCIM).Handler(trace.Route(handlers.SCIMHandler))
115-
m.Path("/graphql").Methods("POST").Name(apirouter.GraphQL).Handler(trace.Route(jsonHandler(serveGraphQL(logger, schema, rateLimiter, false))))
112+
m.PathPrefix("/registry").Methods("GET").Handler(trace.Route(jsonHandler(frontendregistry.HandleRegistry)))
113+
m.PathPrefix("/scim/v2").Methods("GET", "POST", "PUT", "PATCH", "DELETE").Handler(trace.Route(handlers.SCIMHandler))
114+
m.Path("/graphql").Methods("POST").Handler(trace.Route(jsonHandler(serveGraphQL(logger, schema, rateLimiter, false))))
116115

117116
// Webhooks
118117
//
@@ -149,10 +148,10 @@ func NewHandler(
149148
m.Path("/files/batch-changes/{spec}/{file}").Methods("GET").Handler(trace.Route(handlers.BatchesChangesFileGetHandler))
150149
m.Path("/files/batch-changes/{spec}/{file}").Methods("HEAD").Handler(trace.Route(handlers.BatchesChangesFileExistsHandler))
151150
m.Path("/files/batch-changes/{spec}").Methods("POST").Handler(trace.Route(handlers.BatchesChangesFileUploadHandler))
152-
m.Path("/lsif/upload").Methods("POST").Name(apirouter.LSIFUpload).Handler(trace.Route(lsifDeprecationHandler))
153-
m.Path("/scip/upload").Methods("POST").Name(apirouter.SCIPUpload).Handler(trace.Route(handlers.NewCodeIntelUploadHandler(true)))
154-
m.Path("/scip/upload").Methods("HEAD").Name(apirouter.SCIPUploadExists).Handler(trace.Route(noopHandler))
155-
m.Path("/compute/stream").Methods("GET", "POST").Name(apirouter.ComputeStream).Handler(trace.Route(handlers.NewComputeStreamHandler()))
151+
m.Path("/lsif/upload").Methods("POST").Handler(trace.Route(lsifDeprecationHandler))
152+
m.Path("/scip/upload").Methods("POST").Handler(trace.Route(handlers.NewCodeIntelUploadHandler(true)))
153+
m.Path("/scip/upload").Methods("HEAD").Handler(trace.Route(noopHandler))
154+
m.Path("/compute/stream").Methods("GET", "POST").Handler(trace.Route(handlers.NewComputeStreamHandler()))
156155
m.Path("/blame/" + routevar.Repo + routevar.RepoRevSuffix + "/stream/{Path:.*}").Methods("GET").Handler(trace.Route(handleStreamBlame(logger, db, gitserver.NewClient("http.blamestream"))))
157156
// Set up the src-cli version cache handler (this will effectively be a
158157
// no-op anywhere other than dot-com).
@@ -198,6 +197,11 @@ func NewHandler(
198197
return m, nil
199198
}
200199

200+
const (
201+
gitInfoRefs = "internal.git.info-refs"
202+
gitUploadPack = "internal.git.upload-pack"
203+
)
204+
201205
// RegisterInternalServices registers REST and gRPC handlers for Sourcegraph's internal API on the
202206
// provided mux.Router and gRPC server.
203207
//
@@ -226,6 +230,7 @@ func RegisterInternalServices(
226230

227231
// zoekt-indexserver endpoints
228232
gsClient := gitserver.NewClient("http.zoektindexerserver")
233+
229234
indexer := &searchIndexerServer{
230235
db: db,
231236
logger: logger.Scoped("searchIndexerServer"),
@@ -239,28 +244,25 @@ func RegisterInternalServices(
239244
Ranking: rankingService,
240245
MinLastChangedDisabled: os.Getenv("SRC_SEARCH_INDEXER_EFFICIENT_POLLING_DISABLED") != "",
241246
}
242-
m.Get(apirouter.SearchConfiguration).Handler(trace.Route(handler(indexer.serveConfiguration)))
243-
m.Get(apirouter.ReposIndex).Handler(trace.Route(handler(indexer.serveList)))
244-
m.Get(apirouter.DocumentRanks).Handler(trace.Route(handler(indexer.serveDocumentRanks)))
245-
m.Get(apirouter.UpdateIndexStatus).Handler(trace.Route(handler(indexer.handleIndexStatusUpdate)))
246-
247-
zoektProto.RegisterZoektConfigurationServiceServer(s, &searchIndexerGRPCServer{server: indexer})
248-
confProto.RegisterConfigServiceServer(s, &configServer{})
249247

250-
gitService := &gitServiceHandler{
251-
Gitserver: gsClient,
252-
}
253-
m.Get(apirouter.GitInfoRefs).Handler(trace.Route(handler(gitService.serveInfoRefs())))
254-
m.Get(apirouter.GitUploadPack).Handler(trace.Route(handler(gitService.serveGitUploadPack())))
255-
m.Get(apirouter.GraphQL).Handler(trace.Route(handler(serveGraphQL(logger, schema, rateLimitWatcher, true))))
256-
m.Get(apirouter.Configuration).Handler(trace.Route(handler(serveConfiguration)))
248+
gitService := &gitServiceHandler{Gitserver: gsClient}
249+
m.Path("/git/{RepoName:.*}/info/refs").Methods("GET").Name(gitInfoRefs).Handler(trace.Route(handler(gitService.serveInfoRefs())))
250+
m.Path("/git/{RepoName:.*}/git-upload-pack").Methods("GET", "POST").Name(gitUploadPack).Handler(trace.Route(handler(gitService.serveGitUploadPack())))
251+
m.Path("/repos/index").Methods("POST").Handler(trace.Route(handler(indexer.serveList)))
252+
m.Path("/configuration").Methods("POST").Handler(trace.Route(handler(serveConfiguration)))
253+
m.Path("/ranks/{RepoName:.*}/documents").Methods("GET").Handler(trace.Route(handler(indexer.serveDocumentRanks)))
254+
m.Path("/search/configuration").Methods("GET", "POST").Handler(trace.Route(handler(indexer.serveConfiguration)))
255+
m.Path("/search/index-status").Methods("POST").Handler(trace.Route(handler(indexer.handleIndexStatusUpdate)))
256+
m.Path("/lsif/upload").Methods("POST").Handler(trace.Route(newCodeIntelUploadHandler(false)))
257+
m.Path("/scip/upload").Methods("POST").Handler(trace.Route(newCodeIntelUploadHandler(false)))
258+
m.Path("/scip/upload").Methods("HEAD").Handler(trace.Route(noopHandler))
259+
m.Path("/search/stream").Methods("GET").Handler(trace.Route(frontendsearch.StreamHandler(db)))
260+
m.Path("/compute/stream").Methods("GET", "POST").Handler(trace.Route(newComputeStreamHandler()))
261+
m.Path("/graphql").Methods("POST").Handler(trace.Route(handler(serveGraphQL(logger, schema, rateLimitWatcher, true))))
257262
m.Path("/ping").Methods("GET").Name("ping").HandlerFunc(handlePing)
258-
m.Get(apirouter.StreamingSearch).Handler(trace.Route(frontendsearch.StreamHandler(db)))
259-
m.Get(apirouter.ComputeStream).Handler(trace.Route(newComputeStreamHandler()))
260263

261-
m.Get(apirouter.LSIFUpload).Handler(trace.Route(newCodeIntelUploadHandler(false)))
262-
m.Get(apirouter.SCIPUpload).Handler(trace.Route(newCodeIntelUploadHandler(false)))
263-
m.Get(apirouter.SCIPUploadExists).Handler(trace.Route(noopHandler))
264+
zoektProto.RegisterZoektConfigurationServiceServer(s, &searchIndexerGRPCServer{server: indexer})
265+
confProto.RegisterConfigServiceServer(s, &configServer{})
264266

265267
m.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
266268
log.Printf("API no route: %s %s from %s", r.Method, r.URL, r.Referer())

cmd/frontend/internal/httpapi/internal_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ import (
99
"testing"
1010

1111
"github.com/gorilla/mux"
12+
"google.golang.org/grpc"
1213

1314
"github.com/sourcegraph/log/logtest"
1415

15-
apirouter "github.com/sourcegraph/sourcegraph/cmd/frontend/internal/httpapi/router"
1616
"github.com/sourcegraph/sourcegraph/internal/api"
17+
"github.com/sourcegraph/sourcegraph/internal/database/dbmocks"
1718
)
1819

1920
func TestGitServiceHandlers(t *testing.T) {
20-
m := apirouter.NewInternal(mux.NewRouter())
21+
db := dbmocks.NewMockDB()
22+
grpcServer := grpc.NewServer()
23+
dummyCodeIntelHandler := func(_ bool) http.Handler { return noopHandler }
24+
dummyComputeStreamHandler := func() http.Handler { return noopHandler }
25+
26+
m := mux.NewRouter()
27+
28+
RegisterInternalServices(m, grpcServer, db, nil, dummyCodeIntelHandler, nil, dummyComputeStreamHandler, nil)
2129

2230
gitService := &gitServiceHandler{
2331
Gitserver: mockAddrForRepo{},
@@ -27,8 +35,8 @@ func TestGitServiceHandlers(t *testing.T) {
2735
// Internal endpoints can expose sensitive errors
2836
WriteErrBody: true,
2937
})
30-
m.Get(apirouter.GitInfoRefs).Handler(handler(gitService.serveInfoRefs()))
31-
m.Get(apirouter.GitUploadPack).Handler(handler(gitService.serveGitUploadPack()))
38+
m.Get(gitInfoRefs).Handler(handler(gitService.serveInfoRefs()))
39+
m.Get(gitUploadPack).Handler(handler(gitService.serveGitUploadPack()))
3240

3341
cases := map[string]string{
3442
"/git/foo/bar/info/refs?service=git-upload-pack": "http://foo.bar.gitserver/git/foo/bar/info/refs?service=git-upload-pack",
@@ -59,12 +67,3 @@ type mockAddrForRepo struct{}
5967
func (mockAddrForRepo) AddrForRepo(ctx context.Context, name api.RepoName) string {
6068
return strings.ReplaceAll(string(name), "/", ".") + ".gitserver"
6169
}
62-
63-
// newTestInternalRouter creates a minimal router for internal endpoints. You can use
64-
// m.Get(apirouter.FOOBAR) to mock out endpoints, and then provide the router to
65-
// httptest.NewServer.
66-
func newTestInternalRouter() *mux.Router {
67-
// Magic incantation from newInternalHTTPHandler
68-
sr := mux.NewRouter().PathPrefix("/.internal/").Subrouter()
69-
return apirouter.NewInternal(sr)
70-
}

cmd/frontend/internal/httpapi/router/BUILD.bazel

Lines changed: 0 additions & 9 deletions
This file was deleted.

cmd/frontend/internal/httpapi/router/router.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)