1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 module pop_middlewares
23 # Initialize session in request if non existent.
25 # Should be called before any use of the session.
29 redef fun all
(req
, res
) do if req
.session
== null then req
.session
= new Session
32 # Initialize a clock for the resquest.
34 # Can be used to compute the time passed to respond that request.
38 redef fun all
(req
, res
) do req
.clock
= new Clock
41 # Display log info about request processing.
53 # Request status are always logged, whatever the logger level is.
54 var level
= 4 is writable
56 # Do we want colors in the console output?
59 redef fun all
(req
, res
) do
62 log
"{req.method} {req.uri} {status(res)} ({clock.total}s)"
64 log
"{req.method} {req.uri} {status(res)}"
68 # Colorize the request status.
69 private fun status
(res
: HttpResponse): String do
70 if no_colors
then return res
.status_code
.to_s
71 return res
.color_status
74 # Display a `message` with `level`.
76 # Message will only be displayed if `level <= self.level`.
77 # Colors will be used depending on `colors`.
79 # Use `0` for no coloration.
80 private fun display
(level
: Int, message
: String) do
81 if level
> self.level
then return
86 if level
== 0 then print message
87 if level
== 1 then print message
.red
88 if level
== 2 then print message
.yellow
89 if level
== 3 then print message
.blue
90 if level
== 4 then print message
.gray
93 # Display a message wathever the `level`
94 fun log
(message
: String) do display
(0, message
)
96 # Display a red error `message`.
97 fun error
(message
: String) do display
(1, "[ERROR] {message}")
99 # Display a yellow warning `message`.
100 fun warning
(message
: String) do display
(2, "[WARN] {message}")
102 # Display a blue info `message`.
103 fun info
(message
: String) do display
(3, "[INFO] {message}")
105 # Display a gray debug `message`.
106 fun debug
(message
: String) do display
(4, "[DEBUG] {message}")
109 redef class HttpRequest
110 # Time that request was received by the Popcorn app.
111 var clock
: nullable Clock = null
114 redef class HttpResponse
115 # Return `self` status colored for console.
116 fun color_status
: String do
117 if status_code
>= 100 and status_code
< 200 then return status_code
.to_s
.gray
118 if status_code
>= 200 and status_code
< 300 then return status_code
.to_s
.green
119 if status_code
>= 300 and status_code
< 400 then return status_code
.to_s
.blue
120 if status_code
>= 400 and status_code
< 500 then return status_code
.to_s
.yellow
121 if status_code
>= 500 and status_code
< 600 then return status_code
.to_s
.red
122 return status_code
.to_s