Skip to content

Commit 6726f40

Browse files
committed
Create initial workspaces API
1 parent 7329cca commit 6726f40

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

coderd/api.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package coderd
22

3-
import "context"
4-
53
// API offers an HTTP API. Routes are located in routes.go.
64
type API struct {
75
// Services.
8-
projectService *projectService
6+
projectService *projectService
7+
workspaceService *workspaceService
98
}
109

1110
// New returns an instantiated API.
12-
func NewAPI(ctx context.Context) *API {
11+
func NewAPI() *API {
1312
api := &API{
14-
projectService: newProjectService(),
13+
projectService: newProjectService(),
14+
workspaceService: newWorkspaceService(),
1515
}
1616
return api
1717
}

coderd/coderd.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package coderd
22

33
import (
4-
"context"
54
"net/http"
65

76
"cdr.dev/slog"
@@ -24,7 +23,7 @@ const (
2423

2524
// New constructs the Coder API into an HTTP handler.
2625
func New(options *Options) http.Handler {
27-
api := NewAPI(context.Background())
26+
api := NewAPI()
2827
r := chi.NewRouter()
2928
r.Route("/api/v2", func(r chi.Router) {
3029
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
@@ -43,6 +42,17 @@ func New(options *Options) http.Handler {
4342
// TODO: Extract organization and add to context
4443
r.Get("/", api.projectService.getProjects)
4544
r.Post("/", api.projectService.createProject)
45+
46+
r.Get("/{projectId}", api.projectService.getProjectById)
47+
// TODO: Get project by id
48+
})
49+
})
50+
51+
// Workspaces endpoint
52+
r.Route("/workspaces", func(r chi.Router) {
53+
r.Route("/{organization}", func(r chi.Router) {
54+
r.Get("/", api.workspaceService.getWorkspaces)
55+
r.Get("/{projectId}", api.workspaceService.getWorkspaceById)
4656
})
4757
})
4858

coderd/projects.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ func newProjectService() *projectService {
3636
}
3737

3838
func (ps *projectService) getProjects(w http.ResponseWriter, r *http.Request) {
39-
4039
// Construct a couple hard-coded projects to return the UI
41-
4240
terraformProject := Project{
4341
Id: "test_terraform_project_id",
4442
Name: "Terraform",
@@ -81,6 +79,11 @@ func (ps *projectService) getProjects(w http.ResponseWriter, r *http.Request) {
8179
xjson.Write(w, http.StatusOK, projects)
8280
}
8381

82+
func (ps *projectService) getProjectById(w http.ResponseWriter, r *http.Request) {
83+
// TODO: Get a project by id
84+
xjson.Write(w, http.StatusNotFound, nil)
85+
}
86+
8487
func (ps *projectService) createProject(w http.ResponseWriter, r *http.Request) {
8588
// TODO: Validate arguments
8689
// Organization context

coderd/workspaces.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package coderd
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/coder/coder/xjson"
7+
)
8+
9+
type Workspace struct {
10+
Id string `json:"id" validate:"required"`
11+
Name string `json:"name" validate:"required"`
12+
ProjectId string `json:"project_id" validate:"required"`
13+
}
14+
15+
// Placeholder type of workspaceService
16+
type workspaceService struct {
17+
}
18+
19+
func newWorkspaceService() *workspaceService {
20+
workspaceService := &workspaceService{}
21+
return workspaceService
22+
}
23+
24+
func (ws *workspaceService) getWorkspaces(w http.ResponseWriter, r *http.Request) {
25+
// Dummy workspace to return
26+
workspace := Workspace{
27+
Id: "test-workspace",
28+
Name: "Test Workspace",
29+
ProjectId: "test-project-id",
30+
}
31+
32+
workspaces := []Workspace{
33+
workspace,
34+
}
35+
36+
xjson.Write(w, http.StatusOK, workspaces)
37+
}
38+
39+
func (ws *workspaceService) getWorkspaceById(w http.ResponseWriter, r *http.Request) {
40+
// TODO: Read workspace off context
41+
// Dummy workspace to return
42+
workspace := Workspace{
43+
Id: "test-workspace",
44+
Name: "Test Workspace",
45+
ProjectId: "test-project-id",
46+
}
47+
xjson.Write(w, http.StatusOK, workspace)
48+
}

0 commit comments

Comments
 (0)