Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Heartbeats, ssl, constants
  • Loading branch information
wrr14001 committed Feb 25, 2019
1 parent 95d18e7 commit 563b3cc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
*.exe
.idea/*
.idea/*
executable/*
7 changes: 7 additions & 0 deletions constants.go
@@ -0,0 +1,7 @@
package main

const (
// Header constants
headerUUID = "uuid"
headerTimestamp = "time"
)
24 changes: 21 additions & 3 deletions gateway.go
Expand Up @@ -7,15 +7,24 @@ import (
"net/http"
)

// TODO: log to file

func main() {

http.HandleFunc("/data", dataHandler)
if err := http.ListenAndServe(":8081", nil); err != nil {
fmt.Printf("Error serving 8081: %v", err)
http.HandleFunc("/heartbeat", heartbeatHandler)

if err := http.ListenAndServeTLS(
":48820",
"/etc/pki/tls/certs/gateway.crt",
"/etc/pki/tls/private/gateway.key",
nil); err != nil {
fmt.Printf("Error serving TLS: %v\n", err)
}
}

func dataHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Got data from: %+v at %v\n", r.Header.Get("uuid"), r.Header.Get("time"))
fmt.Printf("Got data from: %v at %v\n", r.Header.Get("uuid"), r.Header.Get("time"))
body, _ := ioutil.ReadAll(r.Body)
var data map[string][]StampedReading
if err := json.Unmarshal(body, &data); err != nil {
Expand All @@ -25,3 +34,12 @@ func dataHandler(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(200)
}

func heartbeatHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Got heartbeat from %v@%v at %v\n",
r.Header.Get(headerUUID),
r.RemoteAddr,
r.Header.Get(headerTimestamp))

w.WriteHeader(200)
}

0 comments on commit 563b3cc

Please sign in to comment.