コード例 #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
ファイル: server.go プロジェクト: krisrang/fancypants
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
ファイル: renderer.go プロジェクト: SHMEDIALIMITED/server
// 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)
}
コード例 #8
0
// 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
ファイル: renderer.go プロジェクト: SHMEDIALIMITED/server
// 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
ファイル: handler.go プロジェクト: frank12268/docker-wicket
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)
}
コード例 #14
0
// 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
ファイル: handler.go プロジェクト: frank12268/docker-wicket
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)
}
コード例 #17
0
// 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
ファイル: server.go プロジェクト: krisrang/fancypants
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
ファイル: handler.go プロジェクト: frank12268/docker-wicket
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)
}