add request logging
This commit is contained in:
parent
c31ed4ed27
commit
4e73711018
3 changed files with 22 additions and 1 deletions
|
@ -8,6 +8,7 @@ Configuration is done using environment variables.
|
|||
- `WOLBODGE_DEVICE_TYPE`: Device type to pick, defaults to `test`.
|
||||
- `test`: A dummy device, used for testing.
|
||||
- `command`: Runs a subprocess to aquire the info. See the example below for more info.
|
||||
- `WOLBODGE_LOG_REQUESTS`: Should HTTP requests be logged, defaults to `true`.
|
||||
|
||||
### Example: Raspberry pi using the command device
|
||||
- `WOLBODGE_DEVICE_TYPE=command`
|
||||
|
|
9
main.go
9
main.go
|
@ -20,7 +20,9 @@ func Run() error {
|
|||
logger := log.New(os.Stdout, "", log.LstdFlags)
|
||||
|
||||
var address string
|
||||
var logRequests bool
|
||||
Env("WOLBODGE_ADDRESS", &address, ":3000", logger)
|
||||
Env("WOLBODGE_LOG_REQUESTS", &logRequests, "true", logger)
|
||||
|
||||
tmpl := template.New("")
|
||||
_, err := tmpl.Parse(embed)
|
||||
|
@ -53,8 +55,13 @@ func Run() error {
|
|||
exit := make(chan bool)
|
||||
go devices.KeepDeviceInSync(logger, device, storage.HasSessions, exit)
|
||||
|
||||
var handler http.Handler = mux
|
||||
if logRequests {
|
||||
handler = routes.Logger(mux, logger)
|
||||
}
|
||||
|
||||
logger.Printf("Listening on %s", address)
|
||||
err = http.ListenAndServe(address, mux)
|
||||
err = http.ListenAndServe(address, handler)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to listen: %w", err)
|
||||
}
|
||||
|
|
13
routes/logger.go
Normal file
13
routes/logger.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Logger(handler http.Handler, logger *log.Logger) http.HandlerFunc {
|
||||
return func(res http.ResponseWriter, req *http.Request) {
|
||||
logger.Printf("%-30s %-10s %s", req.RemoteAddr, req.Method, req.URL.Path)
|
||||
handler.ServeHTTP(res, req)
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue