-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebbed.go
More file actions
46 lines (37 loc) · 917 Bytes
/
webbed.go
File metadata and controls
46 lines (37 loc) · 917 Bytes
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
/* Copyright 2021 Kilobit Labs Inc. */
// Simple utilities for building Web Apps in Golang.
//
package webbed
import _ "fmt"
import _ "errors"
import "context"
import "os"
import "net/http"
// Return an error on the calling HTTP connection.
//
// code is a http.Status code.
func ServeError(w http.ResponseWriter, code int, err error) {
http.Error(w, err.Error(), code)
}
// Retrieve a string value from a context.
//
// Missing values or those of a non-string type will retrun "".
//
func StringFromCtx(ctx context.Context, key interface{}) string {
str, ok := ctx.Value(key).(string)
if !ok {
str = ""
}
return str
}
// Convert the environment map to a context.
//
func LoadCtxFromEnv(ctx context.Context, keymap map[string]interface{}) context.Context {
for ekey, ckey := range keymap {
value, ok := os.LookupEnv(ekey)
if ok {
ctx = context.WithValue(ctx, ckey, value)
}
}
return ctx
}