1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-01-05 10:20:53 +02:00
go-micro/plugins/server/http
Johnson C a40f6e8fae
[fix] fixing f.IsExported undefined issue (#2382)
IsExported needs go1.17, replace with PkgPath
2021-12-07 17:30:48 +08:00
..
buffer.go add all the plugins 2020-12-26 15:32:45 +00:00
extractor.go [fix] fixing f.IsExported undefined issue (#2382) 2021-12-07 17:30:48 +08:00
go.mod upgrade to go 1.17 (#2346) 2021-11-11 14:03:34 +00:00
go.sum upgrade to go 1.17 (#2346) 2021-11-11 14:03:34 +00:00
handler.go go-micro.dev/v4 (#2305) 2021-10-12 12:55:53 +01:00
http_test.go replace ioutil with io and os (#2327) 2021-10-30 19:24:40 +01:00
http.go go-micro.dev/v4 (#2305) 2021-10-12 12:55:53 +01:00
message.go go-micro.dev/v4 (#2305) 2021-10-12 12:55:53 +01:00
options.go go-micro.dev/v4 (#2305) 2021-10-12 12:55:53 +01:00
README.md go-micro.dev/v4 (#2305) 2021-10-12 12:55:53 +01:00
subscriber.go go-micro.dev/v4 (#2305) 2021-10-12 12:55:53 +01:00

HTTP Server

The HTTP Server is a go-micro.Server. It's a partial implementation which strips out codecs, transports, etc but enables you to create a HTTP Server that could potentially be used for REST based API services.

Usage

import (
	"net/http"

	"github.com/micro/go-micro/server"
	httpServer "github.com/asim/go-micro/plugins/server/http"
)

func main() {
	srv := httpServer.NewServer(
		server.Name("helloworld"),
	)

	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`hello world`))
	})

	hd := srv.NewHandler(mux)

	srv.Handle(hd)
	srv.Start()
	srv.Register()
}

Or as part of a service

import (
	"net/http"

	"go-micro.dev/v4"
	"github.com/micro/go-micro/server"
	httpServer "github.com/asim/go-micro/plugins/server/http"
)

func main() {
	srv := httpServer.NewServer(
		server.Name("helloworld"),
	)

	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`hello world`))
	})

	hd := srv.NewHandler(mux)

	srv.Handle(hd)

	service := micro.NewService(
		micro.Server(srv),
	)
	service.Init()
	service.Run()
}