Skip to content

Commit

Permalink
refactor: message, database, and router
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Dec 30, 2021
1 parent 719bd64 commit 6775eee
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 169 deletions.
File renamed without changes.
8 changes: 7 additions & 1 deletion app/message_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ type MessageSendRequest struct {
}

func (a *App) MessageSend(req *MessageSendRequest) error {
return a.endpointSVC.SendBridges(req.Message, a.bridgeSVC.GetBridges(req.Message))
err := a.endpointSVC.SendBridges(req.Message, a.bridgeSVC.GetBridges(req.Message))
if err != nil {
a.messageSVC.UpdateStatus(req.Message, domain.StatusFailed)
return err
}

return a.messageSVC.UpdateStatus(req.Message, domain.StatusSent)
}
50 changes: 45 additions & 5 deletions app/message_list.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
package app

import "github.com/ItsNotGoodName/smtpbridge/domain"
import (
"path"

"github.com/ItsNotGoodName/smtpbridge/dto"
)

type MessageListRequest struct {
Limit int
Offset int
Page int
AttachmentPath string
}

func (a *App) MessageList(req *MessageListRequest) ([]domain.Message, error) {
return a.messageSVC.List(req.Limit, req.Offset)
func (a *App) MessageList(req *MessageListRequest) ([]dto.Message, error) {
if req.Page < 0 {
req.Page = 0
}

msgs, err := a.messageSVC.List(10, req.Page*10)
if err != nil {
return nil, err
}

var result []dto.Message
for _, msg := range msgs {
var attachments []dto.Attachment
for _, attachment := range msg.Attachments {
attachments = append(attachments, dto.Attachment{
UUID: attachment.UUID,
Name: attachment.Name,
Path: path.Join(req.AttachmentPath, a.dao.Attachment.GetAttachmentFile(&attachment)),
})
}

var to []string
for toAddr := range msg.To {
to = append(to, toAddr)
}

result = append(result, dto.Message{
UUID: msg.UUID,
From: msg.From,
To: to,
Status: msg.Status.String(),
Subject: msg.Subject,
Text: msg.Text,
Attachments: attachments,
})
}

return result, nil
}
10 changes: 10 additions & 0 deletions domain/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ const (
StatusFailed
)

var status []string = []string{
"created",
"sent",
"failed",
}

func (s Status) String() string {
return status[s]
}

func NewMessage(subject, from string, to map[string]bool, text string) *Message {
return &Message{
CreatedAt: time.Now(),
Expand Down
17 changes: 17 additions & 0 deletions dto/dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dto

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

type Attachment struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Path string `json:"path"`
}
17 changes: 11 additions & 6 deletions left/router/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"net/http"

"github.com/ItsNotGoodName/smtpbridge/app"
"github.com/ItsNotGoodName/smtpbridge/domain"
"github.com/ItsNotGoodName/smtpbridge/dto"
)

func (s *Router) GetAttachments(prefix string) http.HandlerFunc {
func (s *Router) GetAttachments() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
http.StripPrefix(prefix, http.FileServer(http.FS(s.a.AttachmentGetFS()))).ServeHTTP(rw, r)
http.StripPrefix(s.attachmentURI, http.FileServer(http.FS(s.a.AttachmentGetFS()))).ServeHTTP(rw, r)
}
}

Expand All @@ -21,7 +21,7 @@ var templateFS embed.FS

func (s *Router) GetIndex() http.HandlerFunc {
type Data struct {
Messages []domain.Message
Messages []dto.Message
}

index, err := template.ParseFS(templateFS, "template/index.html")
Expand All @@ -30,12 +30,17 @@ func (s *Router) GetIndex() http.HandlerFunc {
}

return func(rw http.ResponseWriter, r *http.Request) {
messages, err := s.a.MessageList(&app.MessageListRequest{Limit: 10, Offset: 0})
messages, err := s.a.MessageList(&app.MessageListRequest{AttachmentPath: s.attachmentURI, Page: 0})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}

index.Execute(rw, Data{Messages: messages})
log.Println("router.Router.GetIndex:", len(messages), "messages")
err = index.Execute(rw, Data{Messages: messages})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
}
}
2 changes: 1 addition & 1 deletion left/router/route.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package router

func (s *Router) route() {
s.r.Get("/attachments/*", s.GetAttachments("/attachments/"))
s.r.Get(s.attachmentURI+"*", s.GetAttachments())
s.r.Get("/", s.GetIndex())
}
10 changes: 6 additions & 4 deletions left/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (
)

type Router struct {
r *chi.Mux
a *app.App
r *chi.Mux
a *app.App
attachmentURI string
}

func New(app *app.App) *Router {
s := Router{
r: chi.NewRouter(),
a: app,
r: chi.NewRouter(),
a: app,
attachmentURI: "/attachments/",
}

// A good base middleware stack
Expand Down
6 changes: 3 additions & 3 deletions left/router/template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h2>{{ $msg.Subject }}</h2>
{{ else }}
<h2>No Subject</h2>
{{ end }}
<div>Status: {{ $msg.Status }}</div>
<span>{{ $msg.Status }}</span>
<div>
<code>
<pre>{{ $msg.Text }}</pre>
Expand All @@ -26,8 +26,8 @@ <h2>No Subject</h2>
{{ range $att := $msg.Attachments }}
<img
width="300"
src="/attachments/{{ $att.UUID }}.{{ $att.Type }}"
alt="{{ $att.UUID }}.{{ $att.Type }}"
src="{{ $att.Path }}"
alt="{{ $att.UUID }}"
/>
{{ end }}
</div>
Expand Down
66 changes: 66 additions & 0 deletions right/database/attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package database

import (
"fmt"
"io/fs"
"os"
"path"

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

func (db *DB) CreateAttachment(att *domain.Attachment) error {
err := db.db.Save(att)
if err != nil {
return err
}

return os.WriteFile(db.getAttachmentPath(att), att.Data, 0644)
}

// getAttachmentPath returns the path to the attachment file on the file system.
func (db *DB) getAttachmentPath(att *domain.Attachment) string {
return path.Join(db.attDir, db.GetAttachmentFile(att))
}

func (db *DB) GetAttachmentFile(att *domain.Attachment) string {
return fmt.Sprintf("%s.%s", att.UUID, att.Type)
}

func (db *DB) GetAttachmentFS() fs.FS {
return db.fs
}

func (db *DB) GetAttachment(uuid string) (*domain.Attachment, error) {
var att domain.Attachment
err := db.db.One("UUID", uuid, att)
if err != nil {
return nil, err
}

return &att, nil
}

func (db *DB) GetAttachmentData(att *domain.Attachment) ([]byte, error) {
data, err := os.ReadFile(db.getAttachmentPath(att))
if err != nil {
return nil, err
}

return data, nil
}

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

return atts, nil
}
Loading

0 comments on commit 6775eee

Please sign in to comment.