Skip to content

Commit

Permalink
classfile demo
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jan 7, 2024
1 parent 23136e4 commit 9653d40
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 29 deletions.
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)):
demo in Go ([blog.go](demo/blog/blog.go)):

```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)):

```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"

0 comments on commit 9653d40

Please sign in to comment.