Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

classfile demo #10

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yap - Yet Another Go/Go+ HTTP Web Framework

### Router and Parameters

demo ([hello.go](demo/hello/hello.go)):
demo in Go ([hello.go](demo/hello/hello.go)):

```go
import "github.com/goplus/yap"
Expand All @@ -26,33 +26,51 @@ y.Handle("/", func(ctx *yap.Context) {
y.Run(":8080")
```

demo in Go+ classfile ([hello_yap.gox](demo/classfile_hello/hello_yap.gox)):

```go
get "/p/:id", ctx => {
ctx.json {
"id": ctx.param("id"),
}
}
handle "/", ctx => {
ctx.html `<html><body>Hello, <a href="/p/123">Yap</a>!</body></html>`
}

run ":8080"
```

### YAP Template

demo ([blog.go](demo/blog/blog.go), [article.yap](demo/blog/yap/article.yap)):
demo in Go ([blog.go](demo/blog/blog.go), [article.yap](demo/blog/yap/article.yap)):

```go
import (
"embed"
"io/fs"
"os"

"github.com/goplus/yap"
)

type article struct {
ID string
}

//go:embed yap
var yapFS embed.FS

fsYap, _ := fs.Sub(yapFS, "yap")
y := yap.New(fsYap)
y := yap.New(os.DirFS("."))

y.GET("/p/:id", func(ctx *yap.Context) {
ctx.YAP(200, "article", article{
ID: ctx.Param("id"),
ctx.YAP(200, "article", yap.H{
"id": ctx.Param("id"),
})
})

y.Run(":8080")
```

demo in Go+ classfile ([blog_yap.gox](demo/classfile_blog/blog_yap.gox), [article.yap](demo/classfile_blog/yap/article.yap)):

```go
get "/p/:id", ctx => {
ctx.yap "article", {
"id": ctx.param("id"),
}
}

run ":8080"
```
17 changes: 4 additions & 13 deletions demo/blog/blog.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
package main

import (
"embed"
"io/fs"
"os"

"github.com/goplus/yap"
)

type article struct {
ID string
}

//go:embed yap
var yapFS embed.FS

func main() {
fsYap, _ := fs.Sub(yapFS, "yap")
y := yap.New(fsYap)
y := yap.New(os.DirFS("."))

y.GET("/p/:id", func(ctx *yap.Context) {
ctx.YAP(200, "article", article{
ID: ctx.Param("id"),
ctx.YAP(200, "article", yap.H{
"id": ctx.Param("id"),
})
})

Expand Down
2 changes: 1 addition & 1 deletion demo/blog/yap/article.yap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<meta charset="utf-8"/>
</head>
<body>
Article {{.ID}}
Article {{.id}}
</body>
</html>
28 changes: 28 additions & 0 deletions demo/blog_emb/blog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"embed"
"io/fs"

"github.com/goplus/yap"
)

type article struct {
ID string
}

//go:embed yap
var yapFS embed.FS

func main() {
fsYap, _ := fs.Sub(yapFS, "yap")
y := yap.New(fsYap)

y.GET("/p/:id", func(ctx *yap.Context) {
ctx.YAP(200, "article", article{
ID: ctx.Param("id"),
})
})

y.Run(":8080")
}
8 changes: 8 additions & 0 deletions demo/blog_emb/yap/article.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
Article {{.ID}}
</body>
</html>
25 changes: 25 additions & 0 deletions demo/classfile_hello/gop_autogen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "github.com/goplus/yap"

type hello_yap struct {
yap.App
}
//line demo/classfile_hello/hello_yap.gox:1
func (this *hello_yap) MainEntry() {
//line demo/classfile_hello/hello_yap.gox:1:1
this.Get("/p/:id", func(ctx *yap.Context) {
//line demo/classfile_hello/hello_yap.gox:2:1
ctx.Json__1(map[string]string{"id": ctx.Param("id")})
})
//line demo/classfile_hello/hello_yap.gox:6:1
this.Handle("/", func(ctx *yap.Context) {
//line demo/classfile_hello/hello_yap.gox:7:1
ctx.Html__1(`<html><body>Hello, <a href="/p/123">Yap</a>!</body></html>`)
})
//line demo/classfile_hello/hello_yap.gox:10:1
this.Run__1(":8080")
}
func main() {
yap.Gopt_App_Main(new(hello_yap))
}
10 changes: 10 additions & 0 deletions demo/classfile_hello/hello_yap.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
get "/p/:id", ctx => {
ctx.json {
"id": ctx.param("id"),
}
}
handle "/", ctx => {
ctx.html `<html><body>Hello, <a href="/p/123">Yap</a>!</body></html>`
}

run ":8080"
Loading