2021-08-31 14:26:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-02 00:13:12 +00:00
|
|
|
"context"
|
2021-08-31 14:26:09 +00:00
|
|
|
"log"
|
2021-09-02 00:13:12 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-09-01 21:18:52 +00:00
|
|
|
"strconv"
|
2021-09-02 00:13:12 +00:00
|
|
|
"syscall"
|
2021-08-31 14:26:09 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/middleware"
|
2021-09-02 17:22:39 +00:00
|
|
|
"github.com/rosti-cz/server_lobby/common"
|
|
|
|
"github.com/rosti-cz/server_lobby/nats_driver"
|
2021-08-31 14:26:09 +00:00
|
|
|
"github.com/rosti-cz/server_lobby/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
var discoveryStorage server.Discoveries = server.Discoveries{}
|
2021-09-02 17:22:39 +00:00
|
|
|
var driver common.Driver
|
2021-09-04 20:17:56 +00:00
|
|
|
var localHost server.LocalHost
|
2021-08-31 14:26:09 +00:00
|
|
|
|
|
|
|
var config Config
|
|
|
|
|
2021-09-02 00:13:12 +00:00
|
|
|
var shuttingDown bool
|
|
|
|
|
2021-08-31 14:26:09 +00:00
|
|
|
func init() {
|
2021-09-02 17:22:39 +00:00
|
|
|
// Load config from environment variables
|
|
|
|
config = *GetConfig()
|
|
|
|
|
|
|
|
// Setup discovery storage
|
2021-08-31 14:26:09 +00:00
|
|
|
discoveryStorage.LogChannel = make(chan string)
|
2021-09-02 17:22:39 +00:00
|
|
|
discoveryStorage.TTL = config.TTL
|
|
|
|
|
2021-09-04 20:17:56 +00:00
|
|
|
// localhost initization
|
|
|
|
localHost = server.LocalHost{
|
|
|
|
LabelsPath: config.LabelsPath,
|
|
|
|
HostnameOverride: config.HostName,
|
|
|
|
InitialLabels: config.Labels,
|
|
|
|
RuntimeLabelsFilename: config.RuntimeLabelsFilename,
|
|
|
|
}
|
|
|
|
|
2021-09-02 17:22:39 +00:00
|
|
|
// Setup driver
|
|
|
|
driver = &nats_driver.Driver{
|
|
|
|
NATSUrl: config.NATSURL,
|
|
|
|
NATSDiscoveryChannel: config.NATSDiscoveryChannel,
|
|
|
|
|
|
|
|
LogChannel: discoveryStorage.LogChannel,
|
|
|
|
}
|
2021-08-31 14:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanDiscoveryPool clears the local server map and keeps only the alive servers
|
|
|
|
func cleanDiscoveryPool() {
|
|
|
|
for {
|
|
|
|
discoveryStorage.Clean()
|
2021-09-01 21:18:52 +00:00
|
|
|
time.Sleep(time.Duration(config.CleanEvery) * time.Second)
|
2021-08-31 14:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-02 00:13:12 +00:00
|
|
|
// sendGoodbyePacket is almost same as sendDiscoveryPacket but it's not running in loop
|
|
|
|
// and it adds goodbye message so other nodes know this node is gonna die.
|
2021-09-02 17:22:39 +00:00
|
|
|
func sendGoodbyePacket() {
|
2021-09-04 20:17:56 +00:00
|
|
|
discovery, err := localHost.GetIdentification()
|
2021-09-02 00:13:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("sending discovery identification error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 17:22:39 +00:00
|
|
|
err = driver.SendGoodbyePacket(discovery)
|
2021-09-02 00:13:12 +00:00
|
|
|
if err != nil {
|
2021-09-02 17:22:39 +00:00
|
|
|
log.Println(err)
|
2021-09-02 00:13:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 14:26:09 +00:00
|
|
|
// sendDisoveryPacket sends discovery packet regularly so the network know we exist
|
2021-09-02 17:22:39 +00:00
|
|
|
func sendDiscoveryPacket() {
|
2021-08-31 14:26:09 +00:00
|
|
|
for {
|
2021-09-04 20:17:56 +00:00
|
|
|
discovery, err := localHost.GetIdentification()
|
2021-08-31 14:26:09 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("sending discovery identification error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 17:22:39 +00:00
|
|
|
err = driver.SendDiscoveryPacket(discovery)
|
2021-08-31 14:26:09 +00:00
|
|
|
if err != nil {
|
2021-09-02 17:22:39 +00:00
|
|
|
log.Println(err)
|
2021-08-31 14:26:09 +00:00
|
|
|
}
|
2021-09-01 21:18:52 +00:00
|
|
|
time.Sleep(time.Duration(config.KeepAlive) * time.Second)
|
2021-09-02 00:13:12 +00:00
|
|
|
|
|
|
|
if shuttingDown {
|
|
|
|
break
|
|
|
|
}
|
2021-08-31 14:26:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print logs acquired from disovery storage
|
|
|
|
func printDiscoveryLogs() {
|
|
|
|
for {
|
|
|
|
logMessage := <-discoveryStorage.LogChannel
|
|
|
|
log.Println(logMessage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Closing the logging channel
|
|
|
|
defer close(discoveryStorage.LogChannel)
|
2021-09-02 17:22:39 +00:00
|
|
|
defer driver.Close()
|
2021-08-31 14:26:09 +00:00
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
// Server discovering stuff
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
// Connect to the NATS service
|
2021-09-02 17:22:39 +00:00
|
|
|
driver.RegisterSubscribeFunction(func(d server.Discovery) {
|
|
|
|
discoveryStorage.Add(d)
|
|
|
|
})
|
|
|
|
driver.RegisterUnsubscribeFunction(func(d server.Discovery) {
|
|
|
|
discoveryStorage.Delete(d.Hostname)
|
|
|
|
})
|
|
|
|
|
|
|
|
err = driver.Init()
|
2021-08-31 14:26:09 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go printDiscoveryLogs()
|
|
|
|
go cleanDiscoveryPool()
|
2021-09-02 17:22:39 +00:00
|
|
|
|
|
|
|
// If config.Register is false this instance won't be registered with other nodes
|
|
|
|
if config.Register {
|
|
|
|
go sendDiscoveryPacket()
|
|
|
|
} else {
|
|
|
|
log.Println("standalone mode, I won't register myself")
|
|
|
|
}
|
2021-08-31 14:26:09 +00:00
|
|
|
|
|
|
|
// --------
|
|
|
|
// REST API
|
|
|
|
// --------
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
// Middleware
|
2021-09-01 21:18:52 +00:00
|
|
|
if len(config.Token) > 0 {
|
|
|
|
e.Use(TokenMiddleware)
|
|
|
|
}
|
2021-08-31 14:26:09 +00:00
|
|
|
e.Use(middleware.Logger())
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
|
|
|
|
// Routes
|
2021-09-02 17:22:39 +00:00
|
|
|
e.GET("/", listHandler)
|
2021-09-04 20:17:56 +00:00
|
|
|
e.GET("/v1/discovery", getIdentificationHandler)
|
|
|
|
e.GET("/v1/discoveries", listHandler)
|
|
|
|
e.POST("/v1/labels", addLabelsHandler)
|
|
|
|
e.DELETE("/v1/labels", deleteLabelsHandler)
|
2021-09-02 21:05:57 +00:00
|
|
|
e.GET("/v1/prometheus/:name", prometheusHandler)
|
2021-09-01 21:18:52 +00:00
|
|
|
|
2021-09-02 00:13:12 +00:00
|
|
|
// ------------------------------
|
|
|
|
// Termination signals processing
|
|
|
|
// ------------------------------
|
|
|
|
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
2021-09-02 17:22:39 +00:00
|
|
|
go func(e *echo.Echo, config Config) {
|
2021-09-02 00:13:12 +00:00
|
|
|
sig := <-signals
|
|
|
|
shuttingDown = true
|
2021-09-02 17:22:39 +00:00
|
|
|
if config.Register {
|
|
|
|
log.Printf("%s signal received, sending goodbye packet\n", sig.String())
|
|
|
|
sendGoodbyePacket()
|
|
|
|
time.Sleep(5 * time.Second) // we wait for a few seconds to let background jobs to finish their job
|
|
|
|
} else {
|
|
|
|
log.Printf("%s signal received", sig.String())
|
|
|
|
}
|
2021-09-02 00:13:12 +00:00
|
|
|
e.Shutdown(context.TODO())
|
2021-09-02 17:22:39 +00:00
|
|
|
}(e, config)
|
2021-09-02 00:13:12 +00:00
|
|
|
|
2021-08-31 14:26:09 +00:00
|
|
|
// Start server
|
2021-09-02 00:13:12 +00:00
|
|
|
e.Logger.Error(e.Start(config.Host + ":" + strconv.Itoa(int(config.Port))))
|
2021-08-31 14:26:09 +00:00
|
|
|
}
|