Skip to content

Commit 366fede

Browse files
author
yusing
committed
feat: add websocket writer and error handling utilities
1 parent 7ef8354 commit 366fede

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

config.example.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# options:
1616
# auth_token: c1234565789-abcdefghijklmnopqrst # your zone API token
1717

18-
# 3. other providers, see https://github.com/yusing/go-proxy/wiki/Supported-DNS%E2%80%9001-Providers#supported-dns-01-providers
18+
# 3. other providers, see https://github.com/yusing/godoxy/wiki/Supported-DNS%E2%80%9001-Providers#supported-dns-01-providers
1919

2020
entrypoint:
2121
# Below define an example of middleware config
@@ -73,7 +73,7 @@ providers:
7373
# url: https://discord.com/api/webhooks/...
7474
# template: discord # this means use payload template from internal/notif/templates/discord.json
7575

76-
# Check https://github.com/yusing/go-proxy/wiki/Certificates-and-domain-matching#domain-matching
76+
# Check https://github.com/yusing/godoxy/wiki/Certificates-and-domain-matching#domain-matching
7777
# for explaination of `match_domains`
7878
#
7979
# match_domains:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package gpwebsocket
2+
3+
import (
4+
"context"
5+
6+
"github.com/coder/websocket"
7+
)
8+
9+
type Writer struct {
10+
conn *websocket.Conn
11+
msgType websocket.MessageType
12+
ctx context.Context
13+
}
14+
15+
func NewWriter(ctx context.Context, conn *websocket.Conn, msgType websocket.MessageType) *Writer {
16+
return &Writer{
17+
ctx: ctx,
18+
conn: conn,
19+
msgType: msgType,
20+
}
21+
}
22+
23+
func (w *Writer) Write(p []byte) (int, error) {
24+
return len(p), w.conn.Write(w.ctx, w.msgType, p)
25+
}
26+
27+
func (w *Writer) Close() error {
28+
return w.conn.CloseNow()
29+
}

internal/net/gphttp/server/error.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package server
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net/http"
7+
8+
"github.com/rs/zerolog"
9+
)
10+
11+
func HandleError(logger *zerolog.Logger, err error, msg string) {
12+
switch {
13+
case err == nil, errors.Is(err, http.ErrServerClosed), errors.Is(err, context.Canceled):
14+
return
15+
default:
16+
logger.Fatal().Err(err).Msg(msg)
17+
}
18+
}

0 commit comments

Comments
 (0)