Based on thenet/httpThe library listens for routes
// @author Walker
// @time 2020/11/13 15:22
package main

import (
	"github.com/micro/go-micro/web"
	"net/http"
)

func main(a) {
	s := web.NewService(
		web.Address(": 8001"),
	)

	s.HandleFunc("/".func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("simple server ..."))
	})

	s.Run()
}
Copy the code
CURL access after running the service
$curl 127.0.0.1:8001 Simple Server...Copy the code
In production, you can use a WEB framework to implement the above routing, which is used hereGin
// @author Walker
// @time 2020/11/13 15:28
package main

import (
	"github.com/gin-gonic/gin"
	"github.com/micro/go-micro/web"
	"net/http"
)

func main(a) {
	r := gin.Default()
	r.GET("/".func(c *gin.Context) {
		c.String(http.StatusOK, "gin server...")
	})

	s := web.NewService(
		web.Address(": 8001"),
		web.Metadata(map[string]string{"protocol": "http"}),
		web.Handler(r),
	)

	s.Run()
}
Copy the code
Also run the service, CURL access
$curl 127.0.0.1:8001 gin Server...Copy the code