๐พ Archived View for source.community โบ ckaznocha โบ gemini โบ blob โบ main โบ geminitest โบ server.go captured on 2024-05-26 at 15:02:08. Gemini links have been rewritten to link to archived content
โฌ ๏ธ Previous capture (2024-02-05)
-=-=-=-=-=-=-
. ,-. ,-. . . ,-. ,-. ,-. ,-. ,-. ,-,-. ,-,-. . . ,-. . |- . . `-. | | | | | | |-' | | | | | | | | | | | | | | | | | `-' `-' `-^ ' `-' `-' :: `-' `-' ' ' ' ' ' ' `-^ ' ' ' `' `-| /| `-'
git clone https://source.community/ckaznocha/gemini.git
View raw contents of /geminitest/server.go (main)
โโโโโฎ 1โ package geminitest 2โ 3โ import ( 4โ "crypto/tls" 5โ "crypto/x509" 6โ "fmt" 7โ "net" 8โ "sync" 9โ "testing" 10โ 11โ "source.community/ckaznocha/gemini" 12โ ) 13โ 14โ // Server is gemini server listing on a randomly chosen open port. 15โ type Server struct { 16โ Listener net.Listener 17โ TLS *tls.Config 18โ Config *gemini.Server 19โ certificate *x509.Certificate 20โ URL string 21โ Addr string 22โ wg sync.WaitGroup 23โ mu sync.Mutex 24โ closed bool 25โ } 26โ 27โ func newLocalListener() net.Listener { 28โ l, err := net.Listen("tcp", "127.0.0.1:0") 29โ if err != nil { 30โ if l, err = net.Listen("tcp6", "[::1]:0"); err != nil { 31โ panic(fmt.Sprintf("httptest: failed to listen on a port: %v", err)) 32โ } 33โ } 34โ 35โ return l 36โ } 37โ 38โ // NewUnstartedServer returns a server which has not been started. 39โ func NewUnstartedServer(t *testing.T, handler gemini.Handler) *Server { 40โ t.Helper() 41โ 42โ return &Server{ 43โ Listener: newLocalListener(), 44โ Config: &gemini.Server{ 45โ Handler: handler, 46โ LogHandler: func(message string, isError bool) { t.Log(message) }, 47โ }, 48โ } 49โ } 50โ 51โ // Start starts an unstarted server. 52โ func (s *Server) Start() { 53โ if s.URL != "" { 54โ panic("Server already started") 55โ } 56โ 57โ cert, err := tls.LoadX509KeyPair("testdata/cert.pem", "testdata/key.pem") 58โ if err != nil { 59โ panic(fmt.Sprintf("httptest: NewTLSServer: %v", err)) 60โ } 61โ 62โ if existingConfig := s.TLS; existingConfig != nil { 63โ s.TLS = existingConfig.Clone() 64โ } else { 65โ s.TLS = new(tls.Config) 66โ } 67โ 68โ if len(s.TLS.Certificates) == 0 { 69โ s.TLS.Certificates = []tls.Certificate{cert} 70โ } 71โ 72โ s.certificate, err = x509.ParseCertificate(s.TLS.Certificates[0].Certificate[0]) 73โ if err != nil { 74โ panic(fmt.Sprintf("httptest: NewTLSServer: %v", err)) 75โ } 76โ 77โ certpool := x509.NewCertPool() 78โ certpool.AddCert(s.certificate) 79โ 80โ s.Listener = tls.NewListener(s.Listener, s.TLS) 81โ s.URL = "gemini://" + s.Listener.Addr().String() 82โ s.Addr = s.Listener.Addr().String() 83โ s.goServe() 84โ } 85โ 86โ func (s *Server) goServe() { 87โ s.wg.Add(1) 88โ 89โ go func() { 90โ defer s.wg.Done() 91โ 92โ if err := s.Config.Serve(s.Listener); err != nil && s.Config.LogHandler != nil { 93โ s.Config.LogHandler(err.Error(), true) 94โ } 95โ }() 96โ } 97โ 98โ // NewServer returns a test server which has been started and is ready to use. 99โ func NewServer(t *testing.T, handler gemini.Handler) *Server { 100โ t.Helper() 101โ 102โ ts := NewUnstartedServer(t, handler) 103โ ts.Start() 104โ 105โ return ts 106โ } 107โ 108โ // Close closes the server. 109โ func (s *Server) Close() { 110โ s.mu.Lock() 111โ if !s.closed { 112โ s.closed = true 113โ 114โ if err := s.Listener.Close(); err != nil && s.Config.LogHandler != nil { 115โ s.Config.LogHandler(err.Error(), true) 116โ } 117โ } 118โ 119โ s.mu.Unlock() 120โ 121โ s.wg.Wait() 122โ } โโโโโฏ
ยท ยท ยท
ยฉ 2024 source.community