mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
Fixes #210 - this isPR implements `coder login` in the case where the default user is already created. This change adds: - A prompt in the case where there is not an initial user that opens the server URL + requests a session token - This ports over some code from v1 for the `openURL` and `isWSL` functions to support opening the browser - A `/api/v2/api-keys` endpoint that can be `POST`'d to in order to request a new api key for a user - This route was inspired by the v1 functionality - A `cli-auth` route + page that shows the generated api key - Tests for the new code + storybook for the new UI The `/cli-auth` route, like in v1, is very minimal: <img width="624" alt="Screen Shot 2022-02-16 at 5 05 07 PM" src="https://user-images.githubusercontent.com/88213859/154384627-78ab9841-27bf-490f-9bbe-23f8173c9e97.png"> And the terminal UX looks like this: 
157 lines
4.7 KiB
Go
157 lines
4.7 KiB
Go
package coderd
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"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
|
|
Pubsub database.Pubsub
|
|
}
|
|
|
|
// New constructs the Coder API into an HTTP handler.
|
|
func New(options *Options) http.Handler {
|
|
api := &api{
|
|
Database: options.Database,
|
|
Logger: options.Logger,
|
|
Pubsub: options.Pubsub,
|
|
}
|
|
|
|
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("/login", api.postLogin)
|
|
r.Post("/logout", api.postLogout)
|
|
|
|
// Used for setup.
|
|
r.Get("/user", api.user)
|
|
r.Post("/user", api.postUser)
|
|
r.Route("/users", func(r chi.Router) {
|
|
r.Use(
|
|
httpmw.ExtractAPIKey(options.Database, nil),
|
|
)
|
|
r.Post("/", api.postUsers)
|
|
|
|
r.Route("/{user}", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractUserParam(options.Database))
|
|
r.Get("/", api.userByName)
|
|
r.Get("/organizations", api.organizationsByUser)
|
|
r.Post("/keys", api.postKeyForUser)
|
|
})
|
|
})
|
|
r.Route("/projects", func(r chi.Router) {
|
|
r.Use(
|
|
httpmw.ExtractAPIKey(options.Database, nil),
|
|
)
|
|
r.Get("/", api.projects)
|
|
r.Route("/{organization}", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractOrganizationParam(options.Database))
|
|
r.Get("/", api.projectsByOrganization)
|
|
r.Post("/", api.postProjectsByOrganization)
|
|
r.Route("/{project}", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractProjectParam(options.Database))
|
|
r.Get("/", api.projectByOrganization)
|
|
r.Get("/workspaces", api.workspacesByProject)
|
|
r.Route("/parameters", func(r chi.Router) {
|
|
r.Get("/", api.parametersByProject)
|
|
r.Post("/", api.postParametersByProject)
|
|
})
|
|
r.Route("/versions", func(r chi.Router) {
|
|
r.Get("/", api.projectVersionsByOrganization)
|
|
r.Post("/", api.postProjectVersionByOrganization)
|
|
r.Route("/{projectversion}", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractProjectVersionParam(api.Database))
|
|
r.Get("/", api.projectVersionByOrganizationAndName)
|
|
})
|
|
})
|
|
})
|
|
})
|
|
})
|
|
|
|
// Listing operations specific to resources should go under
|
|
// their respective routes. eg. /orgs/<name>/workspaces
|
|
r.Route("/workspaces", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractAPIKey(options.Database, nil))
|
|
r.Get("/", api.workspaces)
|
|
r.Route("/{user}", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractUserParam(options.Database))
|
|
r.Post("/", api.postWorkspaceByUser)
|
|
r.Route("/{workspace}", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractWorkspaceParam(options.Database))
|
|
r.Get("/", api.workspaceByUser)
|
|
r.Route("/version", func(r chi.Router) {
|
|
r.Post("/", api.postWorkspaceHistoryByUser)
|
|
r.Get("/", api.workspaceHistoryByUser)
|
|
r.Route("/{workspacehistory}", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractWorkspaceHistoryParam(options.Database))
|
|
r.Get("/", api.workspaceHistoryByName)
|
|
})
|
|
})
|
|
})
|
|
})
|
|
})
|
|
|
|
r.Route("/files", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractAPIKey(options.Database, nil))
|
|
r.Post("/", api.postFiles)
|
|
})
|
|
|
|
r.Route("/projectimport/{organization}", func(r chi.Router) {
|
|
r.Use(
|
|
httpmw.ExtractAPIKey(options.Database, nil),
|
|
httpmw.ExtractOrganizationParam(options.Database),
|
|
)
|
|
r.Post("/", api.postProjectImportByOrganization)
|
|
r.Route("/{provisionerjob}", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractProvisionerJobParam(options.Database))
|
|
r.Get("/", api.provisionerJobByID)
|
|
r.Get("/schemas", api.projectImportJobSchemasByID)
|
|
r.Get("/parameters", api.projectImportJobParametersByID)
|
|
r.Get("/resources", api.projectImportJobResourcesByID)
|
|
r.Get("/logs", api.provisionerJobLogsByID)
|
|
})
|
|
})
|
|
|
|
r.Route("/workspaceprovision/{organization}", func(r chi.Router) {
|
|
r.Use(
|
|
httpmw.ExtractAPIKey(options.Database, nil),
|
|
httpmw.ExtractOrganizationParam(options.Database),
|
|
)
|
|
r.Route("/{provisionerjob}", func(r chi.Router) {
|
|
r.Use(httpmw.ExtractProvisionerJobParam(options.Database))
|
|
r.Get("/", api.provisionerJobByID)
|
|
r.Get("/logs", api.provisionerJobLogsByID)
|
|
})
|
|
})
|
|
|
|
r.Route("/provisioners/daemons", func(r chi.Router) {
|
|
r.Get("/", api.provisionerDaemons)
|
|
r.Get("/serve", api.provisionerDaemonsServe)
|
|
})
|
|
})
|
|
r.NotFound(site.Handler(options.Logger).ServeHTTP)
|
|
return r
|
|
}
|
|
|
|
// API contains all route handlers. Only HTTP handlers should
|
|
// be added to this struct for code clarity.
|
|
type api struct {
|
|
Database database.Store
|
|
Logger slog.Logger
|
|
Pubsub database.Pubsub
|
|
}
|