Consul registration

This commit is contained in:
2024-06-16 22:16:09 +08:00
parent ef055e1144
commit 0695338fa1
7 changed files with 263 additions and 36 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"git.solsynth.dev/hydrogen/passport/pkg/gap"
"os"
"os/signal"
"syscall"
@ -46,6 +47,9 @@ func main() {
}
// Connect other services
if err := gap.Register(); err != nil {
log.Error().Err(err).Msg("An error occurred when registering service to gateway...")
}
if err := services.SetupFirebase(); err != nil {
log.Error().Err(err).Msg("An error occurred when connecting firebase...")
}

38
pkg/gap/server.go Normal file
View File

@ -0,0 +1,38 @@
package gap
import (
"fmt"
"github.com/hashicorp/consul/api"
"github.com/spf13/viper"
"strconv"
"strings"
)
func Register() error {
cfg := api.DefaultConfig()
cfg.Address = viper.GetString("consul.addr")
client, err := api.NewClient(cfg)
if err != nil {
return err
}
bind := strings.SplitN(viper.GetString("consul.srv_serve"), ":", 2)
baseAddr := viper.GetString("consul.srv_http")
port, _ := strconv.Atoi(bind[1])
registration := new(api.AgentServiceRegistration)
registration.ID = viper.GetString("id")
registration.Name = "Hydrogen.Passport"
registration.Address = bind[0]
registration.Port = port
registration.Check = &api.AgentServiceCheck{
HTTP: fmt.Sprintf("%s/.well-known", baseAddr),
Timeout: "5s",
Interval: "5s",
DeregisterCriticalServiceAfter: "10s",
}
return client.Agent().ServiceRegister(registration)
}