Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Mar 8, 2024
1 parent 8474063 commit d0e41a9
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 67 deletions.
89 changes: 41 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ yap - Yet Another Go/Go+ HTTP Web Framework

This repo contains three [Go+ classfiles](https://github.com/goplus/gop/blob/main/doc/classfile.md): `yap` (a HTTP Web Framework), `yaptest` (a HTTP Test Framework) and `ydb` (a Go+ Database Framework).

The classfile `yap` has the file suffix `_yap.gox`. The classfile `yaptest` has the file suffix `_ytest.gox`. And the classfile `ydb` has the file suffix `_ydb.gox`.
The classfile `yap` has the file suffix `.yap`. The classfile `yaptest` has the file suffix `_ytest.gox`. And the classfile `ydb` has the file suffix `_ydb.gox`.

Before using `yap`, `yaptest` or `ydb`, you need to add `github.com/goplus/yap` to `go.mod`:

```sh
gop get github.com/goplus/yap@latest
```

For more details, see [YAP Web Framework Manual](doc/manual.md).
For more details, see [YAP Framework Manual](doc/manual.md).

### How to use in Go+

Expand All @@ -33,90 +33,82 @@ Then we have it reference a classfile called `yap` as the HTTP Web Framework:
gop get github.com/goplus/yap@latest
```

We can use it to implement a static file server:
Create a file named [get.yap](demo/classfile2_hello/get.yap) with the following content:

```coffee
static "/foo", FS("public")
static "/" # Equivalent to static "/", FS("static")

run ":8080"
```

We can also add the ability to handle dynamic GET/POST requests:

```coffee
static "/foo", FS("public")
static "/" # Equivalent to static "/", FS("static")

get "/p/:id", ctx => {
ctx.json {
"id": ctx.param("id"),
}
}

run ":8080"
html `<html><body>Hello, YAP!</body></html>`
```

Save this code to `hello_yap.gox` file and execute:
Execute the following commands:

```sh
mkdir -p yap/static yap/public # Static resources can be placed in these directories
gop mod tidy
gop run .
```

A simplest web program is running now. At this time, if you visit http://localhost:8080/p/123, you will get:
A simplest web program is running now. At this time, if you visit http://localhost:8080, you will get:

```
{"id":"123"}
Hello, YAP!
```


### yap: HTTP Web Framework

This classfile has the file suffix `_yap.gox`.
This classfile has the file suffix `.yap`.


#### Router and Parameters

demo in Go+ classfile ([hello_yap.gox](demo/classfile_hello/hello_yap.gox)):
YAP uses filenames to define routes. `get.yap`'s route is `get "/"` (GET homepage), and `get_p_#id.yap`'s route is `get "/p/:id"` (In fact, the filename can also be `get_p_:id.yap`, but it is not recommended because `:` is not allowed to exist in filenames under Windows).

Let's create a file named [get_p_#id.yap](demo/classfile2_hello/get_p_%23id.yap) with the following content:

```coffee
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>`
json {
"id": param("id"),
}
```

run ":8080"
Execute `gop run .` and visit http://localhost:8080/p/123, you will get:

```
{"id": "123"}
```

#### Static files

Static files server demo in Go+ classfile ([staticfile_yap.gox](demo/classfile_static/staticfile_yap.gox)):
#### YAP Template

In most cases, we will not use the `html` directive to return a html page, but use the yap template. See [get_p_#id.yap](demo/classfile2_blog/get_p_%23id.yap):

```coffee
static "/foo", FS("public")
static "/"
yap "article", {
"id": param("id"),
}
```

run ":8080"

### Run at specified address

By default the YAP server runs on `localhost:8080`, but you can change it in [main.yap](demo/classfile2_blog/main.yap) file:

```coffee
run ":8888"
```

#### YAP Template

demo in Go+ classfile ([blog_yap.gox](demo/classfile_blog/blog_yap.gox), [article_yap.html](demo/classfile_blog/yap/article_yap.html)):
#### Static files

Static files server demo ([main.yap](demo/classfile2_static/main.yap)):

```coffee
get "/p/:id", ctx => {
ctx.yap "article", {
"id": ctx.param("id"),
}
}
static "/foo", FS("public")
static "/"

run ":8080"
```


### yaptest: HTTP Test Framework

This classfile has the file suffix `_ytest.gox`.
Expand Down Expand Up @@ -169,6 +161,7 @@ The directive `testServer` creates the `foo` server by [net/http/httptest](https

For more details, see [yaptest - Go+ HTTP Test Framework](ytest).


### ydb: Database Framework

This classfile has the file suffix `_ydb.gox`.
Expand Down
2 changes: 1 addition & 1 deletion classfile_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ func Gopt_AppV2_Main(app AppType, handlers ...iHandler) {
if me, ok := app.(interface{ MainEntry() }); ok {
me.MainEntry()
} else {
app.Run(":8080")
app.Run("localhost:8080")
}
}
1 change: 1 addition & 0 deletions demo/classfile2_blog/get.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
html `<html><body>Hello, <a href="/p/123">YAP</a>!</body></html>`
26 changes: 24 additions & 2 deletions demo/classfile2_blog/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions demo/classfile2_blog/main.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run ":8888"
2 changes: 1 addition & 1 deletion demo/classfile2_hello/get.yap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
html `<html><body>Hello, <a href="/p/123">Yap</a>!</body></html>`
html `<html><body>Hello, YAP!</body></html>`
2 changes: 1 addition & 1 deletion demo/classfile2_hello/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion demo/classfile_blog/blog_yap.gox
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ get "/p/:id", ctx => {
"id": ctx.param("id"),
}
}
get "/", ctx => {
ctx.html `<html><body>Hello, <a href="/p/123">YAP</a>!</body></html>`
}

run ":8080"
run ":8888"
7 changes: 6 additions & 1 deletion demo/classfile_blog/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions demo/classfile_hello/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions demo/classfile_hello/main.yap
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
get "/", ctx => {
ctx.html `<html><body>Hello, YAP!</body></html>`
}
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"
run "localhost:8080"
7 changes: 6 additions & 1 deletion yap.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ func (p *Engine) Handler(mws ...func(h http.Handler) http.Handler) http.Handler
// Accepted connections are configured to enable TCP keep-alives.
func (p *Engine) Run(addr string, mws ...func(h http.Handler) http.Handler) error {
h := p.Handler(mws...)
return p.las(addr, h)
log.Println("Listen", addr)
err := p.las(addr, h)
if err != nil {
log.Println(err)
}
return err
}

// SetLAS sets listenAndServe func to listens on the TCP network address addr
Expand Down

0 comments on commit d0e41a9

Please sign in to comment.