Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Gateway-Server/gateway.go
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
45 lines (37 sloc)
1006 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
// TODO: log to file | |
func main() { | |
http.HandleFunc("/data", dataHandler) | |
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")) | |
body, _ := ioutil.ReadAll(r.Body) | |
var data map[string][]StampedReading | |
if err := json.Unmarshal(body, &data); err != nil { | |
fmt.Printf("Error unmarshalling body: %v\n", err) | |
} else { | |
fmt.Printf("Data received: %v\n", data) | |
} | |
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) | |
} |