-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Asim Aslam
committed
Jan 6, 2017
1 parent
a2d88de
commit cce485b
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |