Skip to content

Commit

Permalink
Merge pull request #26 from RussellLuo/multi-http-ops
Browse files Browse the repository at this point in the history
annotation: Add support for multiple HTTP operations
  • Loading branch information
RussellLuo authored Feb 26, 2022
2 parents 7a19cca + 5318772 commit 1442bbc
Show file tree
Hide file tree
Showing 16 changed files with 705 additions and 56 deletions.
38 changes: 30 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ See more examples [here](examples).
//kun:op <method> <pattern>
```
If a Go method needs to correspond to more than one URI (or HTTP method), you can specify multiple `//kun:op` directives, which will produce multiple HTTP request operations.
Note that the only differences among these HTTP request operations are the path parameters.
##### Arguments
- **method**: The request method.
Expand All @@ -324,15 +328,33 @@ See more examples [here](examples).
##### Examples
```go
type Service interface {
//kun:op DELETE /users/{id}
DeleteUser(ctx context.Context, id int) (err error)
}
- Single operation:
// HTTP request:
// $ http DELETE /users/101
```
```go
type Service interface {
//kun:op DELETE /users/{id}
DeleteUser(ctx context.Context, id int) (err error)
}
// HTTP request:
// $ http DELETE /users/101
```

- Multiple operations:

```go
type Service interface {
//kun:op GET /messages/{messageID}
//kun:op GET /users/{userID}/messages/{messageID}
GetMessage(ctx context.Context, userID string, messageID string) (text string, err error)
}

// See a runnable example in examples/messaging.

// HTTP request:
// $ http GET /messages/123456
// $ http GET /users/me/messages/123456
```

</details>

Expand Down
33 changes: 33 additions & 0 deletions examples/messaging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# messaging

This example illustrates how to specify multiple HTTP operations.


## Generate the code

```bash
$ go generate
```

## Test the server

Run the server:

```bash
$ go run cmd/main.go
2022/02/26 17:45:20 transport=HTTP addr=:8080
```

Get a message:

```bash
$ curl -XGET 'http://localhost:8080/messages/123456'
{"text":"user[]: message[123456]"}
```

Get a message from a specific user:

```bash
$ curl -XGET 'http://localhost:8080/users/me/messages/123456'
{"text":"user[me]: message[123456]"}
```
35 changes: 35 additions & 0 deletions examples/messaging/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"

"github.com/RussellLuo/kun/examples/messaging"
"github.com/RussellLuo/kun/pkg/httpcodec"
)

func main() {
httpAddr := flag.String("http.addr", ":8080", "HTTP listen address")
flag.Parse()

svc := &messaging.Messaging{}
r := messaging.NewHTTPRouter(svc, httpcodec.NewDefaultCodecs(nil))

errs := make(chan error, 2)
go func() {
log.Printf("transport=HTTP addr=%s\n", *httpAddr)
errs <- http.ListenAndServe(*httpAddr, r)
}()
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()

log.Printf("terminated, err:%v", <-errs)
}
51 changes: 51 additions & 0 deletions examples/messaging/endpoint.go

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

99 changes: 99 additions & 0 deletions examples/messaging/http.go

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

76 changes: 76 additions & 0 deletions examples/messaging/http_client.go

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

Loading

0 comments on commit 1442bbc

Please sign in to comment.