github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/redirect/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  
     6  	"context"
     7  	"github.com/micro/go-micro/v2"
     8  	api "github.com/micro/micro/v2/api/proto"
     9  )
    10  
    11  type Redirect struct{}
    12  
    13  func (r *Redirect) Url(ctx context.Context, req *api.Request, rsp *api.Response) error {
    14  	rsp.StatusCode = int32(301)
    15  	rsp.Header = map[string]*api.Pair{
    16  		"Location": &api.Pair{
    17  			Key:    "Location",
    18  			Values: []string{"https://google.com"},
    19  		},
    20  	}
    21  	return nil
    22  }
    23  
    24  func main() {
    25  	service := micro.NewService(
    26  		micro.Name("go.micro.api.redirect"),
    27  	)
    28  
    29  	// parse command line flags
    30  	service.Init()
    31  
    32  	service.Server().Handle(
    33  		service.Server().NewHandler(new(Redirect)),
    34  	)
    35  
    36  	if err := service.Run(); err != nil {
    37  		log.Fatal(err)
    38  	}
    39  }