forked from joewalnes/websocketd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (51 loc) · 1.58 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
// Copyright 2013 Joe Walnes and the websocketd team.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/joewalnes/websocketd/libwebsocketd"
)
func log(l *libwebsocketd.LogScope, level libwebsocketd.LogLevel, levelName string, category string, msg string, args ...interface{}) {
if level < l.MinLevel {
return
}
fullMsg := fmt.Sprintf(msg, args...)
assocDump := ""
for index, pair := range l.Associated {
if index > 0 {
assocDump += " "
}
assocDump += fmt.Sprintf("%s:'%s'", pair.Key, pair.Value)
}
l.Mutex.Lock()
fmt.Printf("%s | %-6s | %-10s | %s | %s\n", libwebsocketd.Timestamp(), levelName, category, assocDump, fullMsg)
l.Mutex.Unlock()
}
func main() {
flag.Usage = PrintHelp
config := parseCommandLine()
log := libwebsocketd.RootLogScope(config.LogLevel, log)
http.Handle(config.BasePath, libwebsocketd.HttpWsMuxHandler{
Config: config.Config,
Log: log})
log.Info("server", "Starting WebSocket server : ws://%s%s", config.Addr, config.BasePath)
if config.DevConsole {
log.Info("server", "Developer console enabled : http://%s/", config.Addr)
}
if config.UsingScriptDir {
log.Info("server", "Serving from directory : %s", config.ScriptDir)
} else {
log.Info("server", "Serving using application : %s %s", config.CommandName, strings.Join(config.CommandArgs, " "))
}
err := http.ListenAndServe(config.Addr, nil)
if err != nil {
log.Fatal("server", "Could start server: %s", err)
os.Exit(3)
}
}