Skip to content

Commit

Permalink
wip: accounts resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored May 14, 2024
1 parent 59e1d9a commit 3f17218
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 36 deletions.
4 changes: 2 additions & 2 deletions cmd/accounts/.air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
root = "."
tmp_dir = "tmp"
[build]
cmd = "go build -o ./tmp/api cmd/api/main.go"
bin = "./tmp/api"
cmd = "go build -o ./tmp/accounts cmd/accounts/main.go"
bin = "./tmp/accounts"
delay = 1000 # ms
exclude_dir = ["assets", "tmp", "vendor"]
include_ext = ["go", "tpl", "tmpl", "html"]
Expand Down
7 changes: 7 additions & 0 deletions cmd/accounts/cmd/cfg.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd

import (
"fmt"
"os"
)

// DB ...
type DB struct {
Addr string `envconfig:"TYPHOON_DB_ADDR" default:"host.docker.internal"`
Username string `envconfig:"TYPHOON_DB_USERNAME" default:"example"`
Password string `envconfig:"TYPHOON_DB_PASSWORD" default:"example"`
Port int `envconfig:"TYPHOON_DB_PORT" default:"5432"`
Expand Down Expand Up @@ -44,6 +46,11 @@ type Config struct {
Flags *Flags
}

// DSN for PostgreSQL.
func (c *Config) DSN() string {
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", c.Flags.DB.Addr, c.Flags.DB.Username, c.Flags.DB.Password, c.Flags.DB.Database, c.Flags.DB.Port)
}

// Cwd returns the current working directory.
func (c *Config) Cwd() (string, error) {
return os.Getwd()
Expand Down
10 changes: 5 additions & 5 deletions cmd/accounts/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"

"github.com/zeiss/typhoon/internal/accounts/adapters"
"github.com/zeiss/typhoon/internal/accounts/adapters/handlers"
"github.com/zeiss/typhoon/internal/accounts/controllers"
"github.com/zeiss/typhoon/internal/accounts/models"
"gorm.io/driver/postgres"
"gorm.io/gorm"

Expand Down Expand Up @@ -71,7 +71,7 @@ func (s *AccountSrv) Start(ctx context.Context, ready server.ReadyFunc, run serv

db := adapters.NewDB(conn)
ac := controllers.NewAccountsController(db)
lh := adapters.NewAccountLookupRequestHandler(ac)
lh := handlers.NewAccountLookupRequestHandler(ac)

nc, err := nats.Connect(cfg.Flags.Nats.Url, nats.UserCredentials(cfg.Flags.Nats.Credentials))
if err != nil {
Expand All @@ -89,13 +89,13 @@ func (s *AccountSrv) Start(ctx context.Context, ready server.ReadyFunc, run serv
return err
}

accountId := strings.TrimSuffix(strings.TrimPrefix(msg.Subject, "$SYS.REQ.ACCOUNT."), ".CLAIMS.LOOKUP")
accountJWTToken, err := lh.HandleLookupRequest(ctx, models.AccountPublicKey(accountId))
apk := strings.TrimSuffix(strings.TrimPrefix(msg.Subject, "$SYS.REQ.ACCOUNT."), ".CLAIMS.LOOKUP")
jwt, err := lh.HandleLookupRequest(ctx, apk)
if err != nil {
return err
}

err = msg.Respond([]byte(accountJWTToken))
err = msg.Respond([]byte(jwt))
if err != nil {
return err
}
Expand Down
13 changes: 3 additions & 10 deletions internal/accounts/adapters/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package adapters
import (
"context"

"github.com/zeiss/typhoon/internal/accounts/models"
"github.com/zeiss/typhoon/internal/accounts/ports"
api "github.com/zeiss/typhoon/internal/api/models"

"gorm.io/gorm"
)

var _ ports.Accounts = (*DB)(nil)
var _ ports.Repositories = (*DB)(nil)

// DB ...
type DB struct {
Expand All @@ -23,12 +22,6 @@ func NewDB(conn *gorm.DB) *DB {
}

// GetToken ...
func (db *DB) GetToken(ctx context.Context, pubKey models.AccountPublicKey) (models.AccountToken, error) {
var token api.Token

if err := db.conn.Where("id = ?", pubKey).First(&token).Error; err != nil {
return models.AccountToken(""), err
}

return models.AccountToken(token.Token), nil
func (db *DB) GetToken(ctx context.Context, token *api.Token) error {
return db.conn.First(token).Error
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package adapters
package handlers

import (
"context"

"github.com/zeiss/typhoon/internal/accounts/controllers"
"github.com/zeiss/typhoon/internal/accounts/models"
)

// AccountLookupRequestHandler ...
Expand All @@ -18,6 +17,13 @@ func NewAccountLookupRequestHandler(ctrl controllers.AccountsController) *Accoun
}

// HandleLookupRequest ...
func (h *AccountLookupRequestHandler) HandleLookupRequest(ctx context.Context, accountPublicKey models.AccountPublicKey) (models.AccountToken, error) {
return h.ctrl.GetToken(ctx, accountPublicKey)
func (h *AccountLookupRequestHandler) HandleLookupRequest(ctx context.Context, accountPublicKey string) (string, error) {
query := controllers.GetTokenQuery{AccountPublicKey: accountPublicKey}

result, err := h.ctrl.GetToken(ctx, query)
if err != nil {
return "", err
}

return result.Token, nil
}
20 changes: 16 additions & 4 deletions internal/accounts/controllers/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package controllers
import (
"context"

"github.com/zeiss/typhoon/internal/accounts/models"
"github.com/zeiss/typhoon/internal/accounts/ports"
"github.com/zeiss/typhoon/internal/api/models"
)

// GetTokenQuery ...
type GetTokenQuery struct {
AccountPublicKey string `json:"account_public_key" validate:"required"`
}

// AccountsController ...
type AccountsController interface {
GetToken(ctx context.Context, accountPublicKey models.AccountPublicKey) (models.AccountToken, error)
GetToken(ctx context.Context, query GetTokenQuery) (models.Token, error)
}

// AccountsController ...
Expand All @@ -23,6 +28,13 @@ func NewAccountsController(db ports.Accounts) *accountsController {
}

// GetToken ...
func (c *accountsController) GetToken(ctx context.Context, pubkey models.AccountPublicKey) (models.AccountToken, error) {
return c.db.GetToken(ctx, pubkey)
func (c *accountsController) GetToken(ctx context.Context, query GetTokenQuery) (models.Token, error) {
token := models.Token{ID: query.AccountPublicKey}

err := c.db.GetToken(ctx, &token)
if err != nil {
return token, err
}

return token, nil
}
4 changes: 2 additions & 2 deletions internal/accounts/ports/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package ports
import (
"context"

"github.com/zeiss/typhoon/internal/accounts/models"
"github.com/zeiss/typhoon/internal/api/models"
)

// Accounts ...
type Accounts interface {
// GetToken returns the token for the given account.
GetToken(ctx context.Context, account models.AccountPublicKey) (models.AccountToken, error)
GetToken(ctx context.Context, account *models.Token) error
}
9 changes: 0 additions & 9 deletions internal/accounts/services/lookup.go

This file was deleted.

0 comments on commit 3f17218

Please sign in to comment.