Skip to content

Commit 4431887

Browse files
committed
[Config+Doc]: upgrade config system to options + update docs
1 parent 497f760 commit 4431887

File tree

3 files changed

+145
-111
lines changed

3 files changed

+145
-111
lines changed

README.md

+54-29
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# GoSvelt
2-
the `fasthttp` `fullstack` golang framwork using `svelte` (support tailwindcss).
3-
just more 10 time faster than `sveltekit`
2+
The `fasthttp` `fullstack` golang framwork using `svelte` (support tailwindcss)
3+
that use to be (blazingly) faster than default `sveltekit`
44

5-
## why gosvelt ?
6-
### 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)
5+
## Why GoSvelt ?
6+
### Fullstack integration of Svelte
7+
Yeah, gosvelt will compile, group, and serve svelte pages at runtime which is pretty cool.
8+
We are using the vitejs/vite svelte typescript compiler, with this, we can do likely everything we want, we could add few really interesting options.
9+
The "compiler" accept for the moment javascript / typescript svelte and tailwindcss, if you want some features to be added, i'll be happy to add them.
10+
A Svelte handler will give you a **svelte map** wich contain "js" and "css" URLs, 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)
911
```golang
1012
func main() {
11-
r := gosvelt.New()
13+
app := gosvelt.New()
1214

13-
r.Svelte("/", "App.svelte",
15+
app.Svelte("/", "App.svelte",
1416
func(c *gs.Context, svelte gs.Map) error {
1517
return c.Html(200, "assets/index.html", svelte)
1618
},
@@ -19,62 +21,85 @@ func main() {
1921
gs.WithRoot("views"),
2022
)
2123

22-
r.Start(":80")
24+
app.Start(":80")
2325
}
2426
```
25-
### cool way to made sse
26-
there are actyally two 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.
27+
You can note that this could be faster (blazingly fast) than default sveltekit or default vite server as go is likely way faster than nodejs.
28+
### Cool way to do SSE
29+
There are actually two way to use sse in gosvelt:
30+
- The **context** way where you can instantiate your channels in the handler function and you can return a goroutine that will handle the sse stream.
31+
- The **handler** way wich is a handler that will take outside channels (it could be really nice if you have some external struct for events handling) and instead of giving a handler function, you just give the goroutine function that will handle the sse stream.
2732
```golang
2833
func main() {
29-
r := gosvelt.New()
34+
app := gosvelt.New()
3035

31-
r.Get("/sse", func(c *gs.Context) error { // context way
36+
app.Get("/sse", func(c *gs.Context) error {
3237
datach := make(chan interface{})
3338
closech := make(chan struct{})
3439

3540
return c.Sse(datach, closech, func() {
36-
datach <- "hello"
41+
defer close(closech)
42+
datach <- "hello world"
3743

38-
for i := 0; i < 10; i++ {
39-
time.Sleep(100 * time.Millisecond)
40-
datach <- fmt.Sprintf("%d -> actual time is %v", i, time.Now())
44+
for i := 0; i < 6; i++ {
45+
time.Sleep(200 * time.Millisecond)
46+
datach <- gs.SseEvent{
47+
Name: "date",
48+
Data: fmt.Sprintf("time: %v", time.Now()),
49+
}
4150
}
42-
43-
close(closech)
4451
})
4552
})
4653

47-
r.Start(":80")
54+
datach := make(chan interface{})
55+
closech := make(chan struct{})
56+
57+
app.Sse("/ssetoo", datach, closech, func() {
58+
defer close(closech)
59+
datach <- "hello world"
60+
61+
for i := 0; i < 6; i++ {
62+
time.Sleep(200 * time.Millisecond)
63+
datach <- gs.SseEvent{
64+
Name: "date",
65+
Data: fmt.Sprintf("time: %v", time.Now()),
66+
}
67+
}
68+
})
69+
70+
app.Start(":80")
4871
}
4972
```
50-
### pretty simple syntax
51-
the syntax is like popular framworks like fiber, gin, echo
73+
### Pretty simple syntax
74+
The syntax is really easy to remember / use if you are beggining with golang framworks and if you already know all this (useless) framworking stuff, it's like most popular framworks (fiber, gin, echo, ...) so you won't be lost!
5275
```golang
5376
func main() {
54-
r := gosvelt.New()
77+
app := gosvelt.New(
78+
gs.WithHttp2,
79+
)
5580

56-
r.Get("/gg/:name", func(c *gosvelt.Context) error { // url params
81+
app.Get("/gg/:name", func(c *gosvelt.Context) error { // url params
5782
return c.Json(200, gosvelt.Map{"gg": c.Param("name")})
5883
})
5984

60-
r.Get("/ws", func(c *gosvelt.Context) error { // websocket handler
85+
app.Get("/ws", func(c *gosvelt.Context) error { // websocket handler
6186
return c.Ws(func(conn *websocket.Conn) {
6287
conn.WriteJSON(gosvelt.Map{"ez": "pz"})
6388
})
6489
})
6590

66-
r.Static("/index", "assets/index.html") // static files
91+
app.Static("/index", "assets/index.html") // static files
6792

68-
r.Svelte("/", "views/App.svelte", // svelte page handler (runtime compiled)
93+
app.Svelte("/", "views/App.svelte", // svelte page handler (runtime compiled)
6994
func(c *gs.Context, svelte gs.Map) error {
7095
return c.Html(200, "assets/index.html", svelte)
7196
},
7297
)
7398

74-
r.Start(":80")
99+
app.Start(":80")
75100
}
76101
```
77-
## todo:
102+
## Todo:
78103
- [ ] error handler panic issue
79104
- [ ] new gosvelt config options
80105
- [ ] live reload

