Skip to content

Commit 5bfd8c9

Browse files
committed
[Sse]: fix annoying things and update readme
1 parent 6abd2c9 commit 5bfd8c9

File tree

6 files changed

+59
-84
lines changed

6 files changed

+59
-84
lines changed

.gitignore

+2-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,2 @@
1-
# If you prefer the allow list template instead of the deny list, see community template:
2-
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3-
#
4-
# Binaries for programs and plugins
5-
*.exe
6-
*.exe~
7-
*.dll
8-
*.so
9-
*.dylib
10-
11-
# Test binary, built with `go test -c`
12-
*.test
13-
14-
# Output of the go coverage tool, specifically when used with LiteIDE
15-
*.out
16-
17-
# Dependency directories (remove the comment below to include it)
18-
# vendor/
19-
20-
# Go workspace file
21-
go.work
1+
bin/*
2+
.svelte_*

README.md

+18-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44

55
## why gosvelt ?
66
### fullstack integration of svelte
7+
yeah, gosvelt will compile, group, and serve svelte pages.
8+
A Svelte or AdvancedSvelte handler will give you a **svelte map** wich contain "js" and "css" URLs and you can add to this map your own attributes that will be rendered on the html template (note: if you add for example a "test" element to the map, you have to add the `&{test}` element in the html template)
79
```golang
810
func main() {
911
r := gosvelt.New()
1012

1113
r.Svelte("/", "./static/App.svelte", func(c *gosvelt.Context, svelte gosvelt.Map) error {
12-
return c.Html(200, "./static/index.html", svelte)
14+
return c.Html(200, "./static/index.html", svelte) // html template
1315
})
1416

15-
r.AdvancedSvelte("/adv", "./static/", "App.svelte", func(c *gosvelt.Context, svelte gosvelt.Map) error {
16-
return c.Html(200, "./static/index.html", svelte)
17-
18-
}, gs.SvelteConfig{
17+
r.AdvancedSvelte("/adv", "./static/", "App.svelte",
18+
func(c *gosvelt.Context, svelte gosvelt.Map) error {
19+
return c.Html(200, "./static/index.html", svelte) // html template
20+
},
21+
gs.SvelteConfig{
1922
Typescript: false,
2023
Tailwindcss: true,
2124
Pnpm: true,
@@ -25,15 +28,16 @@ func main() {
2528
}
2629
```
2730
### cool way to made sse
31+
there are actyally to way to use sse in gosvelt: the **context** way wich is in a context and can use channels declared in the handler. And the **handler** way wich is an handler function and use channels who are declared outside the handler.
2832
```golang
2933
func main() {
3034
r := gosvelt.New()
3135

32-
r.Get("/sse", func(c *gs.Context) error {
36+
r.Get("/sse", func(c *gs.Context) error { // context way
3337
datach := make(chan interface{})
3438
closech := make(chan struct{})
3539

36-
return c.Sse(datach, closech, "test", func() {
40+
return c.Sse(datach, closech, func() {
3741
datach <- "hello"
3842

3943
for i := 0; i < 10; i++ {
@@ -48,7 +52,7 @@ func main() {
4852
datach := make(chan interface{})
4953
closech := make(chan struct{})
5054

51-
r.Sse("/sse2", datach, closech, "test2", func() {
55+
r.Sse("/sse2", datach, closech, func() { // handler way
5256
datach <- "hello"
5357

5458
for i := 0; i < 4; i++ {
@@ -63,23 +67,25 @@ func main() {
6367
}
6468
```
6569
### pretty simple syntax
70+
the syntax is like popular framworks like fiber, gin, echo
6671
```golang
6772
func main() {
6873
r := gosvelt.New()
6974

70-
r.Get("/gg/:name", func(c *gosvelt.Context) error {
75+
r.Get("/gg/:name", func(c *gosvelt.Context) error { // url params
7176
return c.Json(200, gosvelt.Map{"gg": c.Param("name")})
7277
})
7378

74-
r.Get("/ws", func(c *gosvelt.Context) error {
79+
r.Get("/ws", func(c *gosvelt.Context) error { // websocket handler
7580
return c.Ws(func(conn *websocket.Conn) {
7681
conn.WriteJSON(gosvelt.Map{"ez": "pz"})
7782
})
7883
})
7984

80-
r.Static("/index", "./cmd/static/index.html")
85+
r.Static("/index", "./cmd/static/index.html") // static files
8186

82-
r.Svelte("/", "./cmd/static/App.svelte", func(c *gosvelt.Context, svelte gosvelt.Map) error {
87+
r.Svelte("/", "./cmd/static/App.svelte",
88+
func(c *gosvelt.Context, svelte gosvelt.Map) error { // svelte files
8389
return c.Html(200, "./cmd/static/index.html", svelte)
8490
})
8591

cmd/main.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func main() {
3838

3939
datach := make(chan interface{})
4040
closech := make(chan struct{})
41-
r.Sse("/test/sse", datach, closech, "test", func() {
41+
r.Sse("/test/sse", datach, closech, func() {
4242
datach <- "hello"
4343

4444
for i := 0; i < 4; i++ {
@@ -57,21 +57,18 @@ func main() {
5757
datach <- "hello world"
5858

5959
for i := 0; i < 2; i++ {
60-
time.Sleep(2 * time.Millisecond)
60+
time.Sleep(100 * time.Millisecond)
6161
datach <- gs.SseEvent{
6262
Name: "date",
63-
Data: fmt.Sprintf("%d -> actual time is %v", i, time.Now()),
63+
Data: fmt.Sprintf("time: %v", time.Now()),
6464
}
6565
}
6666

6767
close(closech)
6868
})
6969
})
7070

71-
r.AdvancedSvelte(
72-
"/ssepage",
73-
"cmd/static/",
74-
"sse/App.svelte",
71+
r.AdvancedSvelte("/ssepage", "cmd/static/", "sse/App.svelte",
7572
func(c *gs.Context, svelte gs.Map) error {
7673
return c.Html(200, "./cmd/static/index.html", svelte)
7774
},
@@ -82,14 +79,15 @@ func main() {
8279
},
8380
)
8481

85-
// r.AdvancedSvelte("/", "cmd/static/", "app/App.svelte", func(c *gs.Context, svelte gs.Map) error {
86-
// return c.Html(200, "./cmd/static/index.html", svelte)
87-
88-
// }, gs.SvelteConfig{
89-
// Typescript: false,
90-
// Tailwindcss: true,
91-
// Pnpm: true,
92-
// })
82+
r.AdvancedSvelte("/", "cmd/static/", "app/App.svelte",
83+
func(c *gs.Context, svelte gs.Map) error {
84+
return c.Html(200, "./cmd/static/index.html", svelte)
85+
},
86+
gs.SvelteConfig{
87+
Typescript: false,
88+
Tailwindcss: true,
89+
Pnpm: true,
90+
})
9391

9492
r.Start(":8080")
9593
}

cmd/static/sse/App.svelte

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
<script>
22
import { writable } from 'svelte/store';
33
4-
let messages = writable([]);
4+
let messages = writable([])
55
66
let sse = new EventSource("/sse")
7-
.onmessage = (e)=>{
8-
console.log(`message: ${e.data}`);
9-
messages.update(cm => [...cm, e.data])
10-
}
117
12-
sse.onerror = (e)=>{
13-
console.log(`error ${e}`)
14-
}
8+
sse.onmessage = (e) => messages.update(cm => [...cm, e.data])
9+
sse.onerror = () => sse.close()
1510
1611
</script>
1712

context.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,6 @@ func (c *Context) Ws(handler websocket.FastHTTPHandler) error {
268268
return nil
269269
}
270270

271-
type SseEvent struct {
272-
Name string
273-
Data string
274-
}
275-
276-
type Sse struct {
277-
events []*SseEvent
278-
}
279-
280271
func (c *Context) Sse(datach chan interface{}, closech chan struct{}, fn func()) error {
281272
// cors headers
282273
//c.SetHeader("Access-Control-Allow-Origin", "*")
@@ -316,7 +307,7 @@ func (c *Context) Sse(datach chan interface{}, closech chan struct{}, fn func())
316307
fmt.Fprintf(w, "data: %s\n\n", m)
317308

318309
case SseEvent:
319-
fmt.Fprintf(w, "event: %s\n", m.Name)
310+
fmt.Fprintf(w, "event: %s\n\n", m.Name)
320311
fmt.Fprintf(w, "data: %s\n\n", m.Data)
321312

322313
default: // we don't care

gosvelt.go

+22-18
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,20 @@ func (gs *GoSvelt) Static(path, file string) {
218218
gs.addStatic(MGet, path, file)
219219
}
220220

221-
type SseConfig struct {
222-
Datach chan interface{} // needed
223-
Closech chan struct{} // needed
224-
EventName string // optional
225-
Timeout time.Duration // optional
221+
// // see config
222+
// type SseConfig struct {
223+
// Datach chan interface{} // needed
224+
// Closech chan struct{} // needed
225+
// Timeout time.Duration // optional
226+
// }
227+
228+
// sse event
229+
type SseEvent struct {
230+
Name string // event name
231+
Data string // event datas
226232
}
227233

228-
func (gs *GoSvelt) Sse(path string, datach chan interface{}, closech chan struct{}, eventName string, fn func()) {
234+
func (gs *GoSvelt) Sse(path string, datach chan interface{}, closech chan struct{}, fn func()) {
229235
handler := func(c *fasthttp.RequestCtx) {
230236
// cors headers
231237
//c.Response.Header.Add("Access-Control-Allow-Origin", "*")
@@ -249,28 +255,26 @@ func (gs *GoSvelt) Sse(path string, datach chan interface{}, closech chan struct
249255
}
250256
}
251257

252-
fmt.Printf("sse: write event %s\n", eventName)
253-
fmt.Fprintf(w, "event: %s\n\n", eventName)
254-
flush()
255-
256-
Loop:
258+
//Loop:
257259
for {
258260
select {
259261
case <-closech:
260-
if datach != nil {
261-
for range datach {
262-
}
263-
close(datach)
264-
}
262+
close(datach)
265263

266-
break Loop
264+
//c.Res().Header.SetConnectionClose()
265+
266+
return
267267

268268
case msg := <-datach:
269269
switch m := msg.(type) {
270270
case string:
271271
fmt.Fprintf(w, "data: %s\n\n", m)
272272

273-
case any: // we don't care
273+
case SseEvent:
274+
fmt.Fprintf(w, "event: %s\n\n", m.Name)
275+
fmt.Fprintf(w, "data: %s\n\n", m.Data)
276+
277+
default: // we don't care
274278
fmt.Fprintf(w, "data: %s\n\n", m)
275279
}
276280

0 commit comments

Comments
 (0)