1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/plugins/server/http
2021-02-17 18:20:06 +00:00
..
buffer.go add all the plugins 2020-12-26 15:32:45 +00:00
extractor.go update v3 plugins (#2105) 2021-01-20 21:01:10 +00:00
go.mod update plugins version & remove replace (#2118) 2021-02-05 09:09:25 +00:00
go.sum update v3 plugins (#2105) 2021-01-20 21:01:10 +00:00
handler.go update v3 plugins (#2105) 2021-01-20 21:01:10 +00:00
http_test.go update v3 plugins (#2105) 2021-01-20 21:01:10 +00:00
http.go support for tls on http plugin (#2126) 2021-02-17 18:20:06 +00:00
message.go update v3 plugins (#2105) 2021-01-20 21:01:10 +00:00
options.go update v3 plugins (#2105) 2021-01-20 21:01:10 +00:00
README.md update v3 plugins (#2105) 2021-01-20 21:01:10 +00:00
subscriber.go update v3 plugins (#2105) 2021-01-20 21:01:10 +00: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"

	"github.com/asim/go-micro/v3"
	"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()
}