Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions api/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/getkin/kin-openapi/openapi3"
"github.com/piquel-fr/api/config"
"github.com/piquel-fr/api/database"
"github.com/piquel-fr/api/database/repository"
"github.com/piquel-fr/api/services/auth"
"github.com/piquel-fr/api/services/email"
Expand Down Expand Up @@ -314,7 +313,7 @@ func (h *EmailHandler) handleAddAccount(w http.ResponseWriter, r *http.Request)
}

params.OwnerId = user.ID
if _, err = database.Queries.AddEmailAccount(r.Context(), params); err != nil {
if _, err = h.emailService.AddAccount(r.Context(), params); err != nil {
errors.HandleError(w, r, err)
return
}
Expand Down
31 changes: 0 additions & 31 deletions database/database.go

This file was deleted.

12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/piquel-fr/api/api"
"github.com/piquel-fr/api/config"
"github.com/piquel-fr/api/database"
"github.com/piquel-fr/api/services/auth"
"github.com/piquel-fr/api/services/email"
"github.com/piquel-fr/api/services/storage"
"github.com/piquel-fr/api/services/users"
gh "github.com/piquel-fr/api/utils/github"
"github.com/piquel-fr/api/utils/oauth"
Expand All @@ -22,12 +22,12 @@ func main() {
config.LoadConfig()
gh.InitGithubClient()
oauth.InitOAuth()
database.InitDatabase()
defer database.Connection.Close()

userService := users.NewRealUserService()
authService := auth.NewRealAuthService(userService)
emailService := email.NewRealEmailService()
storageService := storage.NewDatabaseStorageService()
defer storageService.Close()
userService := users.NewRealUserService(storageService)
authService := auth.NewRealAuthService(storageService, userService)
emailService := email.NewRealEmailService(storageService)

config.UsernameBlacklist = userService.GetUsernameBlacklist()
config.Policy = authService.GetPolicy()
Expand Down
23 changes: 12 additions & 11 deletions services/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/golang-jwt/jwt/v5"
"github.com/jackc/pgx/v5"
"github.com/piquel-fr/api/config"
"github.com/piquel-fr/api/database"
"github.com/piquel-fr/api/database/repository"
"github.com/piquel-fr/api/services/storage"
"github.com/piquel-fr/api/services/users"
"github.com/piquel-fr/api/utils"
"github.com/piquel-fr/api/utils/errors"
Expand Down Expand Up @@ -50,11 +50,12 @@ type AuthService interface {
}

type realAuthService struct {
userService users.UserService
userService users.UserService
storageService storage.StorageService
}

func NewRealAuthService(userService users.UserService) AuthService {
return &realAuthService{userService}
func NewRealAuthService(storageService storage.StorageService, userService users.UserService) AuthService {
return &realAuthService{userService, storageService}
}

func (s *realAuthService) GetPolicy() *config.PolicyConfiguration { return &policy }
Expand Down Expand Up @@ -87,7 +88,7 @@ func (s *realAuthService) FinishAuth(user *repository.User, r *http.Request, w h
IpAdress: ipAddress,
ExpiresAt: time.Now().Add(refreshExpiry), // one month
}
if _, err := database.Queries.AddSession(r.Context(), sessionParams); err != nil {
if _, err := s.storageService.AddSession(r.Context(), sessionParams); err != nil {
return err
}

Expand All @@ -101,7 +102,7 @@ func (s *realAuthService) Refresh(w http.ResponseWriter, r *http.Request) error
cookies := utils.GetCookiesFromStr(r.Header.Get("Cookie"))

hash := s.hashRefreshToken(cookies[refreshKey], ipAddress)
session, err := database.Queries.GetSessionFromHash(r.Context(), hash)
session, err := s.storageService.GetSessionFromHash(r.Context(), hash)
if errors.Is(err, pgx.ErrNoRows) {
return errors.ErrorNotAuthenticated
}
Expand Down Expand Up @@ -135,7 +136,7 @@ func (s *realAuthService) Refresh(w http.ResponseWriter, r *http.Request) error
TokenHash: refreshHash,
ExpiresAt: time.Now().Add(refreshExpiry),
}
if err := database.Queries.UpdateSession(r.Context(), updateSessionParams); err != nil {
if err := s.storageService.UpdateSession(r.Context(), updateSessionParams); err != nil {
return err
}

Expand All @@ -152,7 +153,7 @@ func (s *realAuthService) Logout(w http.ResponseWriter, r *http.Request) error {
w.Header().Add("Set-Cookie", utils.GenerateClearCookie(accessKey, config.Envs.Domain, "/"))

hash := s.hashRefreshToken(cookies[refreshKey], ipAddress)
return database.Queries.DeleteSessionByHash(r.Context(), hash)
return s.storageService.DeleteSessionByHash(r.Context(), hash)
}

func (s *realAuthService) generateAccessToken(user *repository.User, expiresAt time.Time) *jwt.Token {
Expand Down Expand Up @@ -226,13 +227,13 @@ func (s *realAuthService) AuthMiddleware(next http.Handler) http.Handler {
}

func (s *realAuthService) GetUserSessions(ctx context.Context, userId int32) ([]*repository.UserSession, error) {
return database.Queries.GetUserSessions(ctx, userId)
return s.storageService.GetUserSessions(ctx, userId)
}

func (s *realAuthService) DeleteUserSession(ctx context.Context, userId, id int32) error {
return database.Queries.DeleteSessionById(ctx, userId, id)
return s.storageService.DeleteSessionById(ctx, userId, id)
}

func (s *realAuthService) DeleteUserSessions(ctx context.Context, userId int32) error {
return database.Queries.ClearUserSessions(ctx, userId)
return s.storageService.ClearUserSessions(ctx, userId)
}
41 changes: 20 additions & 21 deletions services/email/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/emersion/go-imap/v2/imapclient"
"github.com/piquel-fr/api/database"
"github.com/piquel-fr/api/database/repository"
)

Expand All @@ -20,29 +19,29 @@ type AccountInfo struct {
Shares []string `json:"shares"`
}

func (r *realEmailService) GetAccountByEmail(ctx context.Context, email string) (*repository.MailAccount, error) {
return database.Queries.GetMailAccountByEmail(ctx, email)
func (s *realEmailService) GetAccountByEmail(ctx context.Context, email string) (*repository.MailAccount, error) {
return s.storageService.GetMailAccountByEmail(ctx, email)
}

func (r *realEmailService) ListAccounts(ctx context.Context, userId int32) ([]*repository.MailAccount, error) {
return database.Queries.ListUserMailAccounts(ctx, userId)
func (s *realEmailService) ListAccounts(ctx context.Context, userId int32) ([]*repository.MailAccount, error) {
return s.storageService.ListUserMailAccounts(ctx, userId)
}

func (r *realEmailService) CountAccounts(ctx context.Context, userId int32) (int64, error) {
return database.Queries.CountUserMailAccounts(ctx, userId)
func (s *realEmailService) CountAccounts(ctx context.Context, userId int32) (int64, error) {
return s.storageService.CountUserMailAccounts(ctx, userId)
}

func (r *realEmailService) AddAccount(ctx context.Context, params repository.AddEmailAccountParams) (int32, error) {
return database.Queries.AddEmailAccount(ctx, params)
func (s *realEmailService) AddAccount(ctx context.Context, params repository.AddEmailAccountParams) (int32, error) {
return s.storageService.AddEmailAccount(ctx, params)
}

func (r *realEmailService) RemoveAccount(ctx context.Context, accountId int32) error {
func (s *realEmailService) RemoveAccount(ctx context.Context, accountId int32) error {
// TODO: remove the shares as well
return database.Queries.DeleteMailAccount(ctx, accountId)
return s.storageService.DeleteMailAccount(ctx, accountId)
}

func (r *realEmailService) GetAccountInfo(ctx context.Context, account *repository.MailAccount) (AccountInfo, error) {
client, err := imapclient.DialTLS(r.imapAddr, nil)
func (s *realEmailService) GetAccountInfo(ctx context.Context, account *repository.MailAccount) (AccountInfo, error) {
client, err := imapclient.DialTLS(s.imapAddr, nil)
if err != nil {
return AccountInfo{}, err
}
Expand Down Expand Up @@ -73,13 +72,13 @@ func (r *realEmailService) GetAccountInfo(ctx context.Context, account *reposito
}

// get shares
shares, err := r.GetAccountShares(ctx, account.ID)
shares, err := s.GetAccountShares(ctx, account.ID)
if err != nil {
return AccountInfo{}, err
}

for _, share := range shares {
user, err := database.Queries.GetUserById(ctx, share)
user, err := s.storageService.GetUserById(ctx, share)
if err != nil {
return AccountInfo{}, err
}
Expand All @@ -89,14 +88,14 @@ func (r *realEmailService) GetAccountInfo(ctx context.Context, account *reposito
return accountInfo, nil
}

func (r *realEmailService) AddShare(ctx context.Context, params repository.AddShareParams) error {
return database.Queries.AddShare(ctx, params)
func (s *realEmailService) AddShare(ctx context.Context, params repository.AddShareParams) error {
return s.storageService.AddShare(ctx, params)
}

func (r *realEmailService) RemoveShare(ctx context.Context, userId, accountId int32) error {
return database.Queries.DeleteShare(ctx, userId, accountId)
func (s *realEmailService) RemoveShare(ctx context.Context, userId, accountId int32) error {
return s.storageService.DeleteShare(ctx, userId, accountId)
}

func (r *realEmailService) GetAccountShares(ctx context.Context, account int32) ([]int32, error) {
return database.Queries.ListAccountShares(ctx, account)
func (s *realEmailService) GetAccountShares(ctx context.Context, account int32) ([]int32, error) {
return s.storageService.ListAccountShares(ctx, account)
}
9 changes: 6 additions & 3 deletions services/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/piquel-fr/api/config"
"github.com/piquel-fr/api/database/repository"
"github.com/piquel-fr/api/services/storage"
)

type EmailService interface {
Expand All @@ -24,12 +25,14 @@ type EmailService interface {
}

type realEmailService struct {
imapAddr string
imapAddr string
storageService storage.StorageService
}

func NewRealEmailService() *realEmailService {
func NewRealEmailService(storageService storage.StorageService) *realEmailService {
addr := fmt.Sprintf("%s:%s", config.Envs.ImapHost, config.Envs.ImapPort)
return &realEmailService{
imapAddr: addr,
imapAddr: addr,
storageService: storageService,
}
}
45 changes: 45 additions & 0 deletions services/storage/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package storage

import (
"context"
"log"

"github.com/jackc/pgx/v5/pgxpool"
"github.com/piquel-fr/api/config"
"github.com/piquel-fr/api/database/repository"
)

type StorageService interface {
repository.Querier
Close()
}

type databaseStorageService struct {
repository.Queries
connection *pgxpool.Pool
}

func NewDatabaseStorageService() StorageService {
log.Printf("[Database] Attempting to connect to the database...\n")

connection, err := pgxpool.New(context.Background(), config.Envs.DBURL)
if err != nil {
panic(err)
}

if err = connection.Ping(context.Background()); err != nil {
panic(err)
}

queries := repository.New(connection)
log.Printf("[Database] Successfully connected to the database!\n")

return &databaseStorageService{
connection: connection,
Queries: *queries,
}
}

func (s *databaseStorageService) Close() {
s.connection.Close()
}
Loading