mirror of
https://github.com/fiatjaf/khatru.git
synced 2025-11-19 18:46:44 +01:00
reimplement server.Start()
This commit is contained in:
32
start.go
32
start.go
@@ -4,12 +4,15 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/rs/cors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server is a base for package users to implement nostr relays.
|
// Server is a base for package users to implement nostr relays.
|
||||||
@@ -37,6 +40,9 @@ type Server struct {
|
|||||||
// keep a connection reference to all connected clients for Server.Shutdown
|
// keep a connection reference to all connected clients for Server.Shutdown
|
||||||
clientsMu sync.Mutex
|
clientsMu sync.Mutex
|
||||||
clients map[*websocket.Conn]struct{}
|
clients map[*websocket.Conn]struct{}
|
||||||
|
|
||||||
|
// in case you call Server.Start
|
||||||
|
httpServer *http.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer initializes the relay and its storage using their respective Init methods,
|
// NewServer initializes the relay and its storage using their respective Init methods,
|
||||||
@@ -77,12 +83,38 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) Start(host string, port int) error {
|
||||||
|
addr := net.JoinHostPort(host, strconv.Itoa(port))
|
||||||
|
ln, err := net.Listen("tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.httpServer = &http.Server{
|
||||||
|
Handler: cors.Default().Handler(s),
|
||||||
|
Addr: addr,
|
||||||
|
WriteTimeout: 2 * time.Second,
|
||||||
|
ReadTimeout: 2 * time.Second,
|
||||||
|
IdleTimeout: 30 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.httpServer.Serve(ln); err == http.ErrServerClosed {
|
||||||
|
return nil
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Shutdown sends a websocket close control message to all connected clients.
|
// Shutdown sends a websocket close control message to all connected clients.
|
||||||
//
|
//
|
||||||
// If the relay is ShutdownAware, Shutdown calls its OnShutdown, passing the context as is.
|
// If the relay is ShutdownAware, Shutdown calls its OnShutdown, passing the context as is.
|
||||||
// Note that the HTTP server make some time to shutdown and so the context deadline,
|
// Note that the HTTP server make some time to shutdown and so the context deadline,
|
||||||
// if any, may have been shortened by the time OnShutdown is called.
|
// if any, may have been shortened by the time OnShutdown is called.
|
||||||
func (s *Server) Shutdown(ctx context.Context) {
|
func (s *Server) Shutdown(ctx context.Context) {
|
||||||
|
s.httpServer.Shutdown(ctx)
|
||||||
|
|
||||||
s.clientsMu.Lock()
|
s.clientsMu.Lock()
|
||||||
defer s.clientsMu.Unlock()
|
defer s.clientsMu.Unlock()
|
||||||
for conn := range s.clients {
|
for conn := range s.clients {
|
||||||
|
|||||||
Reference in New Issue
Block a user