example/main.go

+44-28
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,44 @@ import (
99
"github.com/valyala/fasthttp"
1010
)
1111

12-
func main() {
13-
r := gs.New(&gs.Config{
14-
Log: true,
15-
Http2: false,
16-
ErrorHandler: func(c *fasthttp.RequestCtx, err error) {
17-
statusCode := 500
18-
19-
respBytes, err := json.Marshal(struct {
20-
Status int `json:"status"`
21-
Error string `json:"error"`
22-
}{
23-
Status: statusCode,
24-
Error: err.Error(),
25-
})
26-
if err != nil {
27-
return
28-
}
12+
func ErrorHandler(c *fasthttp.RequestCtx, err error) {
13+
statusCode := 500
2914

30-
c.SetBody(respBytes)
31-
c.SetStatusCode(statusCode)
32-
},
15+
respBytes, err := json.Marshal(struct {
16+
Status int `json:"status"`
17+
Error string `json:"error"`
18+
}{
19+
Status: statusCode,
20+
Error: err.Error(),
3321
})
22+
if err != nil {
23+
return
24+
}
25+
26+
c.SetBody(respBytes)
27+
c.SetStatusCode(statusCode)
28+
}
29+
30+
func main() {
31+
app := gs.New(
32+
gs.WithLog,
33+
gs.WithErrorHandler(ErrorHandler),
34+
)
3435

35-
r.SvelteMiddleware("/", func(next gs.SvelteHandlerFunc) gs.SvelteHandlerFunc {
36+
app.SvelteMiddleware("/", func(next gs.SvelteHandlerFunc) gs.SvelteHandlerFunc {
3637
return func(c *gs.Context, svelte gs.Map) error {
3738
return next(c, svelte)
3839
}
3940
})
4041

41-
r.Static("/svelte_logo", "assets/svelte_logo.svg")
42+
app.Static("/svelte_logo", "assets/svelte_logo.svg")
4243

43-
r.Get("/sse", func(c *gs.Context) error {
44+
app.Get("/sse", func(c *gs.Context) error {
4445
datach := make(chan interface{})
4546
closech := make(chan struct{})
4647

4748
return c.Sse(datach, closech, func() {
49+
defer close(closech)
4850
datach <- "hello world"
4951

5052
for i := 0; i < 6; i++ {
@@ -54,19 +56,33 @@ func main() {
5456
Data: fmt.Sprintf("time: %v", time.Now()),
5557
}
5658
}
57-
58-
close(closech)
5959
})
6060
})
6161

62-
r.Svelte("/ssepage", "views/sse/App.svelte",
62+
datach := make(chan interface{})
63+
closech := make(chan struct{})
64+
65+
app.Sse("/ssetoo", datach, closech, func() {
66+
defer close(closech)
67+
datach <- "hello world"
68+
69+
for i := 0; i < 6; i++ {
70+
time.Sleep(200 * time.Millisecond)
71+
datach <- gs.SseEvent{
72+
Name: "date",
73+
Data: fmt.Sprintf("time: %v", time.Now()),
74+
}
75+
}
76+
})
77+
78+
app.Svelte("/ssepage", "views/sse/App.svelte",
6379
func(c *gs.Context, svelte gs.Map) error {
6480
return c.Html(200, "assets/index.html", svelte)
6581
},
6682
gs.WithPackageManager("pnpm"),
6783
)
6884

69-
r.Svelte("/", "App.svelte",
85+
app.Svelte("/", "App.svelte",
7086
func(c *gs.Context, svelte gs.Map) error {
7187
return c.Html(200, "assets/index.html", svelte)
7288
},
@@ -75,5 +91,5 @@ func main() {
7591
gs.WithRoot("views"),
7692
)
7793

78-
r.Start(":8080")
94+
app.Start(":8080")
7995
}

0 commit comments

Comments
 (0)