2023-12-07 23:34:34 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2023-12-08 22:02:36 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"bufio"
|
|
|
|
"io"
|
2023-12-07 23:34:34 +08:00
|
|
|
|
2023-12-10 19:26:09 +08:00
|
|
|
"git.pyer.club/kingecg/gologger"
|
2023-12-07 23:34:34 +08:00
|
|
|
logger "git.pyer.club/kingecg/gologger"
|
|
|
|
"github.com/soheilhy/cmux"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ServerManager map[string]*ServerListener = make(map[string]*ServerListener)
|
|
|
|
|
2023-12-10 01:08:39 +08:00
|
|
|
func makeMatcher(name string, s *ServerListener) cmux.Matcher {
|
2023-12-08 22:02:36 +08:00
|
|
|
return func(r io.Reader) bool {
|
2023-12-10 01:08:39 +08:00
|
|
|
if s.ServerCount() == 1 {
|
|
|
|
return true
|
|
|
|
}
|
2023-12-08 22:02:36 +08:00
|
|
|
req, err := http.ReadRequest(bufio.NewReader(r))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.HasPrefix(req.Host, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 23:34:34 +08:00
|
|
|
type ServerListener struct {
|
2023-12-10 19:26:09 +08:00
|
|
|
port int
|
2023-12-07 23:34:34 +08:00
|
|
|
listener cmux.CMux
|
|
|
|
servers map[string]*http.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerListener) AddServer(name string, server *http.Server) {
|
|
|
|
s.servers[name] = server
|
|
|
|
}
|
|
|
|
|
2023-12-10 01:08:39 +08:00
|
|
|
func (s *ServerListener) ServerCount() int {
|
|
|
|
return len(s.servers)
|
|
|
|
}
|
|
|
|
|
2023-12-07 23:34:34 +08:00
|
|
|
func (s *ServerListener) StartServer(name string) error {
|
|
|
|
server, ok := s.servers[name]
|
|
|
|
if !ok {
|
|
|
|
panic("No named server")
|
|
|
|
}
|
|
|
|
|
2023-12-10 01:08:39 +08:00
|
|
|
l := s.listener.Match(makeMatcher(name, s))
|
2023-12-07 23:34:34 +08:00
|
|
|
return server.Serve(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerListener) Start() {
|
|
|
|
for name, _ := range s.servers {
|
2023-12-08 22:02:36 +08:00
|
|
|
go func(name string) {
|
|
|
|
err := s.StartServer(name)
|
|
|
|
if err != nil {
|
|
|
|
l := logger.GetLogger("ServerListener")
|
|
|
|
l.Error("Start server error:", name, err)
|
|
|
|
}
|
|
|
|
}(name)
|
2023-12-07 23:34:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerListener) ShutDown() {
|
|
|
|
// may need add wait group
|
|
|
|
for _, server := range s.servers {
|
|
|
|
server.Shutdown(context.Background())
|
|
|
|
}
|
2023-12-10 01:08:39 +08:00
|
|
|
s.listener.Close()
|
2023-12-07 23:34:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerListener) Serve() {
|
2023-12-10 19:26:09 +08:00
|
|
|
l := gologger.GetLogger("Listener")
|
|
|
|
l.Debug("listen on :", s.port)
|
2023-12-07 23:34:34 +08:00
|
|
|
go s.listener.Serve()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServerListener(port int) *ServerListener {
|
|
|
|
listenStr := fmt.Sprintf(":%d", port)
|
|
|
|
l, err := net.Listen("tcp", listenStr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
l := logger.GetLogger("ServerListener")
|
|
|
|
l.Error("Listen error:", err)
|
|
|
|
}
|
|
|
|
muxer := cmux.New(l)
|
2023-12-10 19:26:09 +08:00
|
|
|
s := &ServerListener{port: port, listener: muxer, servers: make(map[string]*http.Server)}
|
2023-12-07 23:34:34 +08:00
|
|
|
return s
|
|
|
|
}
|
2023-12-08 22:02:36 +08:00
|
|
|
|
|
|
|
func AddServer(name string, server *http.Server, port int) {
|
|
|
|
listenStr := fmt.Sprintf(":%d", port)
|
|
|
|
listener, ok := ServerManager[listenStr]
|
|
|
|
if !ok {
|
|
|
|
listener = NewServerListener(port)
|
|
|
|
ServerManager[listenStr] = listener
|
|
|
|
}
|
|
|
|
listener.AddServer(name, server)
|
|
|
|
}
|