Skip to content

Commit d6440eb

Browse files
新增 event , 优化 websocket
1 parent 043b00b commit d6440eb

File tree

16 files changed

+646
-13
lines changed

16 files changed

+646
-13
lines changed

cmd/local/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"gopkg.d7z.net/gitea-pages/pkg/providers"
1616
"gopkg.d7z.net/middleware/cache"
1717
"gopkg.d7z.net/middleware/kv"
18+
"gopkg.d7z.net/middleware/subscribe"
1819
)
1920

2021
var (
@@ -56,8 +57,9 @@ func main() {
5657
if err != nil {
5758
zap.L().Fatal("failed to init memory provider", zap.Error(err))
5859
}
60+
subscriber := subscribe.NewMemorySubscriber()
5961
server, err := pkg.NewPageServer(http.DefaultClient,
60-
provider, domain, "gh-pages", memory, memory, 0, &nopCache{},
62+
provider, domain, "gh-pages", memory, subscriber, memory, 0, &nopCache{}, 0,
6163
func(w http.ResponseWriter, r *http.Request, err error) {
6264
if errors.Is(err, os.ErrNotExist) {
6365
http.Error(w, "page not found.", http.StatusNotFound)

cmd/server/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Config struct {
2222
Domain string `yaml:"domain"` // 基础域名
2323

2424
Database ConfigDatabase `yaml:"database"` // 配置
25+
Event ConfigEvent `yaml:"event"` // 事件传递
2526

2627
Auth ConfigAuth `yaml:"auth"` // 后端认证配置
2728

@@ -76,6 +77,9 @@ type ConfigPage struct {
7677
type ConfigDatabase struct {
7778
URL string `yaml:"url"`
7879
}
80+
type ConfigEvent struct {
81+
URL string `yaml:"url"`
82+
}
7983

8084
type ConfigProxy struct {
8185
Enable bool `yaml:"enable"` // 是否允许反向代理
@@ -113,6 +117,9 @@ func LoadConfig(path string) (*Config, error) {
113117
if c.Database.URL == "" {
114118
return nil, errors.New("c is required")
115119
}
120+
if c.Event.URL == "" {
121+
c.Event.URL = "memory://"
122+
}
116123
if c.StaticDir != "" {
117124
stat, err := os.Stat(c.StaticDir)
118125
if err != nil {

cmd/server/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"gopkg.d7z.net/gitea-pages/pkg/providers"
1717
"gopkg.d7z.net/middleware/cache"
1818
"gopkg.d7z.net/middleware/kv"
19+
"gopkg.d7z.net/middleware/subscribe"
1920
)
2021

2122
var (
@@ -64,15 +65,22 @@ func main() {
6465
if !ok {
6566
log.Fatalln(errors.New("database not support cursor"))
6667
}
68+
event, err := subscribe.NewSubscriberFromURL(config.Event.URL)
69+
if err != nil {
70+
log.Fatalln(err)
71+
}
72+
defer event.Close()
6773
pageServer, err := pkg.NewPageServer(
6874
http.DefaultClient,
6975
backend,
7076
config.Domain,
7177
config.Page.DefaultBranch,
7278
cdb,
79+
event,
7380
cacheMeta,
7481
config.Cache.MetaTTL,
7582
cacheBlob.Child("filter"),
83+
config.Cache.BlobTTL,
7684
config.ErrorHandler,
7785
config.Filters,
7886
)

examples/js_ws_event/.pages.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
routes:
2+
- path: "sender"
3+
js:
4+
exec: "sender.js"
5+
- path: "event"
6+
js:
7+
exec: "event.js"

examples/js_ws_event/event.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
let name=request.getQuery("name")
2+
if (name===""){
3+
throw Error(`Missing name "${name}"`)
4+
}
5+
let ws = websocket();
6+
event.subscribe("messages").on(function (msg){
7+
ws.writeText(msg)
8+
})
9+
let shouldExit = false;
10+
while (!shouldExit) {
11+
let data = ws.readText();
12+
switch (data) {
13+
case "exit":
14+
shouldExit = true;
15+
break;
16+
default:
17+
event.put("messages",JSON.stringify({
18+
name:name,
19+
data:data
20+
}));
21+
break;
22+
}
23+
}

0 commit comments

Comments
 (0)