summaryrefslogtreecommitdiff
path: root/src/hub/src/spreadspace.org/sfive/s5srvWeb.go
blob: 0aad5ba272f19f2baab71d2f923b081ee2b87aa4 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package sfive

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"

	"github.com/zenazn/goji"
	"github.com/zenazn/goji/web"
)

func (srv Server) webHealthz(c web.C, w http.ResponseWriter, r *http.Request) {
	// TODO: do a more sophisticated check
	fmt.Fprintf(w, "%s\n", srv.store.GetStoreId())
}

func (srv Server) webGetSourcesList(c web.C, w http.ResponseWriter, r *http.Request) {
	const resourceName = "sources"
	values, err := srv.store.GetSources()
	if err != nil {
		http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusInternalServerError)
		return
	}
	jsonString, err := json.Marshal(DataContainer{values})
	if err != nil {
		http.Error(w, fmt.Sprintf("failed to marshal %s: %v", resourceName, err), http.StatusInternalServerError)
		return
	}
	fmt.Fprintf(w, "%s", jsonString)
}

func (srv Server) webGetSource(c web.C, w http.ResponseWriter, r *http.Request) {
	const resourceName = "source"
	id, err := strconv.ParseInt(c.URLParams["id"], 10, 64)
	if err != nil {
		http.Error(w, fmt.Sprintf("invalid id: %s: %v", resourceName, err), http.StatusBadRequest)
		return
	}
	value, err := srv.store.GetSource(int(id))
	if err != nil {
		if err == ErrNotFound {
			http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusNotFound)
		} else {
			http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusInternalServerError)
		}
		return
	}
	jsonString, err := json.Marshal(value)
	if err != nil {
		http.Error(w, fmt.Sprintf("failed to marshal %s: %v", resourceName, err), http.StatusInternalServerError)
		return
	}
	fmt.Fprintf(w, "%s", jsonString)
}

func (srv Server) webGetUpdateList(c web.C, w http.ResponseWriter, r *http.Request) {
	const resourceName = "updates"
	values, err := srv.getUpdatesAfterInvoke(-1, 3) // TODO: get start and limit from param
	if err != nil {
		http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusInternalServerError)
		return
	}
	jsonString, err := json.Marshal(DataContainer{values})
	if err != nil {
		http.Error(w, fmt.Sprintf("failed to marshal %s: %v", resourceName, err), http.StatusInternalServerError)
		return
	}
	fmt.Fprintf(w, "%s", jsonString)
}

func (srv Server) webGetUpdate(c web.C, w http.ResponseWriter, r *http.Request) {
	const resourceName = "update"
	id, err := strconv.ParseInt(c.URLParams["id"], 10, 64)
	if err != nil {
		http.Error(w, fmt.Sprintf("invalid id: %s: %v", resourceName, err), http.StatusBadRequest)
		return
	}
	value, err := srv.store.GetUpdate(int(id))
	if err != nil {
		if err == ErrNotFound {
			http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusNotFound)
		} else {
			http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusInternalServerError)
		}
		return
	}
	jsonString, err := json.Marshal(value)
	if err != nil {
		http.Error(w, fmt.Sprintf("failed to marshal %s: %v", resourceName, err), http.StatusInternalServerError)
		return
	}
	fmt.Fprintf(w, "%s", jsonString)
}

func (srv Server) webPostUpdate(c web.C, w http.ResponseWriter, r *http.Request) {
	const resourceName = "update"
	decoder := NewPlainDecoder()

	buffer, err := ioutil.ReadAll(r.Body)
	if err != nil {
		s5l.Printf("web: failed to read post value: %v\n", err)
		http.Error(w, fmt.Sprintf("failed reading : %s: %v", resourceName, err), http.StatusBadRequest)
		return
	}

	container := DataUpdateFullContainer{}
	err = json.Unmarshal(buffer, &container)
	if err == nil {
		token := appendManyToken{
			data:     container.Data,
			response: make(chan bool, 2)}
		defer close(token.response)
		srv.appendManyData <- token
		success := <-token.response
		if !success {
			http.Error(w, "failed to store data", http.StatusInternalServerError)
		}
		return
	}

	// else try single value
	data, err := decoder.Decode(buffer)
	if err != nil {
		s5l.Printf("web: failed to decode: %v\n", err)
		http.Error(w, fmt.Sprintf("failed decoding %s: %v", resourceName, err), http.StatusBadRequest)
		return
	}

	srv.appendData <- data
	// TODO send response channel, wait for OK
}

func (srv Server) webGetLastUpdateId(c web.C, w http.ResponseWriter, r *http.Request) {
	const resourceName = "lastupdateid"
	value, err := srv.store.GetLastUpdateId()
	if err != nil {
		http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusInternalServerError)
		return
	}
	fmt.Fprintf(w, "%d", value)
}

func (srv Server) webGetLastUpdateIdForUuid(c web.C, w http.ResponseWriter, r *http.Request) {
	const resourceName = "lastupdateid"
	id := c.URLParams["id"]
	value, err := srv.store.GetLastUpdateForUuid(id)
	if err != nil {
		http.Error(w, fmt.Sprintf("failed to retrieve %s: %v", resourceName, err), http.StatusInternalServerError)
		return
	}
	fmt.Fprintf(w, "%d", value)
}

func (srv Server) ServeWeb() {
	goji.Get("/healthz", srv.webHealthz)
	goji.Get("/sources", srv.webGetSourcesList)
	goji.Get("/sources/:id", srv.webGetSource)
	goji.Get("/updates", srv.webGetUpdateList)
	goji.Get("/updates/:id", srv.webGetUpdate)
	goji.Post("/updates", srv.webPostUpdate)
	goji.Get("/lastupdate", srv.webGetLastUpdateId)
	goji.Get("/lastupdate/:id", srv.webGetLastUpdateIdForUuid)
	goji.Serve()
}