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

     1  // +build main2
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"net"
     8  	"net/http"
     9  	"os"
    10  	"os/signal"
    11  	"syscall"
    12  
    13  	"github.com/micro/go-micro/v2/registry"
    14  	"github.com/pborman/uuid"
    15  )
    16  
    17  var (
    18  	service = &registry.Service{
    19  		Name: "go.micro.srv.greeter",
    20  		Nodes: []*registry.Node{
    21  			{
    22  				Id:      "go.micro.srv.greeter-" + uuid.NewUUID().String(),
    23  				Address: "localhost",
    24  				Port:    4000,
    25  			},
    26  		},
    27  	}
    28  )
    29  
    30  func main() {
    31  	l, err := net.Listen("tcp", "localhost:4000")
    32  	if err != nil {
    33  		fmt.Println(err)
    34  	}
    35  
    36  	http.HandleFunc("/greeter", func(w http.ResponseWriter, r *http.Request) {
    37  		r.ParseForm()
    38  		fmt.Fprintf(w, "Hello %s!", r.Form.Get("name"))
    39  	})
    40  
    41  	go http.Serve(l, http.DefaultServeMux)
    42  
    43  	register(service)
    44  
    45  	notify := make(chan os.Signal, 1)
    46  	signal.Notify(notify, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
    47  	<-notify
    48  
    49  	deregister(service)
    50  	l.Close()
    51  }