mirror of
https://github.com/fatedier/frp.git
synced 2025-07-27 07:35:07 +00:00
add more e2e test (#2505)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -12,8 +13,9 @@ type Server struct {
|
||||
bindPort int
|
||||
hanlder http.Handler
|
||||
|
||||
l net.Listener
|
||||
hs *http.Server
|
||||
l net.Listener
|
||||
tlsConfig *tls.Config
|
||||
hs *http.Server
|
||||
}
|
||||
|
||||
type Option func(*Server) *Server
|
||||
@@ -43,6 +45,13 @@ func WithBindPort(port int) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithTlsConfig(tlsConfig *tls.Config) Option {
|
||||
return func(s *Server) *Server {
|
||||
s.tlsConfig = tlsConfig
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
func WithHandler(h http.Handler) Option {
|
||||
return func(s *Server) *Server {
|
||||
s.hanlder = h
|
||||
@@ -50,6 +59,15 @@ func WithHandler(h http.Handler) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithResponse(resp []byte) Option {
|
||||
return func(s *Server) *Server {
|
||||
s.hanlder = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(resp)
|
||||
})
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Run() error {
|
||||
if err := s.initListener(); err != nil {
|
||||
return err
|
||||
@@ -57,11 +75,17 @@ func (s *Server) Run() error {
|
||||
|
||||
addr := net.JoinHostPort(s.bindAddr, strconv.Itoa(s.bindPort))
|
||||
hs := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: s.hanlder,
|
||||
Addr: addr,
|
||||
Handler: s.hanlder,
|
||||
TLSConfig: s.tlsConfig,
|
||||
}
|
||||
|
||||
s.hs = hs
|
||||
go hs.Serve(s.l)
|
||||
if s.tlsConfig == nil {
|
||||
go hs.Serve(s.l)
|
||||
} else {
|
||||
go hs.ServeTLS(s.l, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user