This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
87 lines (67 loc) · 1.81 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"log"
"net"
"net/http"
"strconv"
"strings"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/spf13/viper"
)
var heartRate = 0
func main() {
err := readConfigFile()
if err != nil {
log.Fatal("Could not read config file:", err.Error())
}
fmt.Println("Welcome to Heart of Frogg! Please see https://github.com/bfroggio/heart-of-frogg for usage instructions.")
fmt.Println("I found these local IP addresses on your machine:")
getLocalIP() // Print the local IPs in the terminal
e := echo.New()
e.HideBanner = true
e.Use(middleware.CORS())
e.GET("/heart", func(c echo.Context) error {
return c.String(http.StatusOK, strconv.Itoa(heartRate))
})
e.POST("/heart/:rate", updateHeartRate)
e.Static("/ui", "ui")
e.Logger.Fatal(e.Start(":" + viper.GetString("port")))
}
func updateHeartRate(c echo.Context) error {
newHeartRate, err := strconv.Atoi(c.Param("rate"))
if err != nil {
return err
}
heartRate = newHeartRate
return c.String(http.StatusOK, strconv.Itoa(heartRate))
}
func readConfigFile() error {
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("toml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath(".") // optionally look for config in the working directory
err := viper.ReadInConfig()
if err != nil {
return err
}
return nil
}
// A quick and dirty function to print local IPs
func getLocalIP() {
list, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, iface := range list {
addrs, err := iface.Addrs()
if err != nil {
panic(err)
}
for _, addr := range addrs {
if strings.Contains(addr.String(), "192.168.") {
fmt.Println(" " + iface.Name + ": " + strings.Split(addr.String(), "/")[0])
}
}
}
}