8000 feat: Add Coder Daemon to serve the API by kylecarbs · Pull Request #18 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: Add Coder Daemon to serve the API #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cmd/coderd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"os"

"github.com/coder/coder/coderd/cmd"
)

func main() {
err := cmd.Root().Execute()
if err != nil {
_, _ = fmt.Println(err.Error())
os.Exit(1)
}
}
55 changes: 55 additions & 0 deletions coderd/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"net"
"net/http"
"os"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"github.com/coder/coder/coderd"
"github.com/coder/coder/database"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
)

func Root() *cobra.Command {
var (
address string
)
root := &cobra.Command{
Use: "coderd",
RunE: func(cmd *cobra.Command, args []string) error {
handler := coderd.New(&coderd.Options{
Logger: slog.Make(sloghuman.Sink(os.Stderr)),
Database: database.NewInMemory(),
})

listener, err := net.Listen("tcp", address)
if err != nil {
return xerrors.Errorf("listen %q: %w", address, err)
}
defer listener.Close()

errCh := make(chan error)
go func() {
defer close(errCh)
errCh <- http.Serve(listener, handler)
}()

select {
case <-cmd.Context().Done():
return cmd.Context().Err()
case err := <-errCh:
return err
}
},
}
defaultAddress, ok := os.LookupEnv("ADDRESS")
if !ok {
defaultAddress = "127.0.0.1:3000"
}
root.Flags().StringVarP(&address, "address", "a", defaultAddress, "The address to serve the API and dashboard.")

return root
}
16 changes: 16 additions & 0 deletions coderd/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd_test

import (
"context"
"testing"

"github.com/coder/coder/coderd/cmd"
"github.com/stretchr/testify/require"
)

func TestRoot(t *testing.T) {
ctx, cancelFunc := context.WithCancel(context.Background())
go cancelFunc()
err := cmd.Root().ExecuteContext(ctx)
require.ErrorIs(t, err, context.Canceled)
}
31 changes: 31 additions & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package coderd

import (
"net/http"

"cdr.dev/slog"
"github.com/coder/coder/database"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)

// Options are requires parameters for Coder to start.
type Options struct {
Logger slog.Logger
Database database.Store
}

// New constructs the Coder API into an HTTP handler.
func New(options *Options) http.Handler {
r := chi.NewRouter()
r.Route("/api/v2", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, struct {
Message string `json:"message"`
}{
Message: "👋",
})
})
})
return r
}
2 changes: 1 addition & 1 deletion database/migrations/000001_base.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ CREATE TABLE IF NOT EXISTS api_keys (
devurl_token boolean DEFAULT false NOT NULL
);

CREATE TABLE licenses (
CREATE TABLE IF NOT EXISTS licenses (
id integer NOT NULL,
license jsonb NOT NULL,
created_at timestamp with time zone NOT NULL
Expand Down
21 changes: 13 additions & 8 deletions go.mod
428B
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ go 1.17
replace github.com/hashicorp/terraform-config-inspect => github.com/kylecarbs/terraform-config-inspect v0.0.0-20211215004401-bbc517866b88

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

require (
cdr.dev/slog v1.4.1
github.com/go-chi/chi v1.5.4
github.com/go-chi/render v1.0.1
github.com/golang-migrate/migrate/v4 v4.15.1
github.com/google/uuid v1.3.0
github.com/hashicorp/go-version v1.3.0
Expand All @@ -22,6 +24,7 @@ require (
github.com/pion/logging v0.2.2
github.com/pion/transport v0.13.0
github.com/pion/webrtc/v3 v3.1.13
github.com/spf13/cobra v1.3.0
github.com/stretchr/testify v1.7.0
go.uber.org/atomic v1.7.0
go.uber.org/goleak v1.1.12
Expand All @@ -31,7 +34,6 @@ require (
)

require (
cloud.google.com/go v0.92.3 // indirect
cloud.google.com/go/storage v1.14.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
Expand All @@ -50,9 +52,9 @@ require (
github.com/docker/docker v20.10.12+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
Expand All @@ -62,10 +64,11 @@ require (
github.com/hashicorp/hcl/v2 v2.0.0 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
Expand All @@ -86,6 +89,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand All @@ -94,9 +98,10 @@ require (
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c // indirect
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/api v0.63.0 // indirect
google.golang.org/grpc v1.43.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
Expand Down
Loading
0