mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-06-12 13:21:03 +08:00
【feat】添加http设备接入,优化iothub结构
This commit is contained in:
66
iothub/server/httpserver/hook.go
Normal file
66
iothub/server/httpserver/hook.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/emicklei/go-restful/v3"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"pandax/iothub/hook_message_work"
|
||||
"pandax/pkg/global"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HookHttpService struct {
|
||||
HookService *hook_message_work.HookService
|
||||
}
|
||||
|
||||
func InitHttpHook(addr string, hs *hook_message_work.HookService) {
|
||||
server := NewHttpServer(addr)
|
||||
service := NewHookHttpService(hs)
|
||||
container := server.Container
|
||||
ws := new(restful.WebService)
|
||||
ws.Path("/api/v1").Produces(restful.MIME_JSON)
|
||||
ws.Route(ws.POST("/{token}/{pathType}").Filter(basicAuthenticate).To(service.hook))
|
||||
container.Add(ws)
|
||||
|
||||
server.srv.ConnState = func(conn net.Conn, state http.ConnState) {
|
||||
switch state {
|
||||
case http.StateNew:
|
||||
log.Println("New connection", conn.RemoteAddr())
|
||||
case http.StateActive:
|
||||
log.Println("Connection active", conn.RemoteAddr())
|
||||
case http.StateIdle:
|
||||
log.Println("Connection idle", conn.RemoteAddr())
|
||||
case http.StateHijacked, http.StateClosed:
|
||||
log.Println("Connection closed", conn.RemoteAddr())
|
||||
}
|
||||
}
|
||||
err := server.Start(context.TODO())
|
||||
if err != nil {
|
||||
global.Log.Error("IOTHUB HTTP服务启动错误", err)
|
||||
} else {
|
||||
global.Log.Infof("IOTHUB HOOK Start SUCCESS,Grpc Server listen: %s", addr)
|
||||
}
|
||||
}
|
||||
|
||||
func NewHookHttpService(hs *hook_message_work.HookService) *HookHttpService {
|
||||
hhs := &HookHttpService{
|
||||
HookService: hs,
|
||||
}
|
||||
return hhs
|
||||
}
|
||||
|
||||
// 获取token进行认证
|
||||
func basicAuthenticate(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
|
||||
path := req.Request.URL.Path
|
||||
log.Println(path)
|
||||
split := strings.Split(path, "/")
|
||||
log.Println(split)
|
||||
chain.ProcessFilter(req, resp)
|
||||
}
|
||||
|
||||
func (hhs *HookHttpService) hook(req *restful.Request, resp *restful.Response) {
|
||||
io.WriteString(resp, "42")
|
||||
}
|
||||
62
iothub/server/httpserver/http_server.go
Normal file
62
iothub/server/httpserver/http_server.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/emicklei/go-restful/v3"
|
||||
"net/http"
|
||||
"pandax/pkg/global"
|
||||
)
|
||||
|
||||
const DefaultPort = ":9002"
|
||||
|
||||
type HttpServer struct {
|
||||
Addr string
|
||||
srv *http.Server
|
||||
Container *restful.Container
|
||||
}
|
||||
|
||||
func NewHttpServer(addr string) *HttpServer {
|
||||
if addr == "" {
|
||||
addr = DefaultPort
|
||||
}
|
||||
c := restful.NewContainer()
|
||||
c.EnableContentEncoding(true)
|
||||
return &HttpServer{
|
||||
Addr: addr,
|
||||
Container: c,
|
||||
srv: &http.Server{
|
||||
Addr: addr,
|
||||
Handler: c,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *HttpServer) GetServe() *http.Server {
|
||||
return s.srv
|
||||
}
|
||||
|
||||
func (s *HttpServer) Type() string {
|
||||
return "HTTP"
|
||||
}
|
||||
|
||||
func (s *HttpServer) Start(ctx context.Context) error {
|
||||
go func() {
|
||||
if global.Conf.Server.Tls.Enable {
|
||||
global.Log.Infof("HTTPS Server listen: %s", s.Addr)
|
||||
if err := s.srv.ListenAndServeTLS(global.Conf.Server.Tls.CertFile, global.Conf.Server.Tls.KeyFile); err != nil {
|
||||
global.Log.Errorf("error http serve: %s", err)
|
||||
}
|
||||
} else {
|
||||
global.Log.Infof("HTTP Server listen: %s", s.Addr)
|
||||
if err := s.srv.ListenAndServe(); err != nil {
|
||||
global.Log.Errorf("error http serve: %s", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *HttpServer) Stop(ctx context.Context) error {
|
||||
s.srv.Shutdown(ctx)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user