Skip to content

Commit

Permalink
[Sse]: fix annoying things and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
4lxprime committed Aug 31, 2023
1 parent 6abd2c9 commit 5bfd8c9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 84 deletions.
23 changes: 2 additions & 21 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
bin/*
.svelte_*
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@

## why gosvelt ?
### fullstack integration of svelte
yeah, gosvelt will compile, group, and serve svelte pages.
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)
```golang
func main() {
r := gosvelt.New()

r.Svelte("/", "./static/App.svelte", func(c *gosvelt.Context, svelte gosvelt.Map) error {
return c.Html(200, "./static/index.html", svelte)
return c.Html(200, "./static/index.html", svelte) // html template
})

r.AdvancedSvelte("/adv", "./static/", "App.svelte", func(c *gosvelt.Context, svelte gosvelt.Map) error {
return c.Html(200, "./static/index.html", svelte)

}, gs.SvelteConfig{
r.AdvancedSvelte("/adv", "./static/", "App.svelte",
func(c *gosvelt.Context, svelte gosvelt.Map) error {
return c.Html(200, "./static/index.html", svelte) // html template
},
gs.SvelteConfig{
Typescript: false,
Tailwindcss: true,
Pnpm: true,
Expand All @@ -25,15 +28,16 @@ func main() {
}
```
### cool way to made sse
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.
```golang
func main() {
r := gosvelt.New()

r.Get("/sse", func(c *gs.Context) error {
r.Get("/sse", func(c *gs.Context) error { // context way
datach := make(chan interface{})
closech := make(chan struct{})

return c.Sse(datach, closech, "test", func() {
return c.Sse(datach, closech, func() {
datach <- "hello"

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

r.Sse("/sse2", datach, closech, "test2", func() {
r.Sse("/sse2", datach, closech, func() { // handler way
datach <- "hello"

for i := 0; i < 4; i++ {
Expand All @@ -63,23 +67,25 @@ func main() {
}
```
### pretty simple syntax
the syntax is like popular framworks like fiber, gin, echo
```golang
func main() {
r := gosvelt.New()

r.Get("/gg/:name", func(c *gosvelt.Context) error {
r.Get("/gg/:name", func(c *gosvelt.Context) error { // url params
return c.Json(200, gosvelt.Map{"gg": c.Param("name")})
})

r.Get("/ws", func(c *gosvelt.Context) error {
r.Get("/ws", func(c *gosvelt.Context) error { // websocket handler
return c.Ws(func(conn *websocket.Conn) {
conn.WriteJSON(gosvelt.Map{"ez": "pz"})
})
})

r.Static("/index", "./cmd/static/index.html")
r.Static("/index", "./cmd/static/index.html") // static files

r.Svelte("/", "./cmd/static/App.svelte", func(c *gosvelt.Context, svelte gosvelt.Map) error {
r.Svelte("/", "./cmd/static/App.svelte",
func(c *gosvelt.Context, svelte gosvelt.Map) error { // svelte files
return c.Html(200, "./cmd/static/index.html", svelte)
})

Expand Down
28 changes: 13 additions & 15 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {

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

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

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

close(closech)
})
})

r.AdvancedSvelte(
"/ssepage",
"cmd/static/",
"sse/App.svelte",
r.AdvancedSvelte("/ssepage", "cmd/static/", "sse/App.svelte",
func(c *gs.Context, svelte gs.Map) error {
return c.Html(200, "./cmd/static/index.html", svelte)
},
Expand All @@ -82,14 +79,15 @@ func main() {
},
)

// r.AdvancedSvelte("/", "cmd/static/", "app/App.svelte", func(c *gs.Context, svelte gs.Map) error {
// return c.Html(200, "./cmd/static/index.html", svelte)

// }, gs.SvelteConfig{
// Typescript: false,
// Tailwindcss: true,
// Pnpm: true,
// })
r.AdvancedSvelte("/", "cmd/static/", "app/App.svelte",
func(c *gs.Context, svelte gs.Map) error {
return c.Html(200, "./cmd/static/index.html", svelte)
},
gs.SvelteConfig{
Typescript: false,
Tailwindcss: true,
Pnpm: true,
})

r.Start(":8080")
}
11 changes: 3 additions & 8 deletions cmd/static/sse/App.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<script>
import { writable } from 'svelte/store';
let messages = writable([]);
let messages = writable([])
let sse = new EventSource("/sse")
.onmessage = (e)=>{
console.log(`message: ${e.data}`);
messages.update(cm => [...cm, e.data])
}
sse.onerror = (e)=>{
console.log(`error ${e}`)
}
sse.onmessage = (e) => messages.update(cm => [...cm, e.data])
sse.onerror = () => sse.close()
</script>

Expand Down
11 changes: 1 addition & 10 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,6 @@ func (c *Context) Ws(handler websocket.FastHTTPHandler) error {
return nil
}

type SseEvent struct {
Name string
Data string
}

type Sse struct {
events []*SseEvent
}

func (c *Context) Sse(datach chan interface{}, closech chan struct{}, fn func()) error {
// cors headers
//c.SetHeader("Access-Control-Allow-Origin", "*")
Expand Down Expand Up @@ -316,7 +307,7 @@ func (c *Context) Sse(datach chan interface{}, closech chan struct{}, fn func())
fmt.Fprintf(w, "data: %s\n\n", m)

case SseEvent:
fmt.Fprintf(w, "event: %s\n", m.Name)
fmt.Fprintf(w, "event: %s\n\n", m.Name)
fmt.Fprintf(w, "data: %s\n\n", m.Data)

default: // we don't care
Expand Down
40 changes: 22 additions & 18 deletions gosvelt.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,20 @@ func (gs *GoSvelt) Static(path, file string) {
gs.addStatic(MGet, path, file)
}

type SseConfig struct {
Datach chan interface{} // needed
Closech chan struct{} // needed
EventName string // optional
Timeout time.Duration // optional
// // see config
// type SseConfig struct {
// Datach chan interface{} // needed
// Closech chan struct{} // needed
// Timeout time.Duration // optional
// }

// sse event
type SseEvent struct {
Name string // event name
Data string // event datas
}

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

fmt.Printf("sse: write event %s\n", eventName)
fmt.Fprintf(w, "event: %s\n\n", eventName)
flush()

Loop:
//Loop:
for {
select {
case <-closech:
if datach != nil {
for range datach {
}
close(datach)
}
close(datach)

break Loop
//c.Res().Header.SetConnectionClose()

return

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

case any: // we don't care
case SseEvent:
fmt.Fprintf(w, "event: %s\n\n", m.Name)
fmt.Fprintf(w, "data: %s\n\n", m.Data)

default: // we don't care
fmt.Fprintf(w, "data: %s\n\n", m)
}

Expand Down

0 comments on commit 5bfd8c9

Please sign in to comment.