-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpi_temp.go
67 lines (57 loc) · 1.26 KB
/
rpi_temp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Name:
//
// rpi_temp
//
// Description:
//
// Get Raspberry PI CPU temperature (OS: Raspian).
//
// Example:
//
// $ curl http://carmel:9090/
// {"temp":50.4,"hostName":"carmel"}
//
// REST API notes:
//
// http://thenewstack.io/make-a-restful-json-api-go/
// https://github.com/ant0ine/go-json-rest/graphs/contributors
// https://golang.org/doc/articles/wiki/ (Writing Web Applications)
// http://jsonapi.org/
//
// jens@mesgtone.net
//
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/jens18/rpi_temp/cputemp"
"log"
"net/http"
)
// Identifier have to be 'exported' (the first character of the
// identifier's name is a Unicode upper case letter) to permit access
// to it from another package)
//
// https://golang.org/ref/spec#Exported_identifiers
//
func Index(w http.ResponseWriter, r *http.Request) {
var c cputemp.CpuTemp
c = c.Get()
log.Printf("cpuTemp = \"%s\", cpuArch =\"%s\", hostName = \"%s\"\n",
c.Temp,
c.CpuArch,
c.HostName)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
json.NewEncoder(w).Encode(c)
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", Index)
log.Fatal(http.ListenAndServe(":9090", router))
}