YealinkRemote/main.go

148 lines
3.7 KiB
Go
Raw Permalink Normal View History

2018-08-06 10:25:31 +02:00
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"github.com/gorilla/mux"
"github.com/surgemq/surgemq/service"
)
var StartUpPath string
// our main function
func main() {
// StartupPath auslesen
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
StartUpPath = dir
fmt.Println("-----------------------------------------------")
fmt.Println("Escape Room 0.3")
fmt.Println("-----------------------------------------------")
fmt.Println("Webserver: http://*:8000")
fmt.Println("MQTT-Server: tcp://*:1833")
fmt.Println("Websocket-Server: http://*:8081")
fmt.Println("-----------------------------------------------")
fmt.Println("Admin-Seite: http://127.0.0.1:8000/admin.html")
fmt.Println("-----------------------------------------------")
fmt.Println("")
// Create a new mqtt server
svr := &service.Server{
KeepAlive: 300, // seconds
ConnectTimeout: 2, // seconds
SessionsProvider: "mem", // keeps sessions in memory
Authenticator: "mockSuccess", // always succeed
TopicsProvider: "mem", // keeps topic subscriptions in memory
}
// Websocket
addr := "tcp://:1883"
AddWebsocketHandler("/mqtt", addr)
go ListenAndServeWebsocket(":8081")
// Listen and serve connections at localhost:1883
go svr.ListenAndServe("tcp://:1883")
router := mux.NewRouter()
//router.HandleFunc("/api/folder", GetFolderContent).Methods("GET")
router.HandleFunc("/api/folder/{foldername:.*}", GetFolderContent).Methods("GET")
//router.HandleFunc("/api/image/{imagename:.*}", GetImage).Methods("GET")
//-------------------------------------------
router.HandleFunc("/api/sendkey/{ip}/{key}", GetSendKey).Methods("GET")
//-------------------------------------------
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./web"))).Methods("GET")
log.Fatal(http.ListenAndServe(":8000", router))
}
func GetSendKey(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
pIP := params["ip"]
pKey := params["key"]
resp, err := http.Get("http://admin:admin@" + pIP + "/servlet?key=" + pKey)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
json.NewEncoder(w).Encode(body)
}
func GetFolderContent(w http.ResponseWriter, r *http.Request) {
// http://127.0.0.1:8000/api/folder/1080_stahlwerke
params := mux.Vars(r)
pFolder := params["foldername"]
fmt.Println(path.Join(StartUpPath, "web", pFolder))
files, err := ioutil.ReadDir(path.Join(StartUpPath, "web", pFolder))
if err != nil {
log.Fatal(err)
}
var dateien []string
for _, f := range files {
dateien = append(dateien, f.Name())
}
json.NewEncoder(w).Encode(dateien)
}
func isDir(name string) bool {
var ret bool
fi, err := os.Stat(name)
if err != nil {
panic(err)
}
if fi.IsDir() {
ret = true
} else {
ret = false
}
return ret
}
/*
func GetMem(w http.ResponseWriter, r *http.Request) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Printf("\nAlloc = %v\nTotalAlloc = %v\nSys = %v\nNumGC = %v\n\n", m.Alloc/1024, m.TotalAlloc/1024, m.Sys/1024, m.NumGC)
json.NewEncoder(w).Encode(m)
PrintMemUsage()
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
*/