Skip to content

Commit

Permalink
Initial lark support (#108)
Browse files Browse the repository at this point in the history
* Initial lark support
  • Loading branch information
nt0xa authored Jan 30, 2023
1 parent 190a709 commit da49e1f
Show file tree
Hide file tree
Showing 13 changed files with 750 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ RUN apk add --no-cache ca-certificates
WORKDIR /opt/app
COPY --from=builder /opt/app/server .
COPY ./internal/database/migrations ./migrations
EXPOSE 53/udp 25 80 443
EXPOSE 53/udp 21 25 80 443
CMD ["./server"]
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func main() {
Params: models.UserParams{
TelegramID: cfg.Modules.Telegram.Admin,
APIToken: cfg.Modules.API.Admin,
LarkUserID: cfg.Modules.Lark.Admin,
},
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/gorilla/schema v1.2.0
github.com/jmoiron/sqlx v1.3.5
github.com/kelseyhightower/envconfig v1.4.0
github.com/larksuite/oapi-sdk-go/v3 v3.0.16
github.com/lib/pq v1.10.7
github.com/miekg/dns v1.1.50
github.com/mitchellh/mapstructure v1.5.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/ktrysmt/go-bitbucket v0.6.4/go.mod h1:9u0v3hsd2rqCHRIpbir1oP7F58uo5dq19sBYvuMoyQ4=
github.com/labbsr0x/bindman-dns-webhook v1.0.2/go.mod h1:p6b+VCXIR8NYKpDr8/dg1HKfQoRHCdcsROXKvmoehKA=
github.com/labbsr0x/goh v1.0.1/go.mod h1:8K2UhVoaWXcCU7Lxoa2omWnC8gyW8px7/lmO61c027w=
github.com/larksuite/oapi-sdk-go/v3 v3.0.16 h1:QGsNSAmRrmSQE5Mt1dhY/TBZaiszsUQsvBIePeupTQQ=
github.com/larksuite/oapi-sdk-go/v3 v3.0.16/go.mod h1:FKi8vBgtkBt/xNRQUwdWvoDmsPh7/wP75Sn5IBIBQLk=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand Down
18 changes: 16 additions & 2 deletions internal/cmd/server/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/russtone/sonar/internal/database"
"github.com/russtone/sonar/internal/database/models"
"github.com/russtone/sonar/internal/modules/api"
"github.com/russtone/sonar/internal/modules/lark"
"github.com/russtone/sonar/internal/modules/telegram"
)

Expand All @@ -23,16 +24,21 @@ type Notifier interface {
}

type ModulesConfig struct {
Enabled []string `json:"enabled"`
Enabled []string `json:"enabled"`

// TODO: dynamic modules config (something like json.RawMessage) to be able to not include
// unnecessary modules in binary.
Telegram telegram.Config `json:"telegram"`
API api.Config `json:"api"`
Lark lark.Config `json:"lark"`
}

func (c ModulesConfig) Validate() error {
rules := make([]*validation.FieldRules, 0)
rules = append(rules, validation.Field(&c.Enabled,
validation.Each(validation.In("telegram", "api"))))
validation.Each(validation.In("telegram", "api", "lark"))))

// TODO: dynamic modules registration. Something like sql drivers
for _, name := range c.Enabled {
switch name {

Expand All @@ -41,6 +47,9 @@ func (c ModulesConfig) Validate() error {

case "api":
rules = append(rules, validation.Field(&c.API))

case "lark":
rules = append(rules, validation.Field(&c.Lark))
}
}

Expand All @@ -64,6 +73,8 @@ func Modules(
err error
)

// TODO: dynamic modules registration. Something like sql drivers
// + build tags to include/exclude some modules from build
for _, name := range cfg.Enabled {
switch name {

Expand All @@ -73,6 +84,9 @@ func Modules(
case "api":
m, err = api.New(&cfg.API, db, log, tls, actions)

case "lark":
m, err = lark.New(&cfg.Lark, db, actions, domain)

}

if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions internal/database/fixtures/user_params.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,18 @@
user_id: 3
key: api.token
value: a33bfdbfb3c62feb7ea59314dbd17426

- id: 7
user_id: 1
key: lark.userid
value: 8b55131

- id: 8
user_id: 2
key: lark.userid
value: 8b2494d

- id: 9
user_id: 3
key: lark.userid
value: bca2924
2 changes: 2 additions & 0 deletions internal/database/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type UserParamsKey string
const (
UserTelegramID UserParamsKey = "telegram.id"
UserAPIToken UserParamsKey = "api.token"
UserLarkID UserParamsKey = "lark.userid"
)

var UserParamsKeys = []string{string(UserTelegramID), string(UserAPIToken)}
Expand All @@ -30,6 +31,7 @@ type User struct {
type UserParams struct {
TelegramID int64 `json:"telegram.id" mapstructure:"telegram.id"`
APIToken string `json:"api.token" mapstructure:"api.token"`
LarkUserID string `json:"lark.userid" mapstructure:"lark.userid"`
}

func (p UserParams) Value() (driver.Value, error) {
Expand Down
25 changes: 25 additions & 0 deletions internal/modules/lark/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lark

import (
validation "github.com/go-ozzo/ozzo-validation/v4"
)

type Config struct {
Admin string `json:"admin"`
AppID string `json:"app_id"`
AppSecret string `json:"app_secret"`
VerificationToken string `json:"verification_token"`
EncryptKey string `json:"encrypt_key"`
ProxyURL string `json:"proxy_url"`
ProxyInsecure bool `json:"proxy_insecure"`
}

func (c Config) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.Admin, validation.Required),
validation.Field(&c.AppID, validation.Required),
validation.Field(&c.AppSecret, validation.Required),
validation.Field(&c.VerificationToken, validation.Required),
validation.Field(&c.EncryptKey),
)
}
25 changes: 25 additions & 0 deletions internal/modules/lark/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lark

import (
"context"

"github.com/russtone/sonar/internal/utils/errors"
)

type contextKey string

const (
messageIDKey contextKey = "lark.messageID"
)

func GetMessageID(ctx context.Context) (*string, errors.Error) {
id, ok := ctx.Value(messageIDKey).(string)
if !ok {
return nil, errors.Internalf("no %q key in context", messageIDKey)
}
return &id, nil
}

func SetMessageID(ctx context.Context, msgID string) context.Context {
return context.WithValue(ctx, messageIDKey, msgID)
}
Loading

0 comments on commit da49e1f

Please sign in to comment.