Add consul registeration

This commit is contained in:
LittleSheep 2024-06-16 23:32:52 +08:00
parent 05a59113c9
commit 9a8df85250
4 changed files with 78 additions and 54 deletions

67
.idea/workspace.xml generated
View File

@ -5,34 +5,10 @@
</component>
<component name="ChangeListManager">
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":bug: Fix uuid duplicate when link exists">
<change afterPath="$PROJECT_DIR$/pkg/internal/gap/server.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Dockerfile" beforeDir="false" afterPath="$PROJECT_DIR$/Dockerfile" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/cmd/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/main.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/database/migrator.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/database/migrator.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/database/source.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/database/source.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/grpc/attachments.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/grpc/attachments.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/grpc/client.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/grpc/client.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/grpc/proto/attachments.pb.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/proto/attachments.pb.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/grpc/proto/attachments.proto" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/proto/attachments.proto" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/grpc/proto/attachments_grpc.pb.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/proto/attachments_grpc.pb.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/grpc/server.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/grpc/server.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/meta.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/meta.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/models/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/accounts.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/models/attachments.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/attachments.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/models/base.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/base.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/models/destination.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/destination.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/models/metadata.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/metadata.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/server/attachments_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/attachments_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/server/auth.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/auth.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/server/startup.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/startup.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/server/utils.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/utils.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/server/well_known_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/well_known_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/attachments.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/attachments.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/auth.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/auth.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/cleaner.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/cleaner.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/jwt.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/jwt.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/recycler.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/recycler.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/uploader.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/uploader.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/main.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/settings.toml" beforeDir="false" afterPath="$PROJECT_DIR$/settings.toml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -52,25 +28,28 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;Go 构建.Backend.executor&quot;: &quot;Run&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.go.formatter.settings.were.checked&quot;: &quot;true&quot;,
&quot;RunOnceActivity.go.migrated.go.modules.settings&quot;: &quot;true&quot;,
&quot;RunOnceActivity.go.modules.automatic.dependencies.download&quot;: &quot;true&quot;,
&quot;RunOnceActivity.go.modules.go.list.on.any.changes.was.set&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;master&quot;,
&quot;go.import.settings.migrated&quot;: &quot;true&quot;,
&quot;go.sdk.automatically.set&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;/Users/littlesheep&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;preferences.lookFeel&quot;
<component name="PropertiesComponent"><![CDATA[{
"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": "features/consul",
"go.import.settings.migrated": "true",
"go.sdk.automatically.set": "true",
"last_opened_file_path": "/Users/littlesheep/Documents/Projects/Hydrogen/Paperclip/pkg/internal",
"node.js.detected.package.eslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "preferences.lookFeel"
}
}</component>
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/pkg/internal" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/pkg" />
<recent name="$PROJECT_DIR$/pkg/internal" />

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.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)
}

View File

@ -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)

View File

@ -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"