From 9a8df85250fe5cd7e0059cd9a08373e895f50bb6 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 16 Jun 2024 23:32:52 +0800 Subject: [PATCH] :sparkles: Add consul registeration --- .idea/workspace.xml | 67 +++++++++++++------------------------- pkg/internal/gap/server.go | 38 +++++++++++++++++++++ pkg/main.go | 22 +++++++------ settings.toml | 5 +++ 4 files changed, 78 insertions(+), 54 deletions(-) create mode 100644 pkg/internal/gap/server.go diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4e96ad0..e073386 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,34 +5,10 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - { - "keyToString": { - "Go 构建.Backend.executor": "Run", - "RunOnceActivity.ShowReadmeOnStart": "true", - "RunOnceActivity.go.formatter.settings.were.checked": "true", - "RunOnceActivity.go.migrated.go.modules.settings": "true", - "RunOnceActivity.go.modules.automatic.dependencies.download": "true", - "RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true", - "git-widget-placeholder": "master", - "go.import.settings.migrated": "true", - "go.sdk.automatically.set": "true", - "last_opened_file_path": "/Users/littlesheep", - "node.js.detected.package.eslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "settings.editor.selected.configurable": "preferences.lookFeel" + +}]]> + + + diff --git a/pkg/internal/gap/server.go b/pkg/internal/gap/server.go new file mode 100644 index 0000000..52e2922 --- /dev/null +++ b/pkg/internal/gap/server.go @@ -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.Paperclip" + 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) +} diff --git a/pkg/main.go b/pkg/main.go index 1778b38..f3b895c 100644 --- a/pkg/main.go +++ b/pkg/main.go @@ -1,8 +1,9 @@ package main import ( - database2 "git.solsynth.dev/hydrogen/paperclip/pkg/internal/database" - grpc2 "git.solsynth.dev/hydrogen/paperclip/pkg/internal/grpc" + "git.solsynth.dev/hydrogen/paperclip/pkg/internal/database" + "git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap" + "git.solsynth.dev/hydrogen/paperclip/pkg/internal/grpc" "os" "os/signal" "syscall" @@ -35,14 +36,17 @@ func main() { } // Connect to database - if err := database2.NewSource(); err != nil { + if err := database.NewSource(); err != nil { log.Fatal().Err(err).Msg("An error occurred when connect to database.") - } else if err := database2.RunMigration(database2.C); err != nil { + } else if err := database.RunMigration(database.C); err != nil { log.Fatal().Err(err).Msg("An error occurred when running database auto migration.") } // Connect other services - if err := grpc2.ConnectPassport(); err != nil { + if err := gap.Register(); err != nil { + log.Error().Err(err).Msg("An error occurred when registering service to gateway...") + } + if err := grpc.ConnectPassport(); err != nil { log.Fatal().Err(err).Msg("An error occurred when connecting to passport grpc endpoint...") } @@ -56,11 +60,9 @@ func main() { go server.Listen() // Grpc Server - go func() { - if err := grpc2.StartGrpc(); err != nil { - log.Fatal().Err(err).Msg("An message occurred when starting grpc server.") - } - }() + if err := grpc.StartGrpc(); err != nil { + log.Fatal().Err(err).Msg("An message occurred when starting grpc server.") + } // Messages log.Info().Msgf("Paperclip v%s is started...", pkg.AppVersion) diff --git a/settings.toml b/settings.toml index e43f6d1..21c49cd 100644 --- a/settings.toml +++ b/settings.toml @@ -19,6 +19,11 @@ client_secret = "Z9k9AFTj^p" endpoint = "http://localhost:8444" grpc_endpoint = "localhost:7444" +[consul] +addr = "127.0.0.1:8500" +srv_serve = "127.0.0.1:8443" +srv_http = "http://192.168.50.83:8443" + [security] cookie_domain = "localhost" cookie_samesite = "Lax"