mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
* chore: Fix golangci-lint configuration and patch errors Due to misconfiguration of a linting rules directory, our linter has not been working properly. This change fixes the configuration issue, and all remaining linting errors. * Fix race in peer logging * Fix race and return * Lock on bufferred amount low * Fix mutex lock
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package coderd
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"cdr.dev/slog"
|
|
"github.com/coder/coder/database"
|
|
"github.com/coder/coder/httpapi"
|
|
"github.com/coder/coder/httpmw"
|
|
"github.com/coder/coder/site"
|
|
)
|
|
|
|
// Options are requires parameters for Coder to start.
|
|
type Options struct {
|
|
Logger slog.Logger
|
|
Database database.Store
|
|
}
|
|
|
|
// New constructs the Coder API into an HTTP handler.
|
|
func New(options *Options) http.Handler {
|
|
users := &users{
|
|
Database: options.Database,
|
|
}
|
|
|
|
r := chi.NewRouter()
|
|
r.Route("/api/v2", func(r chi.Router) {
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
httpapi.Write(w, http.StatusOK, httpapi.Response{
|
|
Message: "👋",
|
|
})
|
|
})
|
|
r.Post("/user", users.createInitialUser)
|
|
r.Post("/login", users.loginWithPassword)
|
|
// Require an API key and authenticated user for this group.
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(
|
|
httpmw.ExtractAPIKey(options.Database, nil),
|
|
httpmw.ExtractUser(options.Database),
|
|
)
|
|
r.Get("/user", users.authenticatedUser)
|
|
})
|
|
})
|
|
r.NotFound(site.Handler().ServeHTTP)
|
|
return r
|
|
}
|