8000 feat: Add Coder Daemon to serve the API (#18) · coder/coder@afc2fa3 · GitHub
[go: up one dir, main page]

Skip to content

Commit afc2fa3

Browse files
authored
feat: Add Coder Daemon to serve the API (#18)
* feat: Add v1 schema types This adds compatibility for sharing data with Coder v1. Since the tables are the same, all CRUD operations should function as expected. * Add license table * feat: Add Coder Daemon to serve the API coderd is a public package which will be consumed by v1 to support running both at the same time. The frontend will need to be compiled and statically served as part of this eventually. * Fix initial migration * Move to /api/v2 * Increase peer disconnectedTimeout to reduce flakes on slow machines * Reduce timeout again * Fix version for pion/ice
1 parent 4308f16 commit afc2fa3

File tree

7 files changed

+291
-25
lines changed

7 files changed

+291
-25
lines changed

cmd/coderd/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/coder/coder/coderd/cmd"
8+
)
9+
10+
func main() {
11+
err := cmd.Root().Execute()
12+
if err != nil {
13+
_, _ = fmt.Println(err.Error())
14+
os.Exit(1)
15+
}
16+
}

coderd/cmd/root.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cmd
2+
3+
import (
4+
"net"
5+
"net/http"
6+
"os"
7+
8+
"cdr.dev/slog"
9+
"cdr.dev/slog/sloggers/sloghuman"
10+
"github.com/coder/coder/coderd"
11+
"github.com/coder/coder/database"
12+
"github.com/spf13/cobra"
13+
"golang.org/x/xerrors"
14+
)
15+
16+
func Root() *cobra.Command {
17+
var (
18+
address string
19+
)
20+
root := &cobra.Command{
21+
Use: "coderd",
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
handler := coderd.New(&coderd.Options{
24+
Logger: slog.Make(sloghuman.Sink(os.Stderr)),
25+
Database: database.NewInMemory(),
26+
})
27+
28+
listener, err := net.Listen("tcp", address)
29+
if err != nil {
30+
return xerrors.Errorf("listen %q: %w", address, err)
31+
}
32+
defer listener.Close()
33+
34+
errCh := make(chan error)
35+
go func() {
36+
defer close(errCh)
37+
errCh <- http.Serve(listener, handler)
38+
}()
39+
40+
select {
41+
case <-cmd.Context().Done():
42+
return cmd.Context().Err()
43+
case err := <-errCh:
44+
return err
45+
}
46+
},
47+
}
48+
defaultAddress, ok := os.LookupEnv("ADDRESS")
49+
if !ok {
50+
defaultAddress = "127.0.0.1:3000"
51+
}
52+
root.Flags().StringVarP(&address, "address", "a", defaultAddress, "The address to serve the API and dashboard.")
53+
54+
return root
55+
}

coderd/cmd/root_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cmd_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/coder/coder/coderd/cmd"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestRoot(t *testing.T) {
12+
ctx, cancelFunc := context.WithCancel(context.Background())
13+
go cancelFunc()
14+
err := cmd.Root().ExecuteContext(ctx)
15+
require.ErrorIs(t, err, context.Canceled)
16+
}

coderd/coderd.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package coderd
2+
3+
import (
4+
"net/http"
5+
6+
"cdr.dev/slog"
7+
"github.com/coder/coder/database"
8+
"github.com/go-chi/chi"
9+
"github.com/go-chi/render"
10+
)
11+
12+
// Options are requires parameters for Coder to start.
13+
type Options struct {
14+
Logger slog.Logger
15+
Database database.Store
16+
}
17+
18+
// New constructs the Coder API into an HTTP handler.
19+
func New(options *Options) http.Handler {
20+
r := chi.NewRouter()
21+
r.Route("/api/v2", func(r chi.Router) {
22+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
23+
render.JSON(w, r, struct {
24+
Message string `json:"message"`
25+
}{
26+
Message: "👋",
27+
})
28+
})
29+
})
30+
return r
31+
}

database/migrations/000001_base.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ CREATE TABLE IF NOT EXISTS api_keys (
8585
devurl_token boolean DEFAULT false NOT NULL
8686
);
8787

