-
Notifications
You must be signed in to change notification settings - Fork 51
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
feat: Playground #1575
feat: Playground #1575
Conversation
…ma list api response
Codecov ReportPatch coverage:
@@ Coverage Diff @@
## develop #1575 +/- ##
===========================================
- Coverage 74.92% 74.90% -0.02%
===========================================
Files 203 203
Lines 21072 21072
===========================================
- Hits 15787 15782 -5
- Misses 4207 4210 +3
- Partials 1078 1080 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 4 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
api/http/router.go
Outdated
h.Mux = chi.NewRouter() | ||
var router = chi.NewRouter() | ||
|
||
// setup CORS | ||
if len(h.options.allowedOrigins) != 0 { | ||
h.Use(cors.Handler(cors.Options{ | ||
AllowedOrigins: h.options.allowedOrigins, | ||
AllowedMethods: []string{"GET", "POST", "OPTIONS"}, | ||
AllowedHeaders: []string{"Content-Type"}, | ||
MaxAge: 300, | ||
})) | ||
} | ||
|
||
// setup logger middleware | ||
h.Use(loggerMiddleware) | ||
|
||
// define routes | ||
h.Get(RootPath, h.handle(rootHandler)) | ||
h.Get(PingPath, h.handle(pingHandler)) | ||
h.Get(DumpPath, h.handle(dumpHandler)) | ||
h.Get(BlocksPath+"/{cid}", h.handle(getBlockHandler)) | ||
h.Get(GraphQLPath, h.handle(execGQLHandler)) | ||
h.Post(GraphQLPath, h.handle(execGQLHandler)) | ||
h.Get(SchemaPath, h.handle(listSchemaHandler)) | ||
h.Post(SchemaPath, h.handle(loadSchemaHandler)) | ||
h.Patch(SchemaPath, h.handle(patchSchemaHandler)) | ||
h.Post(IndexPath, h.handle(createIndexHandler)) | ||
h.Delete(IndexPath, h.handle(dropIndexHandler)) | ||
h.Get(IndexPath, h.handle(listIndexHandler)) | ||
h.Get(PeerIDPath, h.handle(peerIDHandler)) | ||
|
||
return h | ||
func init() { | ||
router.Get(RootPath, rootHandler) | ||
router.Get(PingPath, pingHandler) | ||
router.Get(DumpPath, dumpHandler) | ||
router.Get(BlocksPath+"/{cid}", getBlockHandler) | ||
router.Get(GraphQLPath, execGQLHandler) | ||
router.Post(GraphQLPath, execGQLHandler) | ||
router.Get(SchemaPath, listSchemaHandler) | ||
router.Post(SchemaPath, loadSchemaHandler) | ||
router.Patch(SchemaPath, patchSchemaHandler) | ||
router.Post(IndexPath, createIndexHandler) | ||
router.Delete(IndexPath, dropIndexHandler) | ||
router.Get(IndexPath, listIndexHandler) | ||
router.Get(PeerIDPath, peerIDHandler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: I'm not a big fan of a global var router and setting the routes on package init. I think it would be better for future maintenance (and other beneficial reasons) to keep this under the setRoutes
function. Although you can simply return the router.
That being said, I like how you moved the handle method to a middleware and that it avoids wrapping the handlers in the handle
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
playground.go:init
can also be moved inside the setRoutes
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to keep the setRoutes
method but I couldn't think of another way to do build time route registration. I'm using the playground.go:init
function because the playground can be enabled/disabled using the build tag. I'm happy to move everything back if we can think of an alternative that works with build tags.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could create a global withPlayground variable in router.go
and set in to true in the init inside playground.go
. You can then have a if
block in the setRoutes
function to act accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! My brain was stuck on that for a while. I think I came up with a good solution. The route registration is moved back to setRoutes
and the playground handler is set at build time via init
. I made sure it works when not including the playground as well.
@@ -0,0 +1,18 @@ | |||
//go:build playground |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: Should add a copyright header (I am also wondering why the linter didn't complain about the copyright header)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe due to the build tag here? I'm not exactly sure either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job with this Keenan! I actually played around with the playground this morning and it's working quite well. Specially for a first iteration of it.
## Relevant issue(s) Resolves sourcenetwork#884 ## Description This PR adds a web based playground to DefraDB. To use the embedded playground run the following. ``` # build playground make deps:playground # enable embedded playground export BUILD_TAGS=playground make start # or make build ``` The playground is served from the same port as the API: `http://localhost:9181` - [x] View & query DefraDB types - [x] Create & patch schemas - [x] Relational field support - [x] Embed playground in binary ![localhost_5173_](https://github.com/sourcenetwork/defradb/assets/1862560/8ad8bd53-eadf-40bc-9204-6c821b0b13d2) ## Tasks - [x] I made sure the code is well commented, particularly hard-to-understand areas. - [x] I made sure the repository-held documentation is changed accordingly. - [x] I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in [tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)). - [x] I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ... ## How has this been tested? Testing was done manually. Specify the platform(s) on which this was tested: - MacOS
Relevant issue(s)
Resolves #884
Description
This PR adds a web based playground to DefraDB.
To use the embedded playground run the following.
The playground is served from the same port as the API:
http://localhost:9181
Tasks
How has this been tested?
Testing was done manually.
Specify the platform(s) on which this was tested: