in codelabs/health_data_analysis_codelab/src/usleep/workload.go [120:168]
func handleConnectionRequest(w http.ResponseWriter, r *http.Request) {
// Upgrade HTTP Connection to a websocket.
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Printf("failed to upgrade connection to a websocket with err: %v\n", err)
return
}
defer conn.Close()
// Get EKM
hash, err := getEKMHashFromRequest(r)
if err != nil {
fmt.Printf("Failed to get EKM: %v", err)
}
// Request token with TLS Exported Keying Material (EKM) hashed.
token, err := getCustomToken(hash)
if err != nil {
fmt.Printf("failed to get custom token from token endpoint: %v", err)
return
}
// Respond to the client with the token.
conn.WriteMessage(websocket.TextMessage, token)
// Read the sensitive data
_, content, err := conn.ReadMessage()
if err != nil {
fmt.Printf("failed to read message from the connection: %v\n", err)
}
fmt.Printf("Received content from other side, %v\n", string(content))
var healthData healthData
err = json.Unmarshal(content, &healthData)
if err != nil {
fmt.Printf("Failed to unmarshal health data: %v\n", err)
}
// Run algorithms on health data.
result := calculateSleepQuality(healthData)
fmt.Printf("Sleep quality result: %v\n", result)
// Terminate the connection in case the client failed to connect.
fmt.Println("terminating connection")
err = conn.Close()
if err != nil {
fmt.Printf("Failed to close the conn. Not failing. err: %v\n", err)
}
}