Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b0899c

Browse files
committed
Cleanup routes
1 parent b67f8d4 commit 7b0899c

11 files changed

+198
-123
lines changed

coderd/coderd.go

+38-37
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,18 @@ func New(options *Options) (http.Handler, func()) {
4141
Message: "👋",
4242
})
4343
})
44-
r.Post("/login", api.postLogin)
45-
r.Post("/logout", api.postLogout)
46-
47-
r.Route("/user", func(r chi.Router) {
48-
r.Get("/first", api.user)
49-
r.Post("/first", api.user)
50-
r.Group(func(r chi.Router) {
51-
r.Use(httpmw.ExtractAPIKey(options.Database, nil))
52-
r.Post("/", nil)
53-
r.Route("/{user}", func(r chi.Router) {
54-
r.Use(httpmw.ExtractUserParam(options.Database))
55-
r.Get("/", api.userByName)
56-
r.Get("/organizations", api.organizationsByUser)
57-
r.Post("/keys", api.postKeyForUser)
58-
})
59-
})
44+
r.Route("/files", func(r chi.Router) {
45+
r.Use(httpmw.ExtractAPIKey(options.Database, nil))
46+
r.Post("/", api.postUpload)
47+
r.Get("/{hash}", nil)
6048
})
61-
6249
r.Route("/organization/{organization}", func(r chi.Router) {
6350
r.Use(
6451
httpmw.ExtractAPIKey(options.Database, nil),
6552
httpmw.ExtractOrganizationParam(options.Database),
6653
)
67-
r.Post("/users", nil)
6854
r.Get("/provisionerdaemons", nil)
55+
r.Post("/projectversion", nil)
6956
r.Route("/projects", func(r chi.Router) {
7057
r.Post("/", api.postProjectsByOrganization)
7158
r.Get("/", api.projectsByOrganization)
@@ -79,11 +66,13 @@ func New(options *Options) (http.Handler, func()) {
7966
httpmw.ExtractOrganizationParam(options.Database),
8067
)
8168
r.Get("/", api.projectByOrganization)
69+
r.Delete("/", nil)
8270
r.Get("/workspaces", api.workspacesByProject)
8371
r.Get("/parameters", api.parametersByProject)
8472
r.Post("/parameters", api.postParametersByProject)
8573
r.Get("/versions", api.projectVersionsByOrganization)
86-
r.Post("/versions", api.postProjectVersionByOrganization)
74+
r.Get("/versions/latest", nil)
75+
r.Patch("/versions", nil)
8776
})
8877
r.Route("/projectversion/{projectversion}", func(r chi.Router) {
8978
r.Use(
@@ -98,6 +87,36 @@ func New(options *Options) (http.Handler, func()) {
9887
r.Get("/logs", nil)
9988
r.Get("/resources", nil)
10089
})
90+
r.Route("/provisionerdaemon", func(r chi.Router) {
91+
r.Route("/me", func(r chi.Router) {
92+
r.Get("/listen", api.provisionerDaemonsServe)
93+
})
94+
})
95+
r.Route("/user", func(r chi.Router) {
96+
r.Post("/login", api.postLogin)
97+
r.Post("/logout", api.postLogout)
98+
r.Get("/first", api.user)
99+
r.Post("/first", api.user)
100+
r.Group(func(r chi.Router) {
101+
r.Use(httpmw.ExtractAPIKey(options.Database, nil))
102+
r.Post("/", nil)
103+
r.Route("/{user}", func(r chi.Router) {
104+
r.Use(httpmw.ExtractUserParam(options.Database))
105+
r.Get("/", api.userByName)
106+
r.Get("/organizations", api.organizationsByUser)
107+
r.Post("/keys", api.postKeyForUser)
108+
})
109+
})
110+
})
111+
r.Route("/workspaceagent", func(r chi.Router) {
112+
r.Route("/auth", func(r chi.Router) {
113+
r.Post("/google-instance-identity", api.postAuthenticateWorkspaceAgentUsingGoogleInstanceIdentity)
114+
})
115+
r.Route("/me", func(r chi.Router) {
116+
r.Use(httpmw.ExtractWorkspaceAgent(options.Database))
117+
r.Get("/listen", nil)
118+
})
119+
})
101120
r.Route("/workspace/{workspace}", func(r chi.Router) {
102121
r.Use(
103122
httpmw.ExtractAPIKey(options.Database, nil),
@@ -122,24 +141,6 @@ func New(options *Options) (http.Handler, func()) {
122141
r.Get("/dial", nil)
123142
})
124143
})
125-
r.Route("/workspaceagent", func(r chi.Router) {
126-
r.Route("/auth", func(r chi.Router) {
127-
r.Post("/google-instance-identity", api.postAuthenticateWorkspaceAgentUsingGoogleInstanceIdentity)
128-
})
129-
r.Route("/me", func(r chi.Router) {
130-
r.Use(httpmw.ExtractWorkspaceAgent(options.Database))
131-
r.Get("/listen", nil)
132-
})
133-
})
134-
r.Route("/provisionerdaemon", func(r chi.Router) {
135-
r.Route("/me", func(r chi.Router) {
136-
r.Get("/listen", api.provisionerDaemonsServe)
137-
})
138-
})
139-
r.Route("/upload", func(r chi.Router) {
140-
r.Use(httpmw.ExtractAPIKey(options.Database, nil))
141-
r.Post("/", api.postUpload)
142-
})
143144
})
144145
r.NotFound(site.Handler(options.Logger).ServeHTTP)
145146
return r, api.websocketWaitGroup.Wait

coderd/files.go

+29
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package coderd
22

33
import (
44
"crypto/sha256"
5+
"database/sql"
56
"encoding/hex"
7+
"errors"
68
"fmt"
79
"io"
810
"net/http"
911

12+
"github.com/go-chi/chi/v5"
1013
"github.com/go-chi/render"
1114

1215
"github.com/coder/coder/database"
@@ -68,3 +71,29 @@ func (api *api) postUpload(rw http.ResponseWriter, r *http.Request) {
6871
Hash: file.Hash,
6972
})
7073
}
74+
75+
func (api *api) download(rw http.ResponseWriter, r *http.Request) {
76+
hash := chi.URLParam(r, "hash")
77+
if hash == "" {
78+
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
79+
Message: "hash must be provided",
80+
})
81+
return
82+
}
83+
file, err := api.Database.GetFileByHash(r.Context(), hash)
84+
if errors.Is(err, sql.ErrNoRows) {
85+
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
86+
Message: "no file exists with that hash",
87+
})
88+
return
89+
}
90+
if err != nil {
91+
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
92+
Message: fmt.Sprintf("get file: %s", err),
93+
})
94+
return
95+
}
96+
rw.WriteHeader(http.StatusOK)
97+
rw.Header().Set("Content-Type", file.Mimetype)
98+
rw.Write(file.Data)
99+
}

