Skip to content

Commit

Permalink
Merge pull request #269 from systemli/Refactor-Upload-Config
Browse files Browse the repository at this point in the history
♻️ Refactor Upload Config
  • Loading branch information
0x46616c6b authored Oct 29, 2023
2 parents 810bf47 + 32cb651 commit d565126
Show file tree
Hide file tree
Showing 27 changed files with 173 additions and 168 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func initConfig() {
if err != nil {
log.WithError(err).Fatal("could not connect to database")
}
store = storage.NewSqlStorage(db, cfg.UploadPath)
store = storage.NewSqlStorage(db, cfg.Upload.Path)
if err := storage.MigrateDB(db); err != nil {
log.WithError(err).Fatal("could not migrate database")
}
Expand Down
9 changes: 5 additions & 4 deletions config.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ telegram:
token: ""
# listen port for prometheus metrics exporter
metrics_listen: ":8181"
# path where to store the uploaded files
upload_path: "uploads"
# base url for uploaded assets
upload_url: "http://localhost:8080"
upload:
# path where to store the uploaded files
path: "uploads"
# base url for uploaded assets
url: "http://localhost:8080"
9 changes: 5 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ telegram:
token: ""
# listen port for prometheus metrics exporter
metrics_listen: ":8181"
# path where to store the uploaded files
upload_path: "uploads"
# base url for uploaded assets
upload_url: "http://localhost:8080"
upload:
# path where to store the uploaded files
path: "uploads"
# base url for uploaded assets
url: "http://localhost:8080"
```
!!! note
Expand Down
11 changes: 5 additions & 6 deletions docs/quick-install-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ Fill your config file with the following content:
listen: "localhost:8080"
# log_level sets log level for logrus
log_level: "error"
# initiator is the email for the first admin user (see password in logs)
initiator: "<your e-mail>"
# configuration for the database
database:
type: "sqlite" # postgres, mysql, sqlite
Expand All @@ -78,10 +76,11 @@ database:
secret: "<your special little secret> (make it LOOOONG!)"
# listen port for prometheus metrics exporter
metrics_listen: ":8181"
# path where to store the uploaded files
upload_path: "uploads"
# base url for uploaded assets
upload_url: "https://api.domain.tld"
upload:
# path where to store the uploaded files
path: "uploads"
# base url for uploaded assets
url: "https://api.domain.tld"
```
2. Create a systemd Task (see [docs/ticker-api.service](assets/ticker-api.service) for reference)
Expand Down
11 changes: 5 additions & 6 deletions docs/quick-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ Fill your config file with the following content:
listen: "localhost:8080"
# log_level sets log level for logrus
log_level: "error"
# initiator is the email for the first admin user (see password in logs)
initiator: "<your e-mail>"
# configuration for the database
database:
type: "sqlite" # postgres, mysql, sqlite
Expand All @@ -70,10 +68,11 @@ database:
secret: "<your special little secret> (make it LOOOONG!)"
# listen port for prometheus metrics exporter
metrics_listen: ":8181"
# path where to store the uploaded files
upload_path: "uploads"
# base url for uploaded assets
upload_url: "https://api.domain.tld"
upload:
# path where to store the uploaded files
path: "uploads"
# base url for uploaded assets
url: "https://api.domain.tld"
```
2. Create a systemd Task (see [docs/ticker-api.service](assets/ticker-api.service) for reference)
Expand Down
6 changes: 3 additions & 3 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {
}

func TestHealthz(t *testing.T) {
c := config.NewConfig()
c := config.LoadConfig("")
s := &storage.MockStorage{}
r := API(c, s, l)

Expand All @@ -41,7 +41,7 @@ func TestHealthz(t *testing.T) {
}

func TestLoginNotSuccessful(t *testing.T) {
c := config.NewConfig()
c := config.LoadConfig("")
s := &storage.MockStorage{}
user := storage.User{}
user.UpdatePassword("password")
Expand All @@ -64,7 +64,7 @@ func TestLoginNotSuccessful(t *testing.T) {
}

func TestLoginSuccessful(t *testing.T) {
c := config.NewConfig()
c := config.LoadConfig("")
s := &storage.MockStorage{}
user := storage.User{}
user.UpdatePassword("password")
Expand Down
2 changes: 1 addition & 1 deletion internal/api/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestGetFeatures(t *testing.T) {

h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetFeatures(c)
Expand Down
6 changes: 3 additions & 3 deletions internal/api/feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestGetFeedTickerNotFound(t *testing.T) {
s := &storage.MockStorage{}
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetFeed(c)
Expand All @@ -42,7 +42,7 @@ func TestGetFeedMessageFetchError(t *testing.T) {

h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetFeed(c)
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestGetFeed(t *testing.T) {

h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetFeed(c)
Expand Down
6 changes: 3 additions & 3 deletions internal/api/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestGetInit(t *testing.T) {

h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetInit(c)
Expand All @@ -43,7 +43,7 @@ func TestGetInitInvalidDomain(t *testing.T) {

h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetInit(c)
Expand All @@ -64,7 +64,7 @@ func TestGetInitInactiveTicker(t *testing.T) {

h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetInit(c)
Expand Down
4 changes: 2 additions & 2 deletions internal/api/media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestGetMedia(t *testing.T) {

h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetMedia(c)
Expand All @@ -47,7 +47,7 @@ func TestGetMediaNotFound(t *testing.T) {

h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}
h.GetMedia(c)

Expand Down
30 changes: 15 additions & 15 deletions internal/api/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestGetMessagesTickerNotFound(t *testing.T) {
s := &storage.MockStorage{}
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetMessages(c)
Expand All @@ -42,7 +42,7 @@ func TestGetMessagesStorageError(t *testing.T) {
s.On("FindMessagesByTickerAndPagination", mock.Anything, mock.Anything, mock.Anything).Return([]storage.Message{}, errors.New("storage error"))
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetMessages(c)
Expand All @@ -58,7 +58,7 @@ func TestGetMessagesEmptyResult(t *testing.T) {
s.On("FindMessagesByTickerAndPagination", mock.Anything, mock.Anything, mock.Anything).Return([]storage.Message{}, errors.New("not found"))
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetMessages(c)
Expand All @@ -74,7 +74,7 @@ func TestGetMessages(t *testing.T) {
s.On("FindMessagesByTickerAndPagination", mock.Anything, mock.Anything, mock.Anything).Return([]storage.Message{}, nil)
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetMessages(c)
Expand All @@ -88,7 +88,7 @@ func TestGetMessageNotFound(t *testing.T) {
s := &storage.MockStorage{}
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetMessage(c)
Expand All @@ -103,7 +103,7 @@ func TestGetMessage(t *testing.T) {
s := &storage.MockStorage{}
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.GetMessage(c)
Expand All @@ -117,7 +117,7 @@ func TestPostMessageTickerNotFound(t *testing.T) {
s := &storage.MockStorage{}
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.PostMessage(c)
Expand All @@ -133,7 +133,7 @@ func TestPostMessageFormError(t *testing.T) {
s := &storage.MockStorage{}
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.PostMessage(c)
Expand All @@ -153,7 +153,7 @@ func TestPostMessageUploadsNotFound(t *testing.T) {
s.On("FindUploadsByIDs", mock.Anything).Return([]storage.Upload{}, errors.New("storage error"))
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.PostMessage(c)
Expand All @@ -176,7 +176,7 @@ func TestPostMessageStorageError(t *testing.T) {
b.On("Send", mock.Anything, mock.Anything).Return(nil)
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
bridges: bridge.Bridges{"mock": b},
}

Expand All @@ -199,7 +199,7 @@ func TestPostMessage(t *testing.T) {
b.On("Send", mock.Anything, mock.Anything).Return(nil)
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
bridges: bridge.Bridges{"mock": b},
}

Expand All @@ -214,7 +214,7 @@ func TestDeleteMessageTickerNotFound(t *testing.T) {
s := &storage.MockStorage{}
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.DeleteMessage(c)
Expand All @@ -229,7 +229,7 @@ func TestDeleteMessageMessageNotFound(t *testing.T) {
s := &storage.MockStorage{}
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
}

h.DeleteMessage(c)
Expand All @@ -248,7 +248,7 @@ func TestDeleteMessageStorageError(t *testing.T) {
b.On("Delete", mock.Anything, mock.Anything).Return(nil)
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
bridges: bridge.Bridges{"mock": b},
}

Expand All @@ -268,7 +268,7 @@ func TestDeleteMessage(t *testing.T) {
b.On("Delete", mock.Anything, mock.Anything).Return(nil)
h := handler{
storage: s,
config: config.NewConfig(),
config: config.LoadConfig(""),
bridges: bridge.Bridges{"mock": b},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/api/response/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func MessageResponse(message storage.Message, config config.Config) Message {

for _, attachment := range message.Attachments {
name := fmt.Sprintf("%s.%s", attachment.UUID, attachment.Extension)
attachments = append(attachments, MessageAttachment{URL: MediaURL(config.UploadURL, name), ContentType: attachment.ContentType})
attachments = append(attachments, MessageAttachment{URL: MediaURL(config.Upload.URL, name), ContentType: attachment.ContentType})
}

return Message{
Expand Down
2 changes: 1 addition & 1 deletion internal/api/response/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMessagesResponse(t *testing.T) {
config := config.Config{UploadURL: "https://upload.example.com"}
config := config.Config{Upload: config.Upload{URL: "https://upload.example.com"}}
message := storage.NewMessage()
message.Attachments = []storage.Attachment{{UUID: "uuid", Extension: "jpg"}}

Expand Down
2 changes: 1 addition & 1 deletion internal/api/response/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TimelineResponse(messages []storage.Message, config config.Config) []Timeli
var attachments []Attachment
for _, attachment := range message.Attachments {
name := fmt.Sprintf("%s.%s", attachment.UUID, attachment.Extension)
attachments = append(attachments, Attachment{URL: MediaURL(config.UploadURL, name), ContentType: attachment.ContentType})
attachments = append(attachments, Attachment{URL: MediaURL(config.Upload.URL, name), ContentType: attachment.ContentType})
}

timeline = append(timeline, TimelineEntry{
Expand Down
2 changes: 1 addition & 1 deletion internal/api/response/timeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestTimelineResponse(t *testing.T) {
config := config.Config{UploadURL: "https://upload.example.com"}
config := config.Config{Upload: config.Upload{URL: "https://upload.example.com"}}
message := storage.NewMessage()
message.Attachments = []storage.Attachment{{UUID: "uuid", Extension: "jpg"}}

Expand Down
2 changes: 1 addition & 1 deletion internal/api/response/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func UploadResponse(upload storage.Upload, config config.Config) Upload {
ID: upload.ID,
UUID: upload.UUID,
CreatedAt: upload.CreatedAt,
URL: upload.URL(config.UploadURL),
URL: upload.URL(config.Upload.URL),
ContentType: upload.ContentType,
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/api/response/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
var (
u = storage.NewUpload("image.jpg", "image/jpg", 1)
c = config.Config{
UploadURL: "http://localhost:8080",
Upload: config.Upload{URL: "http://localhost:8080"},
}
)

func TestUploadResponse(t *testing.T) {
response := UploadResponse(u, c)

assert.Equal(t, fmt.Sprintf("%s/media/%s", c.UploadURL, u.FileName()), response.URL)
assert.Equal(t, fmt.Sprintf("%s/media/%s", c.Upload.URL, u.FileName()), response.URL)
}

func TestUploadsResponse(t *testing.T) {
Expand Down
Loading

0 comments on commit d565126

Please sign in to comment.