Skip to content
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

Writing middleware documentation is out of date #47

Open
Kingdutch opened this issue Aug 27, 2020 · 0 comments
Open

Writing middleware documentation is out of date #47

Kingdutch opened this issue Aug 27, 2020 · 0 comments

Comments

@Kingdutch
Copy link

https://reason-native-web.github.io/docs/morph/writing-middlewares makes a reference to Morph.Method.to_string but this doesn't actually seem to be in the library.

With some digging I found that the method information from Piaf is actually hidden under request.request.meth so maybe we want to somehow map this to request.method directly (changing Morph.Request.t).

I came up with the following which appears to work.

let method_to_string : Piaf.Method.t => string = fun
  | `GET => "GET"
  | `POST => "POST"
  | `PUT => "PUT"
  | `DELETE => "DELETE"
  | `HEAD => "HEAD"
  | `OPTIONS => "OPTIONS"
  | `TRACE => "TRACE"
  | `CONNECT => "CONNECT"
  | `Other(s) => {j|UNKNOWN($s)|j}
  ;

/**
 * Creates a new logger middleware.
 *
 * The output of this function can be passed to the middlewares
 * argument of `Morph.start`.
 */
let make = () => {
  (service) => (request: Morph.Request.t) => {
    open Lwt.Infix;
    let start_request = Mtime_clock.elapsed();
    service(request)
    >|= (
      response => {
        let end_request = Mtime_clock.elapsed();
        Logs.info(m =>
          m(
            "http: %s request to %s finished in %fms",
            request.request.meth |> method_to_string,
            request.request.target,
            Mtime.Span.abs_diff(start_request, end_request)
            |> Mtime.Span.to_ms,
          )
        );
        response;
      }
    );
  };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant