mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.3 KiB
68 lines
1.3 KiB
// Copyright 2023 ROC. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"connectrpc.com/connect"
|
|
hx "github.com/rocboss/paopao-ce/pkg/http"
|
|
)
|
|
|
|
var (
|
|
_ server = (*connectServer)(nil)
|
|
)
|
|
|
|
// connectServer wraper for connect
|
|
type connectServer struct {
|
|
*baseServer
|
|
|
|
isTLS bool
|
|
certFile string
|
|
keyFile string
|
|
handlerOpts []connect.HandlerOption
|
|
server *http.Server
|
|
mux connectMux
|
|
}
|
|
|
|
type connectMux interface {
|
|
http.Handler
|
|
Handle(string, http.Handler)
|
|
}
|
|
|
|
func (s *connectServer) start() error {
|
|
s.server.Handler = s.mux
|
|
if s.isTLS {
|
|
return s.server.ListenAndServeTLS(s.certFile, s.keyFile)
|
|
} else {
|
|
return s.server.ListenAndServe()
|
|
}
|
|
}
|
|
|
|
func (s *connectServer) stop() error {
|
|
return s.server.Shutdown(context.Background())
|
|
}
|
|
|
|
func (s *connectServer) register(path string, handler http.Handler) {
|
|
s.mux.Handle(path, handler)
|
|
}
|
|
|
|
func defaultConnectServer(addr string) (s *connectServer) {
|
|
s = &connectServer{
|
|
baseServer: newBaseServe(),
|
|
server: &http.Server{
|
|
Addr: addr,
|
|
},
|
|
mux: &http.ServeMux{},
|
|
}
|
|
// TODO: custom value from config
|
|
var useConnectMux bool
|
|
if useConnectMux {
|
|
s.mux = hx.NewConnectMux()
|
|
}
|
|
return
|
|
}
|