Skip to content

Commit

Permalink
refactor: rename domain -> core
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Jan 3, 2022
1 parent ba12844 commit 8580c6a
Show file tree
Hide file tree
Showing 27 changed files with 137 additions and 137 deletions.
30 changes: 15 additions & 15 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package app

import "github.com/ItsNotGoodName/smtpbridge/domain"
import "github.com/ItsNotGoodName/smtpbridge/core"

type App struct {
attachmentREPO domain.AttachmentRepositoryPort
messageREPO domain.MessageRepositoryPort
endpointREPO domain.EndpointRepositoryPort
authSVC domain.AuthServicePort
bridgeSVC domain.BridgeServicePort
endpointSVC domain.EndpointServicePort
messageSVC domain.MessageServicePort
attachmentREPO core.AttachmentRepositoryPort
messageREPO core.MessageRepositoryPort
endpointREPO core.EndpointRepositoryPort
authSVC core.AuthServicePort
bridgeSVC core.BridgeServicePort
endpointSVC core.EndpointServicePort
messageSVC core.MessageServicePort
}

func New(
attachmentREPO domain.AttachmentRepositoryPort,
messageREPO domain.MessageRepositoryPort,
endpointREPO domain.EndpointRepositoryPort,
authSVC domain.AuthServicePort,
bridgeSVC domain.BridgeServicePort,
endpointSVC domain.EndpointServicePort,
messageSVC domain.MessageServicePort,
attachmentREPO core.AttachmentRepositoryPort,
messageREPO core.MessageRepositoryPort,
endpointREPO core.EndpointRepositoryPort,
authSVC core.AuthServicePort,
bridgeSVC core.BridgeServicePort,
endpointSVC core.EndpointServicePort,
messageSVC core.MessageServicePort,
) *App {
return &App{
attachmentREPO,
Expand Down
20 changes: 10 additions & 10 deletions app/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package app
import (
"time"

"github.com/ItsNotGoodName/smtpbridge/domain"
"github.com/ItsNotGoodName/smtpbridge/core"
)

type Message struct {
UUID string `json:"uuid"`
Status domain.Status `json:"status"`
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Text string `json:"text"`
CreatedAt string `json:"created_at"`
Attachments []Attachment `json:"attachments"`
UUID string `json:"uuid"`
Status core.Status `json:"status"`
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Text string `json:"text"`
CreatedAt string `json:"created_at"`
Attachments []Attachment `json:"attachments"`
}

func NewMessage(msg *domain.Message) *Message {
func NewMessage(msg *core.Message) *Message {
var attachments []Attachment
for _, attachment := range msg.Attachments {
attachments = append(attachments, Attachment{
Expand Down
4 changes: 2 additions & 2 deletions app/message_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package app
import (
"fmt"

"github.com/ItsNotGoodName/smtpbridge/domain"
"github.com/ItsNotGoodName/smtpbridge/core"
)

type messageSendRequest struct {
Message *domain.Message
Message *core.Message
}

func (a *App) messageSend(req *messageSendRequest) error {
Expand Down
4 changes: 2 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package cmd
import (
"github.com/ItsNotGoodName/smtpbridge/app"
"github.com/ItsNotGoodName/smtpbridge/config"
"github.com/ItsNotGoodName/smtpbridge/domain"
"github.com/ItsNotGoodName/smtpbridge/core"
"github.com/ItsNotGoodName/smtpbridge/left/router"
"github.com/ItsNotGoodName/smtpbridge/left/smtp"
"github.com/ItsNotGoodName/smtpbridge/left/web"
Expand All @@ -47,7 +47,7 @@ var serverCmd = &cobra.Command{
serverConfig.Load()

// Init database
var db domain.Database
var db core.Database
if serverConfig.DB.IsBolt() {
db = repository.NewDatabase(serverConfig)
} else {
Expand Down
2 changes: 1 addition & 1 deletion domain/attachment.go → core/attachment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package core

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion domain/auth.go → core/auth.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package core

import "fmt"

Expand Down
2 changes: 1 addition & 1 deletion domain/bridge.go → core/bridge.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package core

import "fmt"

Expand Down
2 changes: 1 addition & 1 deletion domain/database.go → core/database.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package core

type Database interface {
Close() error
Expand Down
2 changes: 1 addition & 1 deletion domain/endpoint.go → core/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package core

import "fmt"

Expand Down
2 changes: 1 addition & 1 deletion domain/error.go → core/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package core

import "errors"

Expand Down
6 changes: 3 additions & 3 deletions domain/filter.go → core/filter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package core

import (
"log"
Expand All @@ -19,13 +19,13 @@ func NewFilter(to, from, toRegex, fromRegex string) Filter {
if toRegex != "" {
toRegexp, err = regexp.Compile(toRegex)
if err != nil {
log.Fatalln("domain.NewFilter: bad to regex:", err)
log.Fatalln("core.NewFilter: bad to regex:", err)
}
}
if fromRegex != "" {
fromRegexp, err = regexp.Compile(fromRegex)
if err != nil {
log.Fatalln("domain.NewFilter: bad from regex:", err)
log.Fatalln("core.NewFilter: bad from regex:", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion domain/filter_test.go → core/filter_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package core

import "testing"

Expand Down
2 changes: 1 addition & 1 deletion domain/message.go → core/message.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package core

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions right/endpoint/end_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package endpoint
import (
"fmt"

"github.com/ItsNotGoodName/smtpbridge/domain"
"github.com/ItsNotGoodName/smtpbridge/core"
)

type Mock struct{}
Expand All @@ -12,7 +12,7 @@ func NewMock() (*Mock, error) {
return &Mock{}, nil
}

func (m *Mock) Send(message *domain.EndpointMessage) error {
func (m *Mock) Send(message *core.EndpointMessage) error {
fmt.Printf("endpoint.Mock.Send: %+v\n", message)
return nil
}
4 changes: 2 additions & 2 deletions right/endpoint/end_telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"net/url"

"github.com/ItsNotGoodName/smtpbridge/domain"
"github.com/ItsNotGoodName/smtpbridge/core"
)

type Telegram struct {
Expand All @@ -28,7 +28,7 @@ type TelegramResponse struct {
Description string `json:"description"`
}

func (t *Telegram) Send(msg *domain.EndpointMessage) error {
func (t *Telegram) Send(msg *core.EndpointMessage) error {
if len(msg.Attachments) == 0 {
return t.sendMessage(msg.Text)
}
Expand Down
10 changes: 5 additions & 5 deletions right/endpoint/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ package endpoint
import (
"fmt"

"github.com/ItsNotGoodName/smtpbridge/domain"
"github.com/ItsNotGoodName/smtpbridge/core"
)

const (
NameTelegram = "telegram"
NameMock = "mock"
)

func factory(endpointType string, config map[string]string) (domain.EndpointPort, error) {
func factory(endpointType string, config map[string]string) (core.EndpointPort, error) {
switch endpointType {
case NameTelegram:
return telegramFactory(config)
case NameMock:
return NewMock()
}

return nil, fmt.Errorf("%v: %s", domain.ErrEndpointInvalidType, endpointType)
return nil, fmt.Errorf("%v: %s", core.ErrEndpointInvalidType, endpointType)
}

func telegramFactory(config map[string]string) (*Telegram, error) {
token, ok := config["token"]
if !ok {
return nil, fmt.Errorf("%v: %s: token not found", domain.ErrEndpointInvalidConfig, NameTelegram)
return nil, fmt.Errorf("%v: %s: token not found", core.ErrEndpointInvalidConfig, NameTelegram)
}

chatID, ok := config["chat_id"]
if !ok {
return nil, fmt.Errorf("%v: %s: chat_id not found", domain.ErrEndpointInvalidConfig, NameTelegram)
return nil, fmt.Errorf("%v: %s: chat_id not found", core.ErrEndpointInvalidConfig, NameTelegram)
}

return NewTelegram(token, chatID), nil
Expand Down
12 changes: 6 additions & 6 deletions right/endpoint/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"sync"

"github.com/ItsNotGoodName/smtpbridge/config"
"github.com/ItsNotGoodName/smtpbridge/domain"
"github.com/ItsNotGoodName/smtpbridge/core"
)

type Repository struct {
endpointMu sync.RWMutex
endpointMap map[string]domain.EndpointPort
endpointMap map[string]core.EndpointPort
}

func NewRepository(cfg *config.Config) *Repository {
r := Repository{
endpointMap: make(map[string]domain.EndpointPort),
endpointMap: make(map[string]core.EndpointPort),
endpointMu: sync.RWMutex{},
}

Expand All @@ -30,13 +30,13 @@ func NewRepository(cfg *config.Config) *Repository {
return &r
}

func (r *Repository) Get(name string) (domain.EndpointPort, error) {
func (r *Repository) Get(name string) (core.EndpointPort, error) {
r.endpointMu.RLock()
defer r.endpointMu.RUnlock()

endpoint, ok := r.endpointMap[name]
if !ok {
return nil, fmt.Errorf("%v: %s", domain.ErrEndpointNotFound, name)
return nil, fmt.Errorf("%v: %s", core.ErrEndpointNotFound, name)
}

return endpoint, nil
Expand All @@ -47,7 +47,7 @@ func (r *Repository) Create(name, endpointType string, config map[string]string)
defer r.endpointMu.Unlock()

if _, ok := r.endpointMap[name]; ok {
return fmt.Errorf("%v: %s", domain.ErrEndpointNameConflict, name)
return fmt.Errorf("%v: %s", core.ErrEndpointNameConflict, name)
}

endpoint, err := factory(endpointType, config)
Expand Down
32 changes: 16 additions & 16 deletions right/repository/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"path"

"github.com/ItsNotGoodName/smtpbridge/config"
"github.com/ItsNotGoodName/smtpbridge/domain"
"github.com/ItsNotGoodName/smtpbridge/core"
"github.com/asdine/storm"
"github.com/asdine/storm/q"
)

type attachmentModel struct {
UUID string `storm:"id"`
Name string ``
Type domain.AttachmentType ``
MessageUUID string ``
UUID string `storm:"id"`
Name string ``
Type core.AttachmentType ``
MessageUUID string ``
}

func convertAttachmentD(att *domain.Attachment) *attachmentModel {
func convertAttachmentD(att *core.Attachment) *attachmentModel {
return &attachmentModel{
UUID: att.UUID,
Name: att.Name,
Expand All @@ -28,8 +28,8 @@ func convertAttachmentD(att *domain.Attachment) *attachmentModel {
}
}

func convertAttachmentM(attM *attachmentModel) *domain.Attachment {
return &domain.Attachment{
func convertAttachmentM(attM *attachmentModel) *core.Attachment {
return &core.Attachment{
UUID: attM.UUID,
Name: attM.Name,
Type: attM.Type,
Expand All @@ -56,7 +56,7 @@ func NewAttachment(cfg *config.Config, db *storm.DB) *Attachment {
}
}

func (a *Attachment) Create(att *domain.Attachment) error {
func (a *Attachment) Create(att *core.Attachment) error {
err := a.db.Save(convertAttachmentD(att))
if err != nil {
return err
Expand All @@ -66,15 +66,15 @@ func (a *Attachment) Create(att *domain.Attachment) error {
}

// getAttachmentPath returns the path to the attachment file on the file system.
func (a *Attachment) getPath(att *domain.Attachment) string {
func (a *Attachment) getPath(att *core.Attachment) string {
return path.Join(a.attDir, att.File())
}

func (a *Attachment) GetFS() fs.FS {
return a.fs
}

func (a *Attachment) Get(uuid string) (*domain.Attachment, error) {
func (a *Attachment) Get(uuid string) (*core.Attachment, error) {
var attM *attachmentModel
err := a.db.One("UUID", uuid, attM)
if err != nil {
Expand All @@ -84,7 +84,7 @@ func (a *Attachment) Get(uuid string) (*domain.Attachment, error) {
return convertAttachmentM(attM), nil
}

func (a *Attachment) GetData(att *domain.Attachment) ([]byte, error) {
func (a *Attachment) GetData(att *core.Attachment) ([]byte, error) {
data, err := os.ReadFile(a.getPath(att))
if err != nil {
return nil, err
Expand All @@ -93,24 +93,24 @@ func (a *Attachment) GetData(att *domain.Attachment) ([]byte, error) {
return data, nil
}

func (a *Attachment) ListByMessage(msg *domain.Message) ([]domain.Attachment, error) {
func (a *Attachment) ListByMessage(msg *core.Message) ([]core.Attachment, error) {
var attsM []attachmentModel
err := a.db.Select(q.Eq("MessageUUID", msg.UUID)).Find(&attsM)
if err != nil {
if err == storm.ErrNotFound {
return []domain.Attachment{}, nil
return []core.Attachment{}, nil
}
return nil, err
}

var atts []domain.Attachment
var atts []core.Attachment
for _, attM := range attsM {
atts = append(atts, *convertAttachmentM(&attM))
}

return atts, nil
}

func (a *Attachment) DeleteData(att *domain.Attachment) error {
func (a *Attachment) DeleteData(att *core.Attachment) error {
return os.Remove(a.getPath(att))
}
Loading

0 comments on commit 8580c6a

Please sign in to comment.