khatru/get-started.go

66 lines
1.4 KiB
Go
Raw Permalink Normal View History

package khatru
2021-01-13 23:46:06 -03:00
import (
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
"context"
2023-05-01 19:56:37 -03:00
"net"
2021-01-13 23:46:06 -03:00
"net/http"
2023-05-01 19:56:37 -03:00
"strconv"
2021-01-13 23:46:06 -03:00
"time"
2023-06-23 07:10:03 -03:00
"github.com/fasthttp/websocket"
2023-05-01 19:56:37 -03:00
"github.com/rs/cors"
2021-01-13 23:46:06 -03:00
)
func (rl *Relay) Router() *http.ServeMux {
return rl.serveMux
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
}
2024-10-27 17:20:10 -03:00
func (rl *Relay) SetRouter(mux *http.ServeMux) {
rl.serveMux = mux
}
// Start creates an http server and starts listening on given host and port.
func (rl *Relay) Start(host string, port int, started ...chan bool) error {
2023-05-01 19:56:37 -03:00
addr := net.JoinHostPort(host, strconv.Itoa(port))
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
rl.Addr = ln.Addr().String()
rl.httpServer = &http.Server{
Handler: cors.Default().Handler(rl),
2023-05-01 19:56:37 -03:00
Addr: addr,
WriteTimeout: 2 * time.Second,
ReadTimeout: 2 * time.Second,
IdleTimeout: 30 * time.Second,
}
2023-05-01 20:38:11 -03:00
// notify caller that we're starting
for _, started := range started {
close(started)
}
if err := rl.httpServer.Serve(ln); err == http.ErrServerClosed {
2023-05-01 19:56:37 -03:00
return nil
} else if err != nil {
return err
} else {
return nil
}
}
// Shutdown sends a websocket close control message to all connected clients.
func (rl *Relay) Shutdown(ctx context.Context) {
rl.httpServer.Shutdown(ctx)
2024-07-30 09:43:04 -03:00
rl.clientsMutex.Lock()
defer rl.clientsMutex.Unlock()
for ws := range rl.clients {
ws.conn.WriteControl(websocket.CloseMessage, nil, time.Now().Add(time.Second))
ws.cancel()
2024-07-30 09:43:04 -03:00
ws.conn.Close()
}
clear(rl.clients)
rl.listeners = rl.listeners[:0]
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
}