Exemplo n.º 1
0
func SensorsHandler(db *models.Mysql) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			http.Error(w, http.StatusText(405), 405)
			return
		}

		sensors, err := db.GetSensors()
		if err != nil {
			http.Error(w, http.StatusText(500), 500)
			return
		}

		for _, v := range sensors {
			fmt.Fprintf(w, "%s\n", v)
		}
	})
}
Exemplo n.º 2
0
func RootHandler(tpl *template.Template, db *models.Mysql) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		log.Println("Request:", r)
		if tpl == nil {
			http.Error(w, http.StatusText(404), 404)
			return
		}

		sensors, err := db.GetSensors()
		if err != nil {
			http.Error(w, http.StatusText(500), 500)
			return
		}

		err = tpl.Execute(w, sensors)
		if err != nil {
			fmt.Println(err)
			http.Error(w, http.StatusText(404), 404)
			return
		}
	})
}