coderd/files_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@ func TestPostUpload(t *testing.T) {
3939
require.NoError(t, err)
4040
})
4141
}
42+
43+
func TestDownload(t *testing.T) {
44+
t.Parallel()
45+
t.Run("BadHash", func(t *testing.T) {
46+
t.Parallel()
47+
client := coderdtest.New(t, nil)
48+
_ = coderdtest.CreateInitialUser(t, client)
49+
50+
})
51+
}

codersdk/files.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package codersdk
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
7+
"io"
68
"net/http"
79

810
"github.com/coder/coder/coderd"
@@ -15,7 +17,7 @@ const (
1517
// Upload uploads an arbitrary file with the content type provided.
1618
// This is used to upload a source-code archive.
1719
func (c *Client) Upload(ctx context.Context, contentType string, content []byte) (coderd.UploadResponse, error) {
18-
res, err := c.request(ctx, http.MethodPost, "/api/v2/upload", content, func(r *http.Request) {
20+
res, err := c.request(ctx, http.MethodPost, "/api/v2/files", content, func(r *http.Request) {
1921
r.Header.Set("Content-Type", contentType)
2022
})
2123
if err != nil {
@@ -28,3 +30,20 @@ func (c *Client) Upload(ctx context.Context, contentType string, content []byte)
2830
var resp coderd.UploadResponse
2931
return resp, json.NewDecoder(res.Body).Decode(&resp)
3032
}
33+
34+
// Download fetches a file by uploaded hash.
35+
func (c *Client) Download(ctx context.Context, hash string) ([]byte, string, error) {
36+
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/files/%s", hash), nil)
37+
if err != nil {
38+
return nil, "", err
39+
}
40+
defer res.Body.Close()
41+
if res.StatusCode != http.StatusOK {
42+
return nil, "", readBodyAsError(res)
43+
}
44+
data, err := io.ReadAll(res.Body)
45+
if err != nil {
46+
return nil, "", err
47+
}
48+
return data, res.Header.Get("Content-Type"), nil
49+
}

codersdk/organization.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package codersdk
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
9+
"github.com/coder/coder/coderd"
10+
"github.com/google/uuid"
11+
)
12+
13+
// OrganizationsByUser returns organizations for the provided user.
14+
func (c *Client) OrganizationsByUser(ctx context.Context, user uuid.UUID) ([]coderd.Organization, error) {
15+
return nil, nil
16+
}
17+
18+
// OrganizationByName returns an organization by case-insensitive name.
19+
func (c *Client) OrganizationByName(ctx context.Context, user uuid.UUID, name string) (coderd.Organization, error) {
20+
return coderd.Organization{}, nil
21+
}
22+
23+
// ProvisionerDaemonsByOrganization returns provisioner daemons available for an organization.
24+
func (c *Client) ProvisionerDaemonsByOrganization(ctx context.Context, organization string) ([]coderd.ProvisionerDaemon, error) {
25+
return nil, nil
26+
}
27+
28+
// CreateProjectVersion processes source-code and optionally associates the version with a project.
29+
// Executing without a project is useful for validating source-code.
30+
func (c *Client) CreateProjectVersion(ctx context.Context, organization uuid.UUID, req coderd.CreateProjectVersion) (coderd.ProjectVersion, error) {
31+
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/organizations/%s/projectversion", organization), req)
32+
if err != nil {
33+
return coderd.ProjectVersion{}, err
34+
}
35+
defer res.Body.Close()
36+
if res.StatusCode != http.StatusCreated {
37+
return coderd.ProjectVersion{}, readBodyAsError(res)
38+
}
39+
var projectVersion coderd.ProjectVersion
40+
return projectVersion, json.NewDecoder(res.Body).Decode(&projectVersion)
41+
}
42+
43+
// CreateProject creates a new project inside an organization.
44+
func (c *Client) CreateProject(ctx context.Context, organization string, request coderd.CreateProjectRequest) (coderd.Project, error) {
45+
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/organizations/%s/projects", organization), request)
46+
if err != nil {
47+
return coderd.Project{}, err
48+
}
49+
defer res.Body.Close()
50+
if res.StatusCode != http.StatusCreated {
51+
return coderd.Project{}, readBodyAsError(res)
52+
}
53+
var project coderd.Project
54+
return project, json.NewDecoder(res.Body).Decode(&project)
55+
}
56+
57+
// ProjectsByOrganization lists all projects inside of an organization.
58+
func (c *Client) ProjectsByOrganization(ctx context.Context, organization uuid.UUID) ([]coderd.Project, error) {
59+
return nil, nil
60+
}
61+
62+
// ProjectByName finds a project inside the organization provided with a case-insensitive name.
63+
func (c *Client) ProjectByName(ctx context.Context, organization uuid.UUID, name string) (coderd.Project, error) {
64+
return coderd.Project{}, nil
65+
}

codersdk/organizations.go

-18
This file was deleted.

codersdk/projects.go

+36-24
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,6 @@ import (
1111
"github.com/coder/coder/coderd"
1212
)
1313

14-
// CreateProject creates a new project inside an organization.
15-
func (c *Client) CreateProject(ctx context.Context, organization uuid.UUID, request coderd.CreateProjectRequest) (coderd.Project, error) {
16-
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/organizations/%s/projects", organization), request)
17-
if err != nil {
18-
return coderd.Project{}, err
19-
}
20-
defer res.Body.Close()
21-
if res.StatusCode != http.StatusCreated {
22-
return coderd.Project{}, readBodyAsError(res)
23-
}
24-
var project coderd.Project
25-
return project, json.NewDecoder(res.Body).Decode(&project)
26-
}
27-
28-
// ProjectsByOrganization lists all projects inside of an organization.
29-
func (c *Client) ProjectsByOrganization(ctx context.Context, organization uuid.UUID) ([]coderd.Project, error) {
30-
return nil, nil
31-
}
32-
33-
// ProjectByName finds a project inside the organization provided with a case-insensitive name.
34-
func (c *Client) ProjectByName(ctx context.Context, organization uuid.UUID, name string) (coderd.Project, error) {
35-
return coderd.Project{}, nil
36-
}
37-
3814
// Project returns a single project.
3915
func (c *Client) Project(ctx context.Context, project uuid.UUID) (coderd.Project, error) {
4016
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/project/%s", project), nil)
@@ -49,6 +25,20 @@ func (c *Client) Project(ctx context.Context, project uuid.UUID) (coderd.Project
4925
return resp, json.NewDecoder(res.Body).Decode(&resp)
5026
}
5127

28+
// WorkspacesByProject lists all workspaces for a specific project.
29+
func (c *Client) WorkspacesByProject(ctx context.Context, project uuid.UUID) ([]coderd.Workspace, error) {
30+
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projects/%s/workspaces", project), nil)
31+
if err != nil {
32+
return nil, err
33+
}
34+
defer res.Body.Close()
35+
if res.StatusCode != http.StatusOK {
36+
return nil, readBodyAsError(res)
37+
}
38+
var workspaces []coderd.Workspace
39+
return workspaces, json.NewDecoder(res.Body).Decode(&workspaces)
40+
}
41+
5242
// ProjectParameters returns parameters scoped to a project.
5343
func (c *Client) ProjectParameters(ctx context.Context, project uuid.UUID) ([]coderd.ParameterValue, error) {
5444
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/project/%s/parameters", project), nil)
@@ -76,3 +66,25 @@ func (c *Client) CreateProjectParameter(ctx context.Context, organization, proje
7666
var param coderd.ParameterValue
7767
return param, json.NewDecoder(res.Body).Decode(&param)
7868
}
69+
70+
// ProjectVersionsByProject lists versions associated with a project.
71+
func (c *Client) ProjectVersionsByProject(ctx context.Context, project uuid.UUID) ([]coderd.ProjectVersion, error) {
72+
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/project/%s/versions", project), nil)
73+
if err != nil {
74+
return nil, err
75+
}
76+
defer res.Body.Close()
77+
if res.StatusCode != http.StatusOK {
78+
return nil, readBodyAsError(res)
79+
}
80+
var projectVersion []coderd.ProjectVersion
81+
return projectVersion, json.NewDecoder(res.Body).Decode(&projectVersion)
82+
}
83+
84+
func (c *Client) DeleteProject(ctx context.Context, project uuid.UUID) error {
85+
return nil
86+
}
87+
88+
func (c *Client) UpdateProjectVersion() {
89+
90+
}

0 commit comments

Comments
 (0)