main.go 1.32 KB
Newer Older
Kevin Lyda's avatar
Kevin Lyda committed
1
2
3
4
5
6
7
8
9
10
//
// main.go
// Copyright (C) 2017 kevin <kevin@ie.suberic.net>
//
// Distributed under terms of the MIT license.
//

package main

import (
Kevin Lyda's avatar
Kevin Lyda committed
11
	"flag"
Kevin Lyda's avatar
Kevin Lyda committed
12
	"html/template"
Kevin Lyda's avatar
Kevin Lyda committed
13
	"log"
Kevin Lyda's avatar
Kevin Lyda committed
14
	"net/http"
Kevin Lyda's avatar
Kevin Lyda committed
15
	"path"
Kevin Lyda's avatar
Kevin Lyda committed
16
17

	"github.com/prometheus/client_golang/prometheus/promhttp"
Kevin Lyda's avatar
Kevin Lyda committed
18
	"gitlab.com/lyda/gqgmc/devices/geiger"
Kevin Lyda's avatar
Kevin Lyda committed
19
20
)

Kevin Lyda's avatar
Kevin Lyda committed
21
var addr = flag.String("listen-address", ":8080", "Address for HTTP requests.")
Kevin Lyda's avatar
Kevin Lyda committed
22
23
var device = flag.String("device", "/dev/gqgmc", "Device for Geiger Counter.")
var model = flag.String("model", "gqgmc", "Model of Geiger Counter.")
Kevin Lyda's avatar
Kevin Lyda committed
24
var templateDir = flag.String("template_dir", "templates", "Template directory.")
Kevin Lyda's avatar
Kevin Lyda committed
25

Kevin Lyda's avatar
Kevin Lyda committed
26
27
28
29
30
31
32
33
34
35
36
37
38
type indexPage struct {
	Model   string
	Version string
	Serial  string
	Volts   int16
	CPM     uint16
}

var gc geiger.Counter
var indexPg indexPage

func indexHandler(w http.ResponseWriter, r *http.Request) {
	indexPg.CPM, _ = gc.GetCPM()
Kevin Lyda's avatar
Kevin Lyda committed
39
	t, err := template.ParseFiles(path.Join(*templateDir, "index.html"))
Kevin Lyda's avatar
Kevin Lyda committed
40
41
42
43
	if err != nil {
		log.Printf("Template error: %s\n", err)
	}
	t.Execute(w, &indexPg)
Kevin Lyda's avatar
Kevin Lyda committed
44
45
46
}

func main() {
Kevin Lyda's avatar
Kevin Lyda committed
47
	flag.Parse()
Kevin Lyda's avatar
Kevin Lyda committed
48
49
50
51

	gc, _ = geiger.New(geiger.Config{Model: *model, Device: *device})
	indexPg.Model = gc.Model()
	indexPg.Version = gc.Version()
Kevin Lyda's avatar
Kevin Lyda committed
52
	indexPg.Serial = gc.Serial()
Kevin Lyda's avatar
Kevin Lyda committed
53
54
	indexPg.Volts, _ = gc.Volts()
	http.HandleFunc("/", indexHandler)
Kevin Lyda's avatar
Kevin Lyda committed
55
56
	http.Handle("/metrics", promhttp.Handler())
	log.Fatal(http.ListenAndServe(*addr, nil))
Kevin Lyda's avatar
Kevin Lyda committed
57
}