/
logserv.go
110 lines (87 loc) · 2.43 KB
/
logserv.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"os"
"flag"
"encoding/json"
"io/ioutil"
"net/http"
"code.google.com/p/go.net/websocket"
"github.com/howeyc/fsnotify"
auth "github.com/abbot/go-http-auth"
"io"
"log"
)
var config Config
func rootHandler(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
io.WriteString(w, `<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
window.onload = function() {
var ws = new WebSocket("ws://websocket");
ws.onopen = function() {
ws.send('New participant joined');
alert('toto');
};
ws.onmessage = function (evt) {
$("#chat").append("<div>" + evt.data + "</div>");
};
};
</script>
</head>
<body><h3>Logs</h3>
<div id="chat" style="width: 60em; height: 20em; overflow:auto; border: 1px solid black">
</div>
</body></html>`)
}
func EchoServer(ws *websocket.Conn) {
fmt.Print(ws.Request())
}
type Logfile struct {
Path string
Users []string
}
type Config struct {
Port int
Auth_file string
Log_files []Logfile
Channel_map map[string]chan *os.File
}
func main() {
var config_path = flag.String("config", "", "Path to the config file")
flag.Parse()
if *config_path == "" { log.Fatal("Give path to the config file with -path=") }
b, err := ioutil.ReadFile(*config_path)
if err != nil { log.Fatal(err) }
err = json.Unmarshal(b, &config)
if err != nil { log.Fatal(err) }
if config.Port == 0 || config.Auth_file == "" || len(config.Log_files) == 0 { log.Fatal("Invalid configuration") }
watcher, err := fsnotify.NewWatcher()
if err != nil { log.Fatal(err) }
go func() {
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev)
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}()
for _, v := range(config.Log_files) {
log.Print(v.Path)
err = watcher.Watch(v.Path)
if err != nil { log.Fatal(err) }
}
secrets := auth.HtpasswdFileProvider(config.Auth_file)
authenticator := auth.BasicAuthenticator("logserv", secrets)
http.HandleFunc("/", authenticator(rootHandler))
http.Handle("/websocket", websocket.Handler(EchoServer))
listen_address := fmt.Sprintf(":%d", config.Port)
http.ListenAndServe(listen_address, nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
watcher.Close()
}