-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9ac7ace
feat: Add v1 schema types
kylecarbs 3b1d1da
Add license table
kylecarbs 1da0e31
feat: Add Coder Daemon to serve the API
kylecarbs f2fc0fe
Merge branch 'main' into coderd
kylecarbs d83db68
Fix initial migration
kylecarbs f8140b4
Move to /api/v2
kylecarbs 7616cfb
Merge branch 'main' into coderd
kylecarbs 028a721
Increase peer disconnectedTimeout to reduce flakes on slow machines
kylecarbs 8363efe
Reduce timeout again
kylecarbs 8aef6ec
Fix version for pion/ice
kylecarbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,57 @@ | ||
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(), | ||
}) | ||
mux := http.NewServeMux() | ||
mux.Handle("/api/v1", handler) | ||
|
||
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, mux) | ||
}() | ||
|
||
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 hidden or 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 hidden or 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/v1", 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.