Commit f7be2f25 authored by Silke Hofstra's avatar Silke Hofstra

Move config and handlers to separate files

parent d8a29ab2
Pipeline #22455 passed with stages
in 1 minute and 1 second
package main
import (
"io/ioutil"
"git.snt.utwente.nl/dingen/cloudburst/influxdb"
"gopkg.in/yaml.v2"
)
// Config is used for the main configuration
type Config struct {
Address string
Buienradar BuienradarConfig
InfluxDB influxdb.ClientConfig
}
// loadConfig loads configuration
func loadConfig(path string) (config *Config, err error) {
config = new(Config)
// Get configuration data
data, err := ioutil.ReadFile(path)
if err != nil {
return config, err
}
// Parse configuration
err = yaml.Unmarshal(data, config)
if err != nil {
return
}
// Set default address
if config.Address == "" {
config.Address = ":7136"
}
return
}
package main
import (
"encoding/json"
"net/http"
log "github.com/sirupsen/logrus"
)
// pointsHandler writes a point slice as JSON
func pointsHandler(w http.ResponseWriter, name string) {
pts, err := influxClient.GetLastMeasurements(name)
if err != nil {
log.Printf("Error retrieving measurements: %s", err)
w.WriteHeader(500)
return
}
log.Debugf("Points for %s: %+v", name, pts)
data, err := json.Marshal(pts)
if err != nil {
log.Printf("Error encoding measurements: %s", err)
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(data)
}
// measurementsHandler writes the temperatures as an HTTP response
func measurementsHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("%s %s, client %s", r.Method, r.URL, r.RemoteAddr)
pointsHandler(w, "environmental")
}
// measurementsHandler writes the temperatures as an HTTP response
func weatherHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("%s %s, client %s", r.Method, r.URL, r.RemoteAddr)
pointsHandler(w, "weatherstation")
}
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"git.snt.utwente.nl/dingen/cloudburst/influxdb"
log "github.com/sirupsen/logrus"
)
var (
influxClient *influxdb.Client
)
// Config is used for the main configuration
type Config struct {
Address string
Buienradar BuienradarConfig
InfluxDB influxdb.ClientConfig
}
// loadConfig loads configuration
func loadConfig(path string) (config *Config, err error) {
config = new(Config)
// Get configuration data
data, err := ioutil.ReadFile(path)
if err != nil {
return config, err
}
// Parse configuration
err = yaml.Unmarshal(data, config)
if err != nil {
return
}
// Set default address
if config.Address == "" {
config.Address = ":7136"
}
return
}
// pointsHandler writes a point slice as JSON
func pointsHandler(w http.ResponseWriter, name string) {
pts, err := influxClient.GetLastMeasurements(name)
if err != nil {
log.Printf("Error retrieving measurements: %s", err)
w.WriteHeader(500)
return
}
log.Debugf("Points for %s: %+v", name, pts)
data, err := json.Marshal(pts)
if err != nil {
log.Printf("Error encoding measurements: %s", err)
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(data)
}
// measurementsHandler writes the temperatures as an HTTP response
func measurementsHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("%s %s, client %s", r.Method, r.URL, r.RemoteAddr)
pointsHandler(w, "environmental")
}
// measurementsHandler writes the temperatures as an HTTP response
func weatherHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("%s %s, client %s", r.Method, r.URL, r.RemoteAddr)
pointsHandler(w, "weatherstation")
}
func main() {
var configFile, logLevel string
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment