2014-03-09 16:53:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 struktur AG
|
|
|
|
* http://www.strukturag.com
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2017-05-22 14:04:03 +02:00
|
|
|
|
|
|
|
"github.com/longsleep/realtimetraffic/client"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2014-03-09 16:53:21 +01:00
|
|
|
)
|
|
|
|
|
2017-05-22 14:04:03 +02:00
|
|
|
var staticPath *string
|
|
|
|
var listenAddr *string
|
2014-03-09 16:53:21 +01:00
|
|
|
|
|
|
|
func serveClient(w http.ResponseWriter, r *http.Request) {
|
2017-05-22 14:04:03 +02:00
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "Method nod allowed", 405)
|
2014-03-09 16:53:21 +01:00
|
|
|
return
|
|
|
|
}
|
2017-05-22 14:04:03 +02:00
|
|
|
|
|
|
|
http.StripPrefix("/", client.HandlerFunc).ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveWs(w http.ResponseWriter, r *http.Request) {
|
2014-03-09 16:53:21 +01:00
|
|
|
if r.Method != "GET" {
|
2017-05-22 14:04:03 +02:00
|
|
|
http.Error(w, "Method not allowed", 405)
|
2014-03-09 16:53:21 +01:00
|
|
|
return
|
|
|
|
}
|
2017-05-22 14:04:03 +02:00
|
|
|
|
|
|
|
// Read request details.
|
|
|
|
r.ParseForm()
|
|
|
|
iface := r.FormValue("if")
|
|
|
|
if iface == "" {
|
|
|
|
iface = "eth0"
|
|
|
|
}
|
|
|
|
|
|
|
|
ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
|
|
|
|
if _, ok := err.(websocket.HandshakeError); ok {
|
|
|
|
http.Error(w, "Not a websocket handshake", 400)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
2014-03-09 16:53:21 +01:00
|
|
|
}
|
2017-05-22 14:04:03 +02:00
|
|
|
c := &connection{send: make(chan []byte, 256), ws: ws, iface: iface}
|
|
|
|
h.register <- c
|
|
|
|
go c.writePump()
|
|
|
|
c.readPump()
|
2014-03-09 16:53:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
|
2017-05-22 14:04:03 +02:00
|
|
|
staticPath = flag.String("client", "", "Full path to client directory.")
|
|
|
|
listenAddr = flag.String("listen", "127.0.0.1:8088", "Listen address.")
|
2014-03-09 16:53:21 +01:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
go h.run()
|
|
|
|
http.HandleFunc("/", serveClient)
|
|
|
|
http.HandleFunc("/realtimetraffic", serveWs)
|
2017-05-22 14:04:03 +02:00
|
|
|
/*http.Handle("/css/", http.FileServer(http.Dir(*client)))
|
2014-03-09 16:53:21 +01:00
|
|
|
http.Handle("/scripts/", http.FileServer(http.Dir(*client)))
|
|
|
|
http.Handle("/img/", http.FileServer(http.Dir(*client)))
|
|
|
|
http.Handle("/favicon.ico", http.FileServer(http.Dir(*client)))
|
2017-05-22 14:04:03 +02:00
|
|
|
*/
|
2014-03-09 16:53:21 +01:00
|
|
|
|
2017-05-22 14:04:03 +02:00
|
|
|
err = http.ListenAndServe(*listenAddr, nil)
|
2014-03-09 16:53:21 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("ListenAndServe: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|