Skip to content

Commit

Permalink
Add redirect example
Browse files Browse the repository at this point in the history
  • Loading branch information
Asim Aslam committed Jan 6, 2017
1 parent a2d88de commit cce485b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ Use flag `--registry=mdns`
- [sidecar](sidecar) - Greeter service using the sidecar with multiple languages
- [booking](booking) - A booking.com demo application
- [plugins](plugins) - How to use plugins
- [template](template) - Api, web and srv service templates generated with the 'micro new' command
- [broker](broker) - A example of using Broker for Publish and Subscribing.
- [client](client) - Usage of the Client package to call a service.
- [redirect](redirect) - An example of how to http redirect using an API service
- [server](server) - Use of the Server package directly to server requests.
- [service](service) - Example of the top level Service in go-micro.
- [template](template) - Api, web and srv service templates generated with the 'micro new' command
- [grpc](grpc) - Examples of how to use [go-grpc](https://github.com/micro/go-grpc)

## External
Expand Down
29 changes: 29 additions & 0 deletions redirect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Redirect

This demonstrates how to http redirect using an API service

## Usage

Run the micro API

```
micro --registry=mdns api
```

Run the redirect API

```
go run main.go --registry=mdns
```

Make request
```
curl -v http://localhost:8080/redirect/url
```

Should return

```
HTTP/1.1 301 Moved Permanently
Location: https://google.com
```
39 changes: 39 additions & 0 deletions redirect/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"log"

"github.com/micro/go-micro"
api "github.com/micro/micro/api/proto"
"golang.org/x/net/context"
)

type Redirect struct {}

func (r *Redirect) Url(ctx context.Context, req *api.Request, rsp *api.Response) error {
rsp.StatusCode = int32(301)
rsp.Header = map[string]*api.Pair{
"Location": &api.Pair{
Key: "Location",
Values: []string{"https://google.com"},
},
}
return nil
}

func main() {
service := micro.NewService(
micro.Name("go.micro.api.redirect"),
)

// parse command line flags
service.Init()

service.Server().Handle(
service.Server().NewHandler(new(Redirect)),
)

if err := service.Run(); err != nil {
log.Fatal(err)
}
}

0 comments on commit cce485b

Please sign in to comment.