cwtch/server/server.go

82 lines
2.5 KiB
Go
Raw Normal View History

2018-03-09 20:44:13 +00:00
package server
import (
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/server/fetch"
"cwtch.im/cwtch/server/listen"
"cwtch.im/cwtch/server/metrics"
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/server/send"
"cwtch.im/cwtch/storage"
2018-06-23 16:15:36 +00:00
"git.openprivacy.ca/openprivacy/libricochet-go/application"
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
"log"
2018-03-09 20:44:13 +00:00
)
2018-05-16 21:11:04 +00:00
// Server encapsulates a complete, compliant Cwtch server.
2018-03-09 20:44:13 +00:00
type Server struct {
app *application.RicochetApplication
2018-10-05 03:18:34 +00:00
config Config
metricsPack metrics.Monitors
2018-03-09 20:44:13 +00:00
}
2018-06-03 19:36:20 +00:00
// Run starts a server with the given privateKey
2018-05-16 21:11:04 +00:00
// TODO: surface errors
// TODO: handle HUP/KILL signals to exit and close Tor gracefully
// TODO: handle user input to exit
2018-11-22 18:01:04 +00:00
func (s *Server) Run(acn connectivity.ACN, serverConfig Config) {
s.config = serverConfig
2018-03-09 20:44:13 +00:00
cwtchserver := new(application.RicochetApplication)
s.metricsPack.Start(cwtchserver, serverConfig.ConfigDir, s.config.ServerReporting.LogMetricsToFile)
2018-03-09 20:44:13 +00:00
2018-11-22 18:01:04 +00:00
listenService, err := acn.Listen(s.config.PrivateKey, application.RicochetPort)
2018-03-09 20:44:13 +00:00
if err != nil {
log.Fatalf("error setting up onion service: %v", err)
}
af := application.ApplicationInstanceFactory{}
af.Init()
ms := new(storage.MessageStore)
err = ms.Init(serverConfig.ConfigDir, s.config.MaxBufferLines, s.metricsPack.MessageCounter)
if err != nil {
log.Fatal(err)
}
2018-03-09 20:44:13 +00:00
af.AddHandler("im.cwtch.server.listen", func(rai *application.ApplicationInstance) func() channels.Handler {
return func() channels.Handler {
cslc := new(listen.CwtchServerListenChannel)
return cslc
}
})
af.AddHandler("im.cwtch.server.fetch", func(rai *application.ApplicationInstance) func() channels.Handler {
2018-03-14 22:23:35 +00:00
si := new(Instance)
2018-03-09 20:44:13 +00:00
si.Init(rai, cwtchserver, ms)
return func() channels.Handler {
cssc := new(fetch.CwtchServerFetchChannel)
cssc.Handler = si
return cssc
}
})
af.AddHandler("im.cwtch.server.send", func(rai *application.ApplicationInstance) func() channels.Handler {
2018-03-14 22:23:35 +00:00
si := new(Instance)
2018-03-09 20:44:13 +00:00
si.Init(rai, cwtchserver, ms)
return func() channels.Handler {
cssc := new(send.CwtchServerSendChannel)
cssc.Handler = si
return cssc
}
})
2018-11-22 18:01:04 +00:00
cwtchserver.Init(acn, "cwtch server for "+listenService.AddressIdentity(), s.config.Identity(), af, new(application.AcceptAllContactManager))
log.Printf("cwtch server running on cwtch:%s", listenService.AddressFull())
s.app = cwtchserver
s.app.Run(listenService)
2018-03-09 20:44:13 +00:00
}
// Shutdown kills the app closing all connections and freeing all goroutines
func (s *Server) Shutdown() {
s.app.Shutdown()
s.metricsPack.Stop()
}