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
This commit is contained in:
Kyle Carberry
2022-01-13 16:55:28 -06:00
committed by GitHub
parent 4308f169d6
commit afc2fa3b62
7 changed files with 291 additions and 25 deletions

55
coderd/cmd/root.go Normal file
View File

@ -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
coderd/cmd/root_test.go Normal file
View File

@ -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)
}