88-
CREATE TABLE licenses (
88+
CREATE TABLE IF NOT EXISTS licenses (
8989
id integer NOT NULL,
9090
license jsonb NOT NULL,
9191
created_at timestamp with time zone NOT NULL

go.mod

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ go 1.17
66
replace github.com/hashicorp/terraform-config-inspect => github.com/kylecarbs/terraform-config-inspect v0.0.0-20211215004401-bbc517866b88
77

88
// Required until https://github.com/pion/ice/pull/411 is merged.
9-
replace github.com/pion/ice/v2 => github.com/kylecarbs/ice/v2 v2.1.8-0.20220113174531-3e2410788467
9+
replace github.com/pion/ice/v2 => github.com/kylecarbs/ice/v2 v2.1.8-0.20220113224934-e3297ead83b2
1010

1111
require (
1212
cdr.dev/slog v1.4.1
13+
github.com/go-chi/chi v1.5.4
14+
github.com/go-chi/render v1.0.1
1315
github.com/golang-migrate/migrate/v4 v4.15.1
1416
github.com/google/uuid v1.3.0
1517
github.com/hashicorp/go-version v1.3.0
@@ -22,6 +24,7 @@ require (
2224
github.com/pion/logging v0.2.2
2325
github.com/pion/transport v0.13.0
2426
github.com/pion/webrtc/v3 v3.1.13
27+
github.com/spf13/cobra v1.3.0
2528
github.com/stretchr/testify v1.7.0
2629
go.uber.org/atomic v1.7.0
2730
go.uber.org/goleak v1.1.12
@@ -31,7 +34,6 @@ require (
3134
)
3235

3336
require (
34-
cloud.google.com/go v0.92.3 // indirect
3537
cloud.google.com/go/storage v1.14.0 // indirect
3638
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
3739
github.com/Microsoft/go-winio v0.5.1 // indirect
@@ -50,9 +52,9 @@ require (
5052
github.com/docker/docker v20.10.12+incompatible // indirect
5153
github.com/docker/go-connections v0.4.0 // indirect
5254
github.com/docker/go-units v0.4.0 // indirect
53-
github.com/fatih/color v1.12.0 // indirect
55+
github.com/fatih/color v1.13.0 // indirect
5456
github.com/gogo/protobuf v1.3.2 // indirect
55-
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
57+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5658
github.com/google/go-cmp v0.5.6 // indirect
5759
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
5860
github.com/hashicorp/errwrap v1.0.0 // indirect
@@ -62,10 +64,11 @@ require (
6264
github.com/hashicorp/hcl/v2 v2.0.0 // indirect
6365
github.com/hashicorp/terraform-json v0.13.0 // indirect
6466
github.com/imdario/mergo v0.3.12 // indirect
65-
github.com/mattn/go-colorable v0.1.8 // indirect
66-
github.com/mattn/go-isatty v0.0.13 // indirect
67+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
68+
github.com/mattn/go-colorable v0.1.12 // indirect
69+
github.com/mattn/go-isatty v0.0.14 // indirect
6770
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
68-
github.com/mitchellh/mapstructure v1.4.1 // indirect
71+
github.com/mitchellh/mapstructure v1.4.3 // indirect
6972
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
7073
github.com/opencontainers/go-digest v1.0.0 // indirect
7174
github.com/opencontainers/image-spec v1.0.2 // indirect
@@ -86,6 +89,7 @@ require (
8689
github.com/pkg/errors v0.9.1 // indirect
8790
github.com/pmezard/go-difflib v1.0.0 // indirect
8891
github.com/sirupsen/logrus v1.8.1 // indirect
92+
github.com/spf13/pflag v1.0.5 // indirect
8993
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
9094
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
9195
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
@@ -94,9 +98,10 @@ require (
9498
go.opencensus.io v0.23.0 // indirect
9599
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
96100
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
97-
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c // indirect
101+
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
98102
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
99103
golang.org/x/text v0.3.7 // indirect
104+
google.golang.org/api v0.63.0 // indirect
100105
google.golang.org/grpc v1.43.0 // indirect
101106
gopkg.in/yaml.v2 v2.4.0 // indirect
102107
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect

0 commit comments

Comments
 (0)
0