Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dingen
CloudBurst
Commits
f7be2f25
Commit
f7be2f25
authored
Mar 22, 2019
by
Silke Hofstra
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move config and handlers to separate files
parent
d8a29ab2
Pipeline
#22455
passed with stages
in 1 minute and 1 second
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
70 deletions
+82
-70
config.go
config.go
+39
-0
handlers.go
handlers.go
+42
-0
main.go
main.go
+1
-70
No files found.
config.go
0 → 100644
View file @
f7be2f25
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
}
handlers.go
0 → 100644
View file @
f7be2f25
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"
)
}
main.go
View file @
f7be2f25
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment