Skip to content

Commit 95ac7ad

Browse files
committed
add static enpoint to serve attachments to the 'static' doc in couch
1 parent 68fb6ea commit 95ac7ad

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ module github.com/byuoitav/scheduler
33
go 1.13
44

55
require (
6+
github.com/byuoitav/central-event-system v0.0.0-20200121172633-64fd9d467249
7+
github.com/byuoitav/common v0.0.0-20191210190714-e9b411b3cc0d
8+
github.com/byuoitav/device-monitoring v0.0.0-20200310211254-94d1f85b41c2
69
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
10+
github.com/fatih/color v1.9.0 // indirect
11+
github.com/gorilla/websocket v1.4.2 // indirect
712
github.com/labstack/echo v3.3.10+incompatible
813
github.com/labstack/gommon v0.3.0 // indirect
14+
github.com/shirou/gopsutil v2.20.3+incompatible // indirect
915
github.com/spf13/pflag v1.0.5
1016
go.uber.org/zap v1.13.0
1117
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 // indirect

go.sum

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/byuoitav/central-event-system v0.0.0-20200121172633-64fd9d467249 h1:LffuZk+Xft6AnDURHGAexbT4wrMo15qt/LpLLDq7Tq4=
4+
github.com/byuoitav/central-event-system v0.0.0-20200121172633-64fd9d467249/go.mod h1:+KCvx75wISCCkCrgIWRdlJxRXlA2yxQvR4HHgU/TdpU=
5+
github.com/byuoitav/common v0.0.0-20191210190714-e9b411b3cc0d h1:F3/vBL2hw+zjCm78sWss6eCozj5IopBzN2bIHvKj2hw=
6+
github.com/byuoitav/common v0.0.0-20191210190714-e9b411b3cc0d/go.mod h1:YTDTFEmez7HU3oyCIWjU3RfQ/P6v24LEzH5YUebph7I=
7+
github.com/byuoitav/device-monitoring v0.0.0-20200310211254-94d1f85b41c2 h1:3rJ0gHP8URAfVLSL/whmU3ak3YX5B5SZI00R2hfW9kw=
8+
github.com/byuoitav/device-monitoring v0.0.0-20200310211254-94d1f85b41c2/go.mod h1:n9PY4PhVKyrO15/M9rPsHSQFo/62Zsanvp0ZT6D2fbg=
39
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
410
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
511
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
612
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
713
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
14+
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
15+
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
816
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
17+
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
18+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
919
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
1020
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1121
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -18,14 +28,20 @@ github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0
1828
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
1929
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
2030
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
31+
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
32+
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
2133
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
2234
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
2335
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
36+
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
37+
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
2438
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
2539
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
2640
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2741
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2842
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
43+
github.com/shirou/gopsutil v2.20.3+incompatible h1:0JVooMPsT7A7HqEYdydp/OfjSOYSjhXV7w1hkKj/NPQ=
44+
github.com/shirou/gopsutil v2.20.3+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
2945
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
3046
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
3147
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -62,6 +78,8 @@ golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5h
6278
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6379
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
6480
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
81+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
82+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6583
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
6684
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
6785
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=

handlers/handlers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ func CreateEvent(c echo.Context) error {
7171
return c.JSON(http.StatusOK, fmt.Sprintf("Successfully created %q in %q", event.Title, roomID))
7272
}
7373

74+
func GetStaticElements(c echo.Context) error {
75+
docName := c.Param("doc")
76+
77+
file, fileType, err := schedule.GetStatic(c.Request().Context(), docName)
78+
if err != nil {
79+
log.P.Error("Unable to get static element", zap.Error(err))
80+
return c.String(http.StatusInternalServerError, fmt.Sprintf("unable to get static element"))
81+
}
82+
defer file.Close()
83+
84+
return c.Stream(http.StatusOK, fileType, file)
85+
}
86+
7487
func connectionCheck() {
7588
id := localsystem.MustSystemID()
7689
deviceInfo := events.GenerateBasicDeviceInfo(id)

schedule/static.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package schedule
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
"io/ioutil"
10+
"net/http"
11+
"os"
12+
13+
"github.com/byuoitav/scheduler/log"
14+
"go.uber.org/zap"
15+
)
16+
17+
type staticDoc struct {
18+
ID string `json:"_id"`
19+
Rev string `json:"_rev"`
20+
21+
Attachments map[string]fileAttachment `json:"_attachments"`
22+
}
23+
24+
type fileAttachment struct {
25+
ContentType string `json:"content_type"`
26+
RevPos int `json:"revpos"`
27+
Digest string `json:"digest"`
28+
Length int `json:"length"`
29+
Stub bool `json:"stub"`
30+
}
31+
32+
func GetStatic(ctx context.Context, docName string) (io.ReadCloser, string, error) {
33+
log.P.Info("Getting static document", zap.String("docName", docName))
34+
35+
//get 'static' document
36+
var static staticDoc
37+
url := fmt.Sprintf("%s/%s/%s", os.Getenv("DB_ADDRESS"), "schedulers", "static")
38+
39+
body, err := makeRequest(ctx, "GET", url, "", nil)
40+
if err != nil {
41+
return nil, "", err
42+
}
43+
defer body.Close()
44+
45+
b, err := ioutil.ReadAll(body)
46+
if err != nil {
47+
log.P.Error("failed to read http response body", zap.Error(err))
48+
return nil, "", err
49+
}
50+
51+
err = json.Unmarshal(b, &static)
52+
if err != nil {
53+
log.P.Error("failure to unmarshal resp body", zap.String("body", string(b)), zap.Error(err))
54+
return nil, "", err
55+
}
56+
57+
//get file type
58+
fileType := static.Attachments[docName].ContentType
59+
if fileType == "" {
60+
return nil, "", fmt.Errorf("document (%s) does not exist", docName)
61+
}
62+
63+
//get actual file
64+
url = fmt.Sprintf("%s/%s/%s/%s", os.Getenv("DB_ADDRESS"), "schedulers", "static", docName)
65+
file, err := makeRequest(ctx, "GET", url, "", nil)
66+
67+
//return stream
68+
return file, fileType, nil
69+
}
70+
71+
func makeRequest(ctx context.Context, method, url, contentType string, body []byte) (io.ReadCloser, error) {
72+
log.P.Info("making http request", zap.String("dest-url", url))
73+
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(body))
74+
if err != nil {
75+
log.P.Error("failed to create new http request", zap.String("url", url), zap.Error(err))
76+
return nil, err
77+
}
78+
79+
req.SetBasicAuth(os.Getenv("DB_USERNAME"), os.Getenv("DB_PASSWORD"))
80+
if len(contentType) > 0 {
81+
req.Header.Add("Content-Type", contentType)
82+
}
83+
84+
resp, err := http.DefaultClient.Do(req)
85+
if err != nil {
86+
log.P.Error("failed to make http request", zap.String("url", url), zap.Error(err))
87+
return nil, err
88+
}
89+
90+
if resp.StatusCode/100 != 2 {
91+
log.P.Error("bad response code", zap.Int("resp code", resp.StatusCode))
92+
return nil, fmt.Errorf("bad response code - %v", resp.StatusCode)
93+
}
94+
95+
return resp.Body, nil
96+
}

server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ func main() {
6161
// get config for the room
6262
e.GET("/config", handlers.GetConfig)
6363

64+
// get static elements
65+
e.GET("/static/:doc", handlers.GetStaticElements)
66+
6467
// handle load balancer status check
6568
e.GET("/status", func(c echo.Context) error {
6669
return c.String(http.StatusOK, "healthy")

0 commit comments

Comments
 (0)