forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_utils.go
49 lines (43 loc) · 1.06 KB
/
websocket_utils.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
package slack
import (
"fmt"
"log"
"net"
"net/url"
"strconv"
"time"
)
// JSONTimeString is an auxiliary type to allow us to format the time as we wish
type JSONTimeString string
// String converts the unix timestamp into a string
func (t JSONTimeString) String() string {
tm := t.Time()
if tm.IsZero() {
return ""
}
return fmt.Sprintf("\"%s\"", tm.Format("Mon Jan _2"))
}
// Time converts the timestamp string to time.Time
func (t JSONTimeString) Time() time.Time {
if t == "" {
return time.Time{}
}
floatN, err := strconv.ParseFloat(string(t), 64)
if err != nil {
log.Println("ERROR parsing a JSONTimeString!", err)
return time.Time{}
}
return time.Unix(int64(floatN), 0)
}
var portMapping = map[string]string{"ws": "80", "wss": "443"}
func websocketizeURLPort(orig string) (string, error) {
urlObj, err := url.ParseRequestURI(orig)
if err != nil {
return "", err
}
_, _, err = net.SplitHostPort(urlObj.Host)
if err != nil {
return urlObj.Scheme + "://" + urlObj.Host + ":" + portMapping[urlObj.Scheme] + urlObj.Path, nil
}
return orig, nil
}