staticbackendhq / core

Backend server API handling user mgmt, database, storage and real-time component
https://staticbackend.com
MIT License
682 stars 66 forks source link

[cli] Add a tail command to follow server logs #85

Open dstpierre opened 1 year ago

dstpierre commented 1 year ago

Could be helpful to have a way to display HTTP requests info.

In the CLI program we could add a command line parameter to the server command, say --tail which would display the HTTP requests being handled by the server.

In the core package, we currently do not have a log middleware. We'd need one and use the logger to ourput nice output, but only if we used the --tail option.

This means that in the config we would need to add a new option to indicate if we want to display the server log or not.

The log middleware would be added to all middleware chains only if that config flag is true.

What I mean by middleware chains can be found in server.go:117:

stdAuth := []middleware.Middleware{
  middleware.Cors(),
  middleware.WithDB(backend.DB, backend.Cache, getStripePortalURL),
  .// ...
}

Since they are this function receive the config, the log middleware could accept a flag to be verbose or not, ex:

stdAuth := []middleware.Middleware{
  middleware.Log(config.TailRequests),
  middleware.Cors(),
  middleware.WithDB(backend.DB, backend.Cache, getStripePortalURL),
  // ...
}

Assuming the config flag would be called TailRequests and the log middleware Log.

If TailRequests is false the middleware simply do nothing.

mauriceLC92 commented 1 year ago

Hey @dstpierre I am keen to take this task on. Could you please assign it to me and I will try make a start this weekend?

dstpierre commented 1 year ago

Thanks @mauriceLC92 highly appreciated. Here's a couple of tips since you will need to modify this project core and the cli

The cli import the core project, to make this work you'll need to replace the package in the cli to say you want to use your local version:

  1. Clone both projects side-by-side, ex: ~/src/staticbackend/ have the core and cli there.
  2. In the cli go.mod add replace github.com/staticbackendhq/core => ../core

From there you should be able to make changes in the core to create the log middleware and the changes to config and build the cli. Not optimal, still trying to figure out the best workflow for this.

I'm using make build to build the cli and from there you will have a binary called backend

$ ./backend server --tail-log

For example testing this new flag.

Do not hesitate if you have any questions. `

mauriceLC92 commented 1 year ago

Perfect, thanks for the extra tips! Will shout if I have any questions