codeintel: Clean up internal HTTP server setup (#14733)

This commit is contained in:
Eric Fritz 2020-10-14 17:29:51 -05:00 committed by GitHub
parent 5218bbadff
commit 91659bb2bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 100 additions and 264 deletions

View File

@ -26,28 +26,23 @@ import (
const DefaultMonikerResultPageSize = 100
const DefaultDiagnosticResultPageSize = 100
func (s *Server) handler() http.Handler {
mux := mux.NewRouter()
mux.Path("/uploads/{id:[0-9]+}").Methods("GET").HandlerFunc(s.handleGetUpload)
mux.Path("/uploads/{id:[0-9]+}").Methods("POST").HandlerFunc(s.handlePostUpload)
mux.Path("/uploads/{id:[0-9]+}/{index:[0-9]+}").Methods("POST").HandlerFunc(s.handlePostUploadPart)
mux.Path("/uploads/{id:[0-9]+}/stitch").Methods("POST").HandlerFunc(s.handlePostUploadStitch)
mux.Path("/uploads/{id:[0-9]+}").Methods("DELETE").HandlerFunc(s.handleDeleteUpload)
mux.Path("/dbs/{id:[0-9]+}/{index:[0-9]+}").Methods("POST").HandlerFunc(s.handlePostDatabasePart)
mux.Path("/dbs/{id:[0-9]+}/stitch").Methods("POST").HandlerFunc(s.handlePostDatabaseStitch)
mux.Path("/dbs/{id:[0-9]+}/exists").Methods("GET").HandlerFunc(s.handleExists)
mux.Path("/dbs/{id:[0-9]+}/ranges").Methods("GET").HandlerFunc(s.handleRanges)
mux.Path("/dbs/{id:[0-9]+}/definitions").Methods("GET").HandlerFunc(s.handleDefinitions)
mux.Path("/dbs/{id:[0-9]+}/references").Methods("GET").HandlerFunc(s.handleReferences)
mux.Path("/dbs/{id:[0-9]+}/hover").Methods("GET").HandlerFunc(s.handleHover)
mux.Path("/dbs/{id:[0-9]+}/diagnostics").Methods("GET").HandlerFunc(s.handleDiagnostics)
mux.Path("/dbs/{id:[0-9]+}/monikersByPosition").Methods("GET").HandlerFunc(s.handleMonikersByPosition)
mux.Path("/dbs/{id:[0-9]+}/monikerResults").Methods("GET").HandlerFunc(s.handleMonikerResults)
mux.Path("/dbs/{id:[0-9]+}/packageInformation").Methods("GET").HandlerFunc(s.handlePackageInformation)
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
return mux
func (s *Server) setupRoutes(router *mux.Router) {
router.Path("/uploads/{id:[0-9]+}").Methods("GET").HandlerFunc(s.handleGetUpload)
router.Path("/uploads/{id:[0-9]+}").Methods("POST").HandlerFunc(s.handlePostUpload)
router.Path("/uploads/{id:[0-9]+}/{index:[0-9]+}").Methods("POST").HandlerFunc(s.handlePostUploadPart)
router.Path("/uploads/{id:[0-9]+}/stitch").Methods("POST").HandlerFunc(s.handlePostUploadStitch)
router.Path("/uploads/{id:[0-9]+}").Methods("DELETE").HandlerFunc(s.handleDeleteUpload)
router.Path("/dbs/{id:[0-9]+}/{index:[0-9]+}").Methods("POST").HandlerFunc(s.handlePostDatabasePart)
router.Path("/dbs/{id:[0-9]+}/stitch").Methods("POST").HandlerFunc(s.handlePostDatabaseStitch)
router.Path("/dbs/{id:[0-9]+}/exists").Methods("GET").HandlerFunc(s.handleExists)
router.Path("/dbs/{id:[0-9]+}/ranges").Methods("GET").HandlerFunc(s.handleRanges)
router.Path("/dbs/{id:[0-9]+}/definitions").Methods("GET").HandlerFunc(s.handleDefinitions)
router.Path("/dbs/{id:[0-9]+}/references").Methods("GET").HandlerFunc(s.handleReferences)
router.Path("/dbs/{id:[0-9]+}/hover").Methods("GET").HandlerFunc(s.handleHover)
router.Path("/dbs/{id:[0-9]+}/diagnostics").Methods("GET").HandlerFunc(s.handleDiagnostics)
router.Path("/dbs/{id:[0-9]+}/monikersByPosition").Methods("GET").HandlerFunc(s.handleMonikersByPosition)
router.Path("/dbs/{id:[0-9]+}/monikerResults").Methods("GET").HandlerFunc(s.handleMonikerResults)
router.Path("/dbs/{id:[0-9]+}/packageInformation").Methods("GET").HandlerFunc(s.handlePackageInformation)
}
// GET /uploads/{id:[0-9]+}

View File

@ -1,20 +1,12 @@
package server
import (
"context"
"database/sql"
"net"
"net/http"
"os"
"strconv"
"sync"
"github.com/inconshreveable/log15"
"github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/bundles/persistence/cache"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/httpserver"
"github.com/sourcegraph/sourcegraph/internal/observation"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
)
const Port = 3187
@ -24,49 +16,15 @@ type Server struct {
storeCache cache.StoreCache
codeIntelDB *sql.DB
observationContext *observation.Context
server *http.Server
once sync.Once
}
var _ goroutine.BackgroundRoutine = &Server{}
func New(
bundleDir string,
storeCache cache.StoreCache,
codeIntelDB *sql.DB,
observationContext *observation.Context,
) *Server {
host := ""
if env.InsecureDev {
host = "127.0.0.1"
}
s := &Server{
func New(bundleDir string, storeCache cache.StoreCache, codeIntelDB *sql.DB, observationContext *observation.Context) goroutine.BackgroundRoutine {
server := &Server{
bundleDir: bundleDir,
storeCache: storeCache,
codeIntelDB: codeIntelDB,
observationContext: observationContext,
}
s.server = &http.Server{
Addr: net.JoinHostPort(host, strconv.FormatInt(int64(Port), 10)),
Handler: ot.Middleware(s.handler()),
}
return s
}
func (s *Server) Start() {
if err := s.server.ListenAndServe(); err != http.ErrServerClosed {
log15.Error("Failed to start server", "error", err)
os.Exit(1)
}
}
func (s *Server) Stop() {
s.once.Do(func() {
if err := s.server.Shutdown(context.Background()); err != nil {
log15.Error("Failed to shutdown server", "error", err)
}
})
return httpserver.New(Port, server.setupRoutes)
}

View File

@ -1,15 +0,0 @@
package server
import (
"net/http"
"github.com/gorilla/mux"
)
func (s *Server) handler() http.Handler {
mux := mux.NewRouter()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
return mux
}

View File

@ -1,55 +0,0 @@
package server
import (
"context"
"net"
"net/http"
"os"
"strconv"
"sync"
"github.com/inconshreveable/log15"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
)
const Port = 3190
type Server struct {
server *http.Server
once sync.Once
}
var _ goroutine.BackgroundRoutine = &Server{}
func New() *Server {
host := ""
if env.InsecureDev {
host = "127.0.0.1"
}
s := &Server{}
s.server = &http.Server{
Addr: net.JoinHostPort(host, strconv.FormatInt(int64(Port), 10)),
Handler: ot.Middleware(s.handler()),
}
return s
}
func (s *Server) Start() {
if err := s.server.ListenAndServe(); err != http.ErrServerClosed {
log15.Error("Failed to start server", "error", err)
os.Exit(1)
}
}
func (s *Server) Stop() {
s.once.Do(func() {
if err := s.server.Shutdown(context.Background()); err != nil {
log15.Error("Failed to shutdown server", "error", err)
}
})
}

View File

@ -4,22 +4,25 @@ import (
"context"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/inconshreveable/log15"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/sourcegraph/sourcegraph/enterprise/cmd/precise-code-intel-indexer-vm/internal/heartbeat"
indexmanager "github.com/sourcegraph/sourcegraph/enterprise/cmd/precise-code-intel-indexer-vm/internal/index_manager"
"github.com/sourcegraph/sourcegraph/enterprise/cmd/precise-code-intel-indexer-vm/internal/indexer"
"github.com/sourcegraph/sourcegraph/enterprise/cmd/precise-code-intel-indexer-vm/internal/server"
queue "github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/queue/client"
"github.com/sourcegraph/sourcegraph/internal/debugserver"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/httpserver"
"github.com/sourcegraph/sourcegraph/internal/logging"
"github.com/sourcegraph/sourcegraph/internal/observation"
"github.com/sourcegraph/sourcegraph/internal/trace"
)
const Port = 3190
func main() {
env.Lock()
env.HandleHelpFlag()
@ -59,7 +62,8 @@ func main() {
internalProxyAuthToken,
)
indexManager := indexmanager.New()
server := server.New()
server := httpserver.New(Port, func(router *mux.Router) {})
heartbeater := heartbeat.NewHeartbeater(context.Background(), queueClient, indexManager, heartbeat.HeartbeaterOptions{
Interval: indexerHeartbeatInterval,
})

View File

@ -9,16 +9,11 @@ import (
"github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/queue/types"
)
func (s *Server) handler() http.Handler {
mux := mux.NewRouter()
mux.Path("/dequeue").Methods("POST").HandlerFunc(s.handleDequeue)
mux.Path("/setlog").Methods("POST").HandlerFunc(s.handleSetLogContents)
mux.Path("/complete").Methods("POST").HandlerFunc(s.handleComplete)
mux.Path("/heartbeat").Methods("POST").HandlerFunc(s.handleHeartbeat)
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
return mux
func (s *Server) setupRoutes(router *mux.Router) {
router.Path("/dequeue").Methods("POST").HandlerFunc(s.handleDequeue)
router.Path("/setlog").Methods("POST").HandlerFunc(s.handleSetLogContents)
router.Path("/complete").Methods("POST").HandlerFunc(s.handleComplete)
router.Path("/heartbeat").Methods("POST").HandlerFunc(s.handleHeartbeat)
}
// POST /dequeue

View File

@ -1,59 +1,21 @@
package server
import (
"context"
"net"
"net/http"
"os"
"strconv"
"sync"
"github.com/inconshreveable/log15"
indexmanager "github.com/sourcegraph/sourcegraph/enterprise/cmd/precise-code-intel-indexer/internal/index_manager"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
"github.com/sourcegraph/sourcegraph/internal/httpserver"
)
const Port = 3189
type Server struct {
indexManager indexmanager.Manager
server *http.Server
once sync.Once
}
var _ goroutine.BackgroundRoutine = &Server{}
func New(indexManager indexmanager.Manager) *Server {
host := ""
if env.InsecureDev {
host = "127.0.0.1"
}
s := &Server{
func New(indexManager indexmanager.Manager) goroutine.BackgroundRoutine {
server := &Server{
indexManager: indexManager,
}
s.server = &http.Server{
Addr: net.JoinHostPort(host, strconv.FormatInt(int64(Port), 10)),
Handler: ot.Middleware(s.handler()),
}
return s
}
func (s *Server) Start() {
if err := s.server.ListenAndServe(); err != http.ErrServerClosed {
log15.Error("Failed to start server", "error", err)
os.Exit(1)
}
}
func (s *Server) Stop() {
s.once.Do(func() {
if err := s.server.Shutdown(context.Background()); err != nil {
log15.Error("Failed to shutdown server", "error", err)
}
})
return httpserver.New(Port, server.setupRoutes)
}

View File

@ -1,15 +0,0 @@
package server
import (
"net/http"
"github.com/gorilla/mux"
)
func (s *Server) handler() http.Handler {
mux := mux.NewRouter()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
return mux
}

View File

@ -1,55 +0,0 @@
package server
import (
"context"
"net"
"net/http"
"os"
"strconv"
"sync"
"github.com/inconshreveable/log15"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
)
const Port = 3188
type Server struct {
server *http.Server
once sync.Once
}
var _ goroutine.BackgroundRoutine = &Server{}
func New() *Server {
host := ""
if env.InsecureDev {
host = "127.0.0.1"
}
s := &Server{}
s.server = &http.Server{
Addr: net.JoinHostPort(host, strconv.FormatInt(int64(Port), 10)),
Handler: ot.Middleware(s.handler()),
}
return s
}
func (s *Server) Start() {
if err := s.server.ListenAndServe(); err != http.ErrServerClosed {
log15.Error("Failed to start server", "error", err)
os.Exit(1)
}
}
func (s *Server) Stop() {
s.once.Do(func() {
if err := s.server.Shutdown(context.Background()); err != nil {
log15.Error("Failed to shutdown server", "error", err)
}
})
}

View File

@ -4,13 +4,13 @@ import (
"database/sql"
"log"
"github.com/gorilla/mux"
"github.com/inconshreveable/log15"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
commitupdater "github.com/sourcegraph/sourcegraph/enterprise/cmd/precise-code-intel-worker/internal/commit-updater"
"github.com/sourcegraph/sourcegraph/enterprise/cmd/precise-code-intel-worker/internal/metrics"
"github.com/sourcegraph/sourcegraph/enterprise/cmd/precise-code-intel-worker/internal/resetter"
"github.com/sourcegraph/sourcegraph/enterprise/cmd/precise-code-intel-worker/internal/server"
"github.com/sourcegraph/sourcegraph/enterprise/cmd/precise-code-intel-worker/internal/worker"
bundles "github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/bundles/client"
"github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/commits"
@ -21,6 +21,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/debugserver"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/httpserver"
"github.com/sourcegraph/sourcegraph/internal/logging"
"github.com/sourcegraph/sourcegraph/internal/observation"
"github.com/sourcegraph/sourcegraph/internal/sqliteutil"
@ -28,6 +29,8 @@ import (
"github.com/sourcegraph/sourcegraph/internal/tracer"
)
const Port = 3188
func main() {
env.Lock()
env.HandleHelpFlag()
@ -58,7 +61,7 @@ func main() {
MustRegisterQueueMonitor(observationContext.Registerer, store)
workerMetrics := metrics.NewWorkerMetrics(observationContext)
resetterMetrics := resetter.NewResetterMetrics(prometheus.DefaultRegisterer)
server := server.New()
server := httpserver.New(Port, func(router *mux.Router) {})
uploadResetter := resetter.NewUploadResetter(store, resetInterval, resetterMetrics)
commitUpdater := commitupdater.NewUpdater(
store,

View File

@ -0,0 +1,59 @@
package httpserver
import (
"context"
"net"
"net/http"
"os"
"strconv"
"sync"
"github.com/gorilla/mux"
"github.com/inconshreveable/log15"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
)
type server struct {
server *http.Server
once sync.Once
}
// New returns a BackgroundRoutine that maintains an HTTP server listening on the given
// port with a router configured with the given function. All servers will respond 200
// to requests to /healthz.
func New(port int, setupRoutes func(router *mux.Router)) goroutine.BackgroundRoutine {
host := ""
if env.InsecureDev {
host = "127.0.0.1"
}
router := mux.NewRouter()
setupRoutes(router)
router.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
return &server{
server: &http.Server{
Addr: net.JoinHostPort(host, strconv.FormatInt(int64(port), 10)),
Handler: ot.Middleware(router),
},
}
}
func (s *server) Start() {
if err := s.server.ListenAndServe(); err != http.ErrServerClosed {
log15.Error("Failed to start server", "error", err)
os.Exit(1)
}
}
func (s *server) Stop() {
s.once.Do(func() {
if err := s.server.Shutdown(context.Background()); err != nil {
log15.Error("Failed to shutdown server", "error", err)
}
})
}