-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
76 lines (67 loc) · 1.69 KB
/
Copy pathapi.go
File metadata and controls
76 lines (67 loc) · 1.69 KB
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
66
67
68
69
70
71
72
73
74
75
76
package api
import (
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/lucianthorr/goProject/db"
)
type Config struct {
Hostname string `yaml:"hostname"`
Port int `yaml:"port"`
Timeout time.Duration `yaml:"timeout"`
}
type Client interface {
// set public methods
Run() error
}
func New(cfg *Config, dbCli db.Client) Client {
c := &client{
cfg: cfg,
dbCli: dbCli,
}
c.server = &http.Server{
Addr: fmt.Sprintf("%s:%d", cfg.Hostname, cfg.Port),
ReadTimeout: cfg.Timeout,
WriteTimeout: cfg.Timeout,
}
return c
}
type client struct {
cfg *Config
dbCli db.Client
server *http.Server
}
func (c *client) Run() error {
http.HandleFunc("/", makeGetThing(c.dbCli))
return c.server.ListenAndServe()
}
// The following route just shows how the API uses its DB dependency and then does some post-processing on the result
// It expects something like "/xyz" where "xyz" is an ID that is looked up in the DB
func makeGetThing(dbCli db.Client) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
path := strings.TrimPrefix(req.URL.Path, "/")
tokens := strings.Split(path, "/")
if len(tokens) < 1 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Missing Thing ID in path")
return
}
if len(tokens) > 1 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Expects one Thing ID as path parameter")
return
}
thingID := tokens[0]
result, err := dbCli.GetThing(thingID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
processedResult := result + "_processed"
w.WriteHeader(http.StatusOK)
io.WriteString(w, processedResult)
}
}