예제 #1
0
// Get counter events in a optionally given time range
// Query parameters: start,end
func (c *Context) CounterByCodeEventsHandler(rw web.ResponseWriter, req *web.Request) {

	code := parseStringPathParameter(req, "code")
	var counter orm.Counter
	db.Where(&orm.Counter{Code: code}).First(&counter)

	if counter.ID == 0 {
		rw.WriteHeader(http.StatusNotFound)
		rw.Write([]byte("Counter not found"))
		return
	}

	start, err := c.parseUintQueryParameter(rw, "start")
	if err != nil {
		return
	}

	end, err := c.parseUintQueryParameter(rw, "end")
	if err != nil {
		return
	}

	var counterEvents []orm.CounterEvent
	orm.GetOrderedWindowedQuery(db, "counter_id", counter.ID, start, end).Find(&counterEvents)

	lastEvent := orm.NewLastCounterEvent(counter)
	if lastEvent.Timestamp > end {
		counterEvents = append(counterEvents, lastEvent)
	}

	marshal(rw, counterEvents)
}
예제 #2
0
파일: server.go 프로젝트: esantoro/heyfyi
func ReturnJSON(rw web.ResponseWriter, object interface{}) {
	j, err := json.MarshalIndent(object, "", "\t")
	if err != nil {
		http.Error(rw, "500: "+err.Error(), http.StatusInternalServerError)
		return
	}
	rw.Write(j)
}
예제 #3
0
func (this *RequestHandler) getServerConfiguration(response web.ResponseWriter, request *web.Request) error {
	json, err := json.Marshal(this.ServerConfiguration)
	if err != nil {
		return err
	}
	response.Write(json)
	return nil
}
예제 #4
0
func (c *Context) RetrieveStats(rw web.ResponseWriter, req *web.Request) {
	rw.Header().Set("Content-Type", "application/json")

	stats := Stats.Data()

	b, _ := json.Marshal(stats)

	rw.Write(b)
}
예제 #5
0
// Format Error to JSON with moreInfo link
func (c *Renderer) RenderError(rw web.ResponseWriter, err error, info string, status int) {
	js, err := json.MarshalIndent(&JSONError{err.Error(), info}, "", "    ")
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		log.Error("RenderError failed to marshall JSONError", err)
		return
	}
	header := rw.Header()
	header.Set("Content-Type", "application/json")
	rw.WriteHeader(status)
	rw.Write(js)
}
예제 #6
0
파일: main.go 프로젝트: ae6rt/goref
func (c *WidgetContext) MMUInfo(rw web.ResponseWriter, req *web.Request) {
	build := mmu.Build{Name: appName, Version: appVersion}
	git := mmu.Git{Branch: buildCommitBranch, Commit: mmu.Commit{ID: buildCommit, Time: buildCommitTime}}
	mgtEndpoints := mmu.ManagementEndpoints{Info: "/internal/info", Health: "/internal/health"}
	serviceDependencies := []mmu.ServiceDependency{mmu.ServiceDependency{ServiceID: "auth", Version: "2"}}
	info := mmu.Info{Build: build, Git: git, ManagementEndpoints: mgtEndpoints, ServiceDependencies: serviceDependencies, SDK: buildGoSDK}
	if data, err := json.Marshal(&info); err != nil {
		rw.WriteHeader(500)
		return
	} else {
		rw.Write(data)
	}
}
예제 #7
0
func (c *Context) QueryVarsMiddleware(rw web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {

	values, err := parseQueryParams(rw, r)
	if err != nil {
		log.Println(err)
		rw.WriteHeader(http.StatusBadRequest)
		rw.Write([]byte("Malformed URL"))
		return
	}

	c.values = &values
	next(rw, r)
}
// Get specific thermometer by code
func (c *Context) ThermometerByCodeHandler(rw web.ResponseWriter, req *web.Request) {

	code := parseStringPathParameter(req, "code")
	var thermometer orm.Thermometer
	db.Where(&orm.Thermometer{Code: code}).First(&thermometer)

	if thermometer.ID == 0 {
		rw.WriteHeader(http.StatusNotFound)
		rw.Write([]byte("Thermometer not found"))
		return
	}

	marshal(rw, thermometer)
}
예제 #9
0
// Renders from struct to JSON
func (a *Renderer) Render(rw web.ResponseWriter, model interface{}, status int) {
	jsonString, err := json.MarshalIndent(&model, "", "    ")
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		log.Error("Render failed to marshall model", err)
		return
	}
	header := rw.Header()
	header.Set("Content-Type", "application/json")
	header.Set("Access-Control-Allow-Origin", "*")
	header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
	header.Set("Access-Control-Allow-Headers", "Authorization")
	rw.WriteHeader(status)
	rw.Write(jsonString)
}
예제 #10
0
func (this *RequestHandler) getJobStatus(response web.ResponseWriter, request *web.Request) error {
	payload, err := GetStatusRequestPayloadFromRequest(request)
	if err != nil {
		return err
	}
	status, err := this.Repository.GetJobStatus(payload.JobIds)
	if err != nil {
		return err
	}
	json, err := json.Marshal(status)
	if err != nil {
		return err
	}
	response.Write(json)
	return nil
}
예제 #11
0
func marshal(rw web.ResponseWriter, v interface{}) {

	buffer, err := json.Marshal(v)
	if err != nil {
		log.Println("Error marshalling JSON")
		rw.WriteHeader(http.StatusInternalServerError)
		return
	}

	rw.Header().Set("Content-Type", "application/json; charset=utf-8")

	if _, err = rw.Write(buffer); err != nil {
		log.Println("Error writing JSON to buffer")
		rw.WriteHeader(http.StatusInternalServerError)
	}

}
예제 #12
0
func (c *Context) parseUintQueryParameter(rw web.ResponseWriter, name string) (ret uint64, err error) {

	s := c.values.Get(name)
	if s == "" {
		return 0, nil
	}

	ret, err = parseUintFromString(s)
	if err != nil {
		log.Println("Error parsing uint64 " + name + ": " + s)
		log.Println(err)
		rw.WriteHeader(http.StatusBadRequest)
		rw.Write([]byte("Malformed parameter " + name))
	}

	return
}
예제 #13
0
func (c *context) getImages(rw web.ResponseWriter, req *web.Request) {

	rw.Header().Set("Content-Type", "application/json")

	m, err := runningContext.Index.GetIndexImages(c.namespace, c.repo)

	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}

	b, err := json.Marshal(m)

	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}

	rw.Write(b)
}
// Get thermometer readings in a optionally given time range
// Query parameters: start,end
func (c *Context) ThermometerByCodeGetReadingsHandler(rw web.ResponseWriter, req *web.Request) {

	code := parseStringPathParameter(req, "code")
	var thermometer orm.Thermometer
	db.Where(&orm.Thermometer{Code: code}).First(&thermometer)

	if thermometer.ID == 0 {
		rw.WriteHeader(http.StatusNotFound)
		rw.Write([]byte("Thermometer not found"))
		return
	}

	start, err := c.parseUintQueryParameter(rw, "start")
	if err != nil {
		return
	}

	end, err := c.parseUintQueryParameter(rw, "end")
	if err != nil {
		return
	}

	var thermometerReadings []orm.ThermometerReading
	orm.GetOrderedWindowedQuery(db, "thermometer_id", thermometer.ID, start, end).Find(&thermometerReadings)

	startReading := orm.ThermometerReading{
		Reading:       thermometerReadings[0].Reading,
		Timestamp:     start,
		ThermometerID: thermometer.ID,
	}

	endReading := orm.ThermometerReading{
		Reading:       thermometerReadings[len(thermometerReadings)-1].Reading,
		Timestamp:     end,
		ThermometerID: thermometer.ID,
	}

	thermometerReadings = append([]orm.ThermometerReading{startReading}, thermometerReadings...)
	thermometerReadings = append(thermometerReadings, endReading)

	marshal(rw, thermometerReadings)
}
예제 #15
0
func (c *context) writeToken(rw web.ResponseWriter, req *web.Request) {

	token, err := runningContext.TokenAuth.CreateToken(&c.authReq)

	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}

	rw.Header().Set("Content-Type", "application/json")

	result, err := json.Marshal(&map[string]string{"token": token})

	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}

	rw.Write(result)
}
예제 #16
0
// Tick counter by code
func (c *Context) CounterByCodeTickHandler(rw web.ResponseWriter, req *web.Request) {

	code := parseStringPathParameter(req, "code")
	var counter orm.Counter
	db.Where(&orm.Counter{Code: code}).First(&counter)

	if counter.ID == 0 {
		rw.WriteHeader(http.StatusNotFound)
		rw.Write([]byte("Counter not found"))
		return
	}

	counterEvent := orm.NewTickCounterEvent(counter)
	db.Create(&counterEvent)

	counter.Reading = counterEvent.Reading
	counter.LastTick = counterEvent.Timestamp
	db.Save(counter)

	marshal(rw, counterEvent)
}
// Add thermometer reading by code
func (c *Context) ThermometerByCodeAddReadingHandler(rw web.ResponseWriter, req *web.Request) {

	code := parseStringPathParameter(req, "code")
	var thermometer orm.Thermometer
	db.Where(&orm.Thermometer{Code: code}).First(&thermometer)

	if thermometer.ID == 0 {
		rw.WriteHeader(http.StatusNotFound)
		rw.Write([]byte("Thermometer not found"))
		return
	}

	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Could not read body"))
		return
	}

	reading, err := strconv.Atoi(string(body))
	if err != nil {
		rw.WriteHeader(http.StatusBadRequest)
		rw.Write([]byte("Could not parse reading"))
		return
	}

	thermometerReading := orm.NewThermometerReading(thermometer, uint64(reading))
	db.Create(&thermometerReading)

	thermometer.Reading = uint64(reading)
	thermometer.LastReading = thermometerReading.Timestamp
	db.Save(thermometer)

	marshal(rw, thermometerReading)
}
예제 #18
0
// Correct counter by code
func (c *Context) CounterByCodeCorrectHandler(rw web.ResponseWriter, req *web.Request) {

	code := parseStringPathParameter(req, "code")
	var counter orm.Counter
	db.Where(&orm.Counter{Code: code}).First(&counter)

	if counter.ID == 0 {
		rw.WriteHeader(http.StatusNotFound)
		rw.Write([]byte("Counter not found"))
		return
	}

	hah, err := ioutil.ReadAll(req.Body)
	fmt.Println(hah)
	if err != nil {
		fmt.Println(err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error reading body"))
		return
	}

	newReading, err := parseUintFromString(string(hah))
	if err != nil {
		fmt.Println(err)
		rw.WriteHeader(http.StatusBadRequest)
		rw.Write([]byte("Malformed value"))
		return
	}

	delta := int64(newReading) - int64(counter.Reading)
	counter.Reading = newReading
	db.Save(counter)

	counterEvent := orm.NewAbsCorrCounterEvent(counter, delta)

	db.Create(&counterEvent)
	marshal(rw, counterEvent)
}
예제 #19
0
func (c *Context) Root(rw web.ResponseWriter, req *web.Request) {
	rw.Header().Set("Content-Type", "application/json")
	rw.Write([]byte("{\"hello\": \"world\"}"))
}
예제 #20
0
func gocraftWebHandler(w web.ResponseWriter, r *web.Request) {
	if sleepTime > 0 {
		time.Sleep(sleepTimeDuration)
	}
	w.Write(message)
}
예제 #21
0
func (c *context) sayAccess(rw web.ResponseWriter, req *web.Request) {
	rw.Write([]byte(`{"access": true}`))
}
예제 #22
0
파일: context.go 프로젝트: ahjdzx/dis
func (c *Context) OutputJson(rw web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) {
	next(rw, req)
	rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
	resultJson, _ := json.Marshal(c.response)
	rw.Write(resultJson)
}