cwtch/server/server.go

80 lines
2.4 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/utils"
"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
2018-10-05 03:18:34 +00:00
func (s *Server) Run(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
l, err := application.SetupOnionV3("127.0.0.1:9051", "tcp4", "", s.config.PrivateKey, utils.GetTorV3Hostname(s.config.PublicKey), 9878)
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-10-05 03:18:34 +00:00
cwtchserver.InitV3("cwtch server for "+l.Addr().String(), s.config.Identity(), af, new(application.AcceptAllContactManager))
log.Printf("cwtch server running on cwtch:%s", l.Addr().String())
s.app = cwtchserver
s.app.Run(l)
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()
}