2022-01-13 16:55:28 -06:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2022-01-30 19:13:14 -08:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-01-20 07:46:51 -06:00
|
|
|
|
2022-01-13 16:55:28 -06:00
|
|
|
"cdr.dev/slog"
|
|
|
|
|
"github.com/coder/coder/database"
|
2022-01-20 07:46:51 -06:00
|
|
|
"github.com/coder/coder/httpapi"
|
|
|
|
|
"github.com/coder/coder/httpmw"
|
2022-01-18 13:13:19 -08:00
|
|
|
"github.com/coder/coder/site"
|
2022-01-13 16:55:28 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Options are requires parameters for Coder to start.
|
|
|
|
|
type Options struct {
|
|
|
|
|
Logger slog.Logger
|
|
|
|
|
Database database.Store
|
2022-02-01 16:15:26 -06:00
|
|
|
Pubsub database.Pubsub
|
2022-01-13 16:55:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New constructs the Coder API into an HTTP handler.
|
|
|
|
|
func New(options *Options) http.Handler {
|
2022-02-01 16:15:26 -06:00
|
|
|
api := &api{
|
2022-01-25 13:52:58 -06:00
|
|
|
Database: options.Database,
|
2022-02-03 19:13:22 -06:00
|
|
|
Logger: options.Logger,
|
2022-02-01 16:15:26 -06:00
|
|
|
Pubsub: options.Pubsub,
|
2022-01-25 13:52:58 -06:00
|
|
|
}
|
2022-01-20 07:46:51 -06:00
|
|
|
|
2022-01-13 16:55:28 -06:00
|
|
|
r := chi.NewRouter()
|
|
|
|
|
r.Route("/api/v2", func(r chi.Router) {
|
|
|
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
2022-01-20 07:46:51 -06:00
|
|
|
httpapi.Write(w, http.StatusOK, httpapi.Response{
|
2022-01-13 16:55:28 -06:00
|
|
|
Message: "👋",
|
|
|
|
|
})
|
|
|
|
|
})
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Post("/login", api.postLogin)
|
|
|
|
|
r.Post("/logout", api.postLogout)
|
2022-01-25 13:52:58 -06:00
|
|
|
// Used for setup.
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Post("/user", api.postUser)
|
2022-01-22 23:58:10 -06:00
|
|
|
r.Route("/users", func(r chi.Router) {
|
2022-01-25 13:52:58 -06:00
|
|
|
r.Use(
|
|
|
|
|
httpmw.ExtractAPIKey(options.Database, nil),
|
|
|
|
|
)
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Post("/", api.postUsers)
|
2022-01-22 23:58:10 -06:00
|
|
|
r.Group(func(r chi.Router) {
|
2022-01-25 13:52:58 -06:00
|
|
|
r.Use(httpmw.ExtractUserParam(options.Database))
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Get("/{user}", api.userByName)
|
|
|
|
|
r.Get("/{user}/organizations", api.organizationsByUser)
|
2022-01-22 23:58:10 -06:00
|
|
|
})
|
2022-01-20 07:46:51 -06:00
|
|
|
})
|
2022-01-24 11:07:42 -06:00
|
|
|
r.Route("/projects", func(r chi.Router) {
|
|
|
|
|
r.Use(
|
|
|
|
|
httpmw.ExtractAPIKey(options.Database, nil),
|
|
|
|
|
)
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Get("/", api.projects)
|
2022-01-24 11:07:42 -06:00
|
|
|
r.Route("/{organization}", func(r chi.Router) {
|
|
|
|
|
r.Use(httpmw.ExtractOrganizationParam(options.Database))
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Get("/", api.projectsByOrganization)
|
|
|
|
|
r.Post("/", api.postProjectsByOrganization)
|
2022-01-24 11:07:42 -06:00
|
|
|
r.Route("/{project}", func(r chi.Router) {
|
2022-01-25 13:52:58 -06:00
|
|
|
r.Use(httpmw.ExtractProjectParam(options.Database))
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Get("/", api.projectByOrganization)
|
|
|
|
|
r.Get("/workspaces", api.workspacesByProject)
|
2022-02-04 12:11:24 -06:00
|
|
|
r.Route("/parameters", func(r chi.Router) {
|
|
|
|
|
r.Get("/", api.parametersByProject)
|
|
|
|
|
r.Post("/", api.postParametersByProject)
|
|
|
|
|
})
|
2022-02-04 20:47:58 -06:00
|
|
|
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)
|
|
|
|
|
r.Get("/parameters", api.projectVersionParametersByOrganizationAndName)
|
2022-02-03 14:34:50 -06:00
|
|
|
})
|
2022-01-25 13:52:58 -06:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 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))
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Get("/", api.workspaces)
|
2022-01-25 13:52:58 -06:00
|
|
|
r.Route("/{user}", func(r chi.Router) {
|
|
|
|
|
r.Use(httpmw.ExtractUserParam(options.Database))
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Post("/", api.postWorkspaceByUser)
|
2022-01-25 13:52:58 -06:00
|
|
|
r.Route("/{workspace}", func(r chi.Router) {
|
|
|
|
|
r.Use(httpmw.ExtractWorkspaceParam(options.Database))
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Get("/", api.workspaceByUser)
|
2022-02-04 20:47:58 -06:00
|
|
|
r.Route("/version", func(r chi.Router) {
|
2022-02-01 16:15:26 -06:00
|
|
|
r.Post("/", api.postWorkspaceHistoryByUser)
|
|
|
|
|
r.Get("/", api.workspaceHistoryByUser)
|
2022-02-03 14:34:50 -06:00
|
|
|
r.Route("/{workspacehistory}", func(r chi.Router) {
|
|
|
|
|
r.Use(httpmw.ExtractWorkspaceHistoryParam(options.Database))
|
|
|
|
|
r.Get("/", api.workspaceHistoryByName)
|
2022-02-04 13:36:43 -06:00
|
|
|
r.Get("/logs", api.workspaceHistoryLogsByName)
|
2022-02-03 14:34:50 -06:00
|
|
|
})
|
2022-01-24 11:07:42 -06:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2022-02-03 14:34:50 -06:00
|
|
|
|
|
|
|
|
r.Route("/provisioners/daemons", func(r chi.Router) {
|
|
|
|
|
r.Get("/", api.provisionerDaemons)
|
|
|
|
|
r.Get("/serve", api.provisionerDaemonsServe)
|
|
|
|
|
})
|
2022-01-13 16:55:28 -06:00
|
|
|
})
|
2022-01-18 13:13:19 -08:00
|
|
|
r.NotFound(site.Handler().ServeHTTP)
|
2022-01-13 16:55:28 -06:00
|
|
|
return r
|
|
|
|
|
}
|
2022-02-01 16:15:26 -06:00
|
|
|
|
|
|
|
|
// API contains all route handlers. Only HTTP handlers should
|
|
|
|
|
// be added to this struct for code clarity.
|
|
|
|
|
type api struct {
|
|
|
|
|
Database database.Store
|
2022-02-03 19:13:22 -06:00
|
|
|
Logger slog.Logger
|
2022-02-01 16:15:26 -06:00
|
|
|
Pubsub database.Pubsub
|
|
|
|
|
}
|