-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
024774a
commit 04b97a1
Showing
6 changed files
with
150 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package dashboard | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
// responseWriter is a minimal wrapper for http.ResponseWriter that allows the | ||
// written HTTP status code to be captured for logging. | ||
type responseWriter struct { | ||
http.ResponseWriter | ||
status int | ||
wroteHeader bool | ||
} | ||
|
||
func wrapResponseWriter(w http.ResponseWriter) *responseWriter { | ||
return &responseWriter{ResponseWriter: w} | ||
} | ||
|
||
func (rw *responseWriter) Status() int { | ||
return rw.status | ||
} | ||
|
||
func (rw *responseWriter) WriteHeader(code int) { | ||
if rw.wroteHeader { | ||
return | ||
} | ||
|
||
rw.status = code | ||
rw.ResponseWriter.WriteHeader(code) | ||
rw.wroteHeader = true | ||
} | ||
|
||
// LoggingMiddleware logs the incoming HTTP request & its duration. | ||
func LoggingMiddleware(logger *zap.SugaredLogger) func(http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
logger.Errorw("error handling request", | ||
"error", err, | ||
) | ||
} | ||
}() | ||
|
||
start := time.Now() | ||
wrapped := wrapResponseWriter(w) | ||
next.ServeHTTP(wrapped, r) | ||
logger.Debugw("handled request", | ||
"status", wrapped.status, | ||
"method", r.Method, | ||
"path", r.URL.EscapedPath(), | ||
"duration", time.Since(start), | ||
) | ||
} | ||
|
||
return http.HandlerFunc(fn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,66 @@ | ||
{{define "dashboard"}} | ||
|
||
<h1>LED Control</h1> | ||
|
||
<div class="container-card"> | ||
<div class="card"> | ||
<h1 class="card-heading">Control</h1> | ||
<div class="card"> | ||
<h1 class="card-heading">Control</h1> | ||
|
||
<form action="/control" method="post" id="control"> | ||
<label for="control-color">Color</label> | ||
<input id="control-color" type="color" name="color"> | ||
<form action="/control" method="post" id="control"> | ||
<label for="control-color">Color</label> | ||
<input id="control-color" type="color" name="color" /> | ||
|
||
<label for="control-brightness">Brightness</label> | ||
<input type="range" min="0" max="100" name="brightness"> | ||
<label for="control-brightness">Brightness</label> | ||
<input type="range" min="0" max="100" name="brightness" /> | ||
|
||
<input type="submit" value="set" id="control-submit"> | ||
</form> | ||
</div> | ||
<input type="submit" value="set" id="control-submit" /> | ||
</form> | ||
</div> | ||
|
||
<div class="card"> | ||
<h1 class="card-heading">Demo</h1> | ||
<div class="card"> | ||
<h1 class="card-heading">Demo</h1> | ||
|
||
<form action="/demo" method="post" id="demo"> | ||
<label for="demo-count">Count</label> | ||
<input id="demo-count" type="number" name="count" value="1" min="1" max="10"> | ||
<form action="/demo" method="post" id="demo"> | ||
<label for="demo-count">Count</label> | ||
<input | ||
id="demo-count" | ||
type="number" | ||
name="count" | ||
value="1" | ||
min="1" | ||
max="10" | ||
/> | ||
|
||
<label for="demo-delay">Delay (in ms)</label> | ||
<input id="demo-delay" type="number" name="delay" value="100" min="0" max="10000"> | ||
<label for="demo-delay">Delay (in ms)</label> | ||
<input | ||
id="demo-delay" | ||
type="number" | ||
name="delay" | ||
value="100" | ||
min="0" | ||
max="10000" | ||
/> | ||
|
||
<label for="demo-brightness">Brightness</label> | ||
<input id="demo-brightness" type="range" min="0" max="100" name="brightness"> | ||
<label for="demo-brightness">Brightness</label> | ||
<input | ||
id="demo-brightness" | ||
type="range" | ||
min="0" | ||
max="100" | ||
name="brightness" | ||
/> | ||
|
||
<label for="demo-gradient-steps">Gradient Steps</label> | ||
<input id="demo-gradient-steps" type="number" name="gradient-steps" value="2048" min="10" max="10000"> | ||
<label for="demo-gradient-steps">Gradient Steps</label> | ||
<input | ||
id="demo-gradient-steps" | ||
type="number" | ||
name="gradient-steps" | ||
value="2048" | ||
min="10" | ||
max="10000" | ||
/> | ||
|
||
<input type="submit" value="run" id="demo-submit"> | ||
</form> | ||
</div> | ||
<input type="submit" value="run" id="demo-submit" /> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
{{end}} | ||
{{ end }} |