52 lines
900 B
Go
52 lines
900 B
Go
package util
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
func GetPath() string {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
exe, err = filepath.EvalSymlinks(exe)
|
|
if err != nil {
|
|
}
|
|
|
|
return filepath.Dir(exe)
|
|
}
|
|
|
|
func NormalizePath(path string) string {
|
|
return filepath.Clean(filepath.FromSlash(path))
|
|
}
|
|
|
|
func NormalizeLogPath(path string) string {
|
|
if filepath.IsAbs(path) {
|
|
return path
|
|
}
|
|
|
|
return NormalizePath(filepath.Join(GetPath(), path))
|
|
}
|
|
|
|
//
|
|
|
|
func CloseConnWithControlMessage(conn *websocket.Conn, typ int, text string) {
|
|
_ = conn.SetWriteDeadline(time.Now().Add(time.Second))
|
|
_ = conn.WriteControl(
|
|
typ, websocket.FormatCloseMessage(typ, text), time.Now().Add(time.Second),
|
|
)
|
|
_ = conn.Close()
|
|
}
|
|
|
|
func CloseConn(conn *websocket.Conn) {
|
|
CloseConnWithControlMessage(
|
|
conn, websocket.CloseNormalClosure,
|
|
"Disconnecting.",
|
|
)
|
|
}
|