-
Notifications
You must be signed in to change notification settings - Fork 0
/
problems.go
65 lines (52 loc) · 1.65 KB
/
problems.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package handlers
import (
"bytes"
"context"
"fmt"
"net/http"
"github.com/go-chi/chi"
"github.com/toddgaunt/bastion/internal/errors"
)
const problemsCtxKey = contextKey("problemID")
// ProblemID extracts the problem ID from the request URL
func ProblemID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
problemID := chi.URLParam(r, "problemID")
ctx := context.WithValue(r.Context(), problemsCtxKey, problemID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// Problems is a request handler that returns an HTTP handler that responds
// to a request with a document describing a particular problem.
func (env Env) Problems(w http.ResponseWriter, r *http.Request) {
const op = "Problems"
fn := func(w http.ResponseWriter, r *http.Request) errors.Problem {
problemID := r.Context().Value(problemsCtxKey).(string)
description := ""
switch problemID {
case "article-not-found":
description = `This article does not exist`
case "not-found":
description = `There was no content available`
case "internal-server-error":
description = `The server experienced an error which was no fault of the client`
default:
return errors.Note{
Op: op,
StatusCode: http.StatusNotFound,
Detail: fmt.Sprintf("Documenation for %s is not available", problemID),
}.Wrap(errors.New("problem not registered"))
}
vars := templateVariables{
Title: problemID,
Description: description,
content: env.Store,
}
buf := &bytes.Buffer{}
problemTemplate.Execute(buf, vars)
w.Write(buf.Bytes())
return nil
}
err := fn(w, r)
handleError(w, err, env.Logger)
}