-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
7 changed files
with
291 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.