Skip to content

Commit

Permalink
update: grpc api for avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
minoic committed Apr 16, 2024
1 parent 6418d09 commit acc7ecf
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 163 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 打包依赖阶段使用golang作为基础镜像
FROM golang:1.21.0-alpine as builder
FROM golang:1.22.0-alpine as builder

WORKDIR /workspace

Expand Down
45 changes: 22 additions & 23 deletions common/storage/etcd.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
package storage

import (
"github.com/minoic/glgf"
"gopkg.in/yaml.v3"
"net/http"
"os"
"strings"
"sync"

"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
)

var etcdOnce sync.Once
var etcdConfig *ConfigInfo

type ConfigInfo struct {
InfluxDBToken string `mapstructure:"influxdb_token"`
InfluxDBURI string `mapstructure:"influxdb_uri"`
InfluxDBOrg string `mapstructure:"influxdb_org"`
InfluxDBDatabase string `mapstructure:"influxdb_database"`
MongoDBURI string `mapstructure:"mongodb_uri"`
TelegramBotToken string `mapstructure:"telegram_bot_token"`
TelegramBotApiEndpoint string `mapstructure:"telegram_bot_api_endpoint"`
TelegramBotName string `mapstructure:"telegram_bot_name"`
TencentSTMPPassword string `mapstructure:"tencent_smtp_password"`
WebEndpoint string `mapstructure:"web_endpoint"`
RedisAddrs []string `mapstructure:"redis_addrs"`
RedisUsername string `mapstructure:"redis_username"`
RedisPassword string `mapstructure:"redis_password"`
InfluxDBToken string `yaml:"influxdb_token"`
InfluxDBURI string `yaml:"influxdb_uri"`
InfluxDBOrg string `yaml:"influxdb_org"`
InfluxDBDatabase string `yaml:"influxdb_database"`
MongoDBURI string `yaml:"mongodb_uri"`
TelegramBotToken string `yaml:"telegram_bot_token"`
TelegramBotApiEndpoint string `yaml:"telegram_bot_api_endpoint"`
TelegramBotName string `yaml:"telegram_bot_name"`
TencentSTMPPassword string `yaml:"tencent_smtp_password"`
WebEndpoint string `yaml:"web_endpoint"`
RedisAddrs []string `yaml:"redis_addrs"`
RedisUsername string `yaml:"redis_username"`
RedisPassword string `yaml:"redis_password"`
}

func Config() *ConfigInfo {
etcdOnce.Do(func() {
url := os.ExpandEnv("$CONFIG_URL")
etcdConfig = new(ConfigInfo)
err := viper.AddRemoteProvider("etcd3", "etcd:2379", "/config")
if err != nil {
panic(err)
}
viper.SetConfigType("yaml") // Need to explicitly set this to json
err = viper.ReadRemoteConfig()
resp, err := http.Get(url)
if err != nil {
glgf.Error(url)
panic(err)
}
err = viper.Unmarshal(etcdConfig)
defer resp.Body.Close()
err = yaml.NewDecoder(resp.Body).Decode(etcdConfig)
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions common/storage/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func Redis() redis.UniversalClient {
}

func QuickRCSearch[T any](ctx context.Context, key string, fallback func() (T, error), exp ...time.Duration) (*T, error) {
const QUICK_RC = "QUICK_RC_"
cmd := Redis().Get(ctx, QUICK_RC+key)
const QuickRc = "QUICK_RC_"
cmd := Redis().Get(ctx, QuickRc+key)
if cmd.Err() != nil {
value, err := fallback()
if err != nil {
Expand All @@ -52,7 +52,7 @@ func QuickRCSearch[T any](ctx context.Context, key string, fallback func() (T, e
glgf.Warn(err)
return
}
Redis().Set(ctx, QUICK_RC+key, buf.String(), dur)
Redis().Set(ctx, QuickRc+key, buf.String(), dur)
}()
return &value, nil
}
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/bellis-daemon/bellis

go 1.21
go 1.22

require (
firebase.google.com/go v3.13.0+incompatible
github.com/avast/retry-go/v4 v4.3.4
github.com/avast/retry-go/v4 v4.5.1
github.com/go-ping/ping v1.1.0
github.com/go-redsync/redsync/v4 v4.11.0
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
Expand All @@ -25,7 +25,6 @@ require (
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/soheilhy/cmux v0.1.5
github.com/spf13/cast v1.5.1
github.com/spf13/viper v1.17.0
github.com/v2fly/v2ray-core/v5 v5.4.1
github.com/wcharczuk/go-chart/v2 v2.1.1
github.com/xhit/go-simple-mail/v2 v2.16.0
Expand All @@ -35,7 +34,7 @@ require (
golang.org/x/time v0.3.0
google.golang.org/api v0.143.0
google.golang.org/grpc v1.58.3
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.33.0
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf h1:eg0MeVzs
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/avast/retry-go/v4 v4.3.4 h1:pHLkL7jvCvP317I8Ge+Km2Yhntv3SdkJm7uekkqbKhM=
github.com/avast/retry-go/v4 v4.3.4/go.mod h1:rv+Nla6Vk3/ilU0H51VHddWHiwimzX66yZ0JT6T+UvE=
github.com/avast/retry-go/v4 v4.5.1 h1:AxIx0HGi4VZ3I02jr78j5lZ3M6x1E0Ivxa6b0pUUh7o=
github.com/avast/retry-go/v4 v4.5.1/go.mod h1:/sipNsvNB3RRuT5iNcb6h73nw3IBmXJ/H3XrCQYSOpc=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -1187,6 +1189,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
66 changes: 66 additions & 0 deletions modules/backend/app/mobile/profile/gravatar/gravatar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package gravatar

import (
"crypto/md5"
"encoding/hex"
"net/url"
"strconv"
"strings"
)

const (
defaultScheme = "https"
defaultHostname = "gravatar.loli.net"
)

func NewGravatarFromEmail(email string) Gravatar {
hasher := md5.Sum([]byte(strings.TrimSpace(email)))
hash := hex.EncodeToString(hasher[:])

g := NewGravatar()
g.Hash = hash
g.Default = "retro"
return g
}

func NewGravatar() Gravatar {
return Gravatar{
Scheme: defaultScheme,
Host: defaultHostname,
}
}

type Gravatar struct {
Scheme string
Host string
Hash string
Default string
Rating string
Size int
}

func (g Gravatar) GetURL() string {
path := "/avatar/" + g.Hash

v := url.Values{}
if g.Size > 0 {
v.Add("s", strconv.Itoa(g.Size))
}

if g.Rating != "" {
v.Add("r", g.Rating)
}

if g.Default != "" {
v.Add("d", g.Default)
}

url := url.URL{
Scheme: g.Scheme,
Host: g.Host,
Path: path,
RawQuery: v.Encode(),
}

return url.String()
}
28 changes: 28 additions & 0 deletions modules/backend/app/mobile/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/bellis-daemon/bellis/common/models"
"github.com/bellis-daemon/bellis/common/storage"
"github.com/bellis-daemon/bellis/modules/backend/app/mobile"
"github.com/bellis-daemon/bellis/modules/backend/app/mobile/profile/gravatar"
"github.com/bellis-daemon/bellis/modules/backend/midwares"
"github.com/minoic/glgf"
"github.com/spf13/cast"
Expand All @@ -20,6 +21,8 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"io"
"net/http"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -267,9 +270,34 @@ func (h handler) GetUserProfile(ctx context.Context, empty *emptypb.Empty) (*Use
Email: user.Email,
CreatedAt: user.CreatedAt.Local().Format(time.DateTime),
Level: uint32(user.Level),
Avatar: nil,
Policies: nil,
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
avatar, err := storage.QuickRCSearch[string](ctx, "AVATAR_CACHE_"+user.ID.Hex(), func() (string, error) {
cl := &http.Client{
Timeout: 3 * time.Second,
}
get, err := cl.Get(gravatar.NewGravatarFromEmail(user.Email).GetURL())
if err != nil {
return "", err
}
defer get.Body.Close()
ret, err := io.ReadAll(get.Body)
if err != nil {
return "", err
}
return string(ret), nil
}, time.Hour)
if err != nil {
glgf.Error(err)
} else {
ret.Avatar = []byte(*avatar)
}
}()
for i := range user.EnvoyPolicies {
wg.Add(1)
p := user.EnvoyPolicies[i]
Expand Down
Loading

0 comments on commit acc7ecf

Please sign in to comment.