2016-06-30 20:21:57 +01:00
|
|
|
# HTTP Server
|
|
|
|
|
2020-10-10 00:38:35 +03:00
|
|
|
The HTTP Server is a go-micro.Server. It's a partial implementation which strips out codecs, transports, etc but enables you
|
2016-06-30 20:21:57 +01:00
|
|
|
to create a HTTP Server that could potentially be used for REST based API services.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```go
|
|
|
|
import (
|
2016-06-30 20:24:43 +01:00
|
|
|
"net/http"
|
2016-06-30 20:21:57 +01:00
|
|
|
|
2023-04-28 21:59:31 +03:00
|
|
|
"go.unistack.org/micro/v4/server"
|
|
|
|
httpServer "go.unistack.org/micro-server-http/v4"
|
2016-06-30 20:21:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-06-30 20:23:53 +01:00
|
|
|
srv := httpServer.NewServer(
|
|
|
|
server.Name("helloworld"),
|
|
|
|
)
|
2016-06-30 20:21:57 +01:00
|
|
|
|
2016-06-30 20:23:53 +01:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(`hello world`))
|
|
|
|
})
|
2016-06-30 20:21:57 +01:00
|
|
|
|
2016-06-30 20:23:53 +01:00
|
|
|
hd := srv.NewHandler(mux)
|
2016-06-30 20:21:57 +01:00
|
|
|
|
2016-06-30 20:23:53 +01:00
|
|
|
srv.Handle(hd)
|
2016-06-30 20:21:57 +01:00
|
|
|
srv.Start()
|
|
|
|
srv.Register()
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Or as part of a service
|
|
|
|
|
|
|
|
```go
|
|
|
|
import (
|
2016-06-30 20:24:43 +01:00
|
|
|
"net/http"
|
2016-06-30 20:21:57 +01:00
|
|
|
|
2023-04-28 21:59:31 +03:00
|
|
|
"go.unistack.org/micro/v4"
|
|
|
|
"go.unistack.org/micro/v4/server"
|
|
|
|
httpServer "go.unistack.org/micro-server-http/v4"
|
2016-06-30 20:21:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-06-30 20:23:53 +01:00
|
|
|
srv := httpServer.NewServer(
|
|
|
|
server.Name("helloworld"),
|
|
|
|
)
|
2016-06-30 20:21:57 +01:00
|
|
|
|
2016-06-30 20:23:53 +01:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(`hello world`))
|
|
|
|
})
|
2016-06-30 20:21:57 +01:00
|
|
|
|
2016-06-30 20:23:53 +01:00
|
|
|
hd := srv.NewHandler(mux)
|
2016-06-30 20:21:57 +01:00
|
|
|
|
2016-06-30 20:23:53 +01:00
|
|
|
srv.Handle(hd)
|
2016-06-30 20:21:57 +01:00
|
|
|
|
2016-06-30 20:23:53 +01:00
|
|
|
service := micro.NewService(
|
|
|
|
micro.Server(srv),
|
|
|
|
)
|
2016-06-30 20:21:57 +01:00
|
|
|
service.Init()
|
2016-06-30 20:23:53 +01:00
|
|
|
service.Run()
|
2016-06-30 20:21:57 +01:00
|
|
|
}
|
|
|
|
```
|