aboutsummaryrefslogtreecommitdiff
path: root/api/internal/models
diff options
context:
space:
mode:
authorZach Berwaldt <zberwaldt@tutamail.com>2024-03-07 19:16:07 -0500
committerZach Berwaldt <zberwaldt@tutamail.com>2024-03-07 19:16:07 -0500
commit6651daca670664f3de8af9c7bcb74b1e7c6c6be9 (patch)
treef1bb35ef8b0e1d498842a17b84c87c6ee996274e /api/internal/models
parent5fa57845052655883120ba4d19a85d8756fb8d8c (diff)
Add CORS middleware and authentication middleware to the API server.
The `setupRouter` function in `main.go` now includes a CORS middleware and a token authentication middleware. The CORS middleware allows cross-origin resource sharing by setting the appropriate response headers. The token authentication middleware checks for the presence of an `Authorization` header with a valid bearer token. If the token is missing or invalid, an unauthorized response is returned. In addition to these changes, a new test file `main_test.go` has been added to test the `/api/v1/auth` route. This test suite includes two test cases: one for successful authentication and one for failed authentication. Update go.mod to include new dependencies. The `go.mod` file has been modified to include two new dependencies: `github.com/spf13/viper` and `github.com/stretchr/testify`. Ignore go.sum changes. Ignore changes in the `go.sum` file, as they only include updates to existing dependencies.
Diffstat (limited to 'api/internal/models')
-rw-r--r--api/internal/models/auth.go11
-rw-r--r--api/internal/models/preferences.go14
-rw-r--r--api/internal/models/statistics.go26
-rw-r--r--api/internal/models/user.go10
4 files changed, 61 insertions, 0 deletions
diff --git a/api/internal/models/auth.go b/api/internal/models/auth.go
new file mode 100644
index 0000000..41344d5
--- /dev/null
+++ b/api/internal/models/auth.go
@@ -0,0 +1,11 @@
1package models
2
3import "time"
4
5type Token struct {
6 ID int64 `json:"-"`
7 UserID int64 `json:"user_id"`
8 Token string `json:"token"`
9 CreatedAt time.Time `json:"created_at"`
10 ExpiredAt time.Time `json:"expired_at"`
11}
diff --git a/api/internal/models/preferences.go b/api/internal/models/preferences.go
new file mode 100644
index 0000000..cbbd47c
--- /dev/null
+++ b/api/internal/models/preferences.go
@@ -0,0 +1,14 @@
1package models
2
3type Preference struct {
4 ID int64 `json:"-"`
5 Color string `json:"color"`
6 UserID int64 `json:"-"`
7 Size Size `json:"size"`
8}
9
10type Size struct {
11 ID int64 `json:"-"`
12 Size int64 `json:"size"`
13 Unit string `json:"unit"`
14}
diff --git a/api/internal/models/statistics.go b/api/internal/models/statistics.go
new file mode 100644
index 0000000..457e6a0
--- /dev/null
+++ b/api/internal/models/statistics.go
@@ -0,0 +1,26 @@
1package models
2
3import "time"
4
5type Statistic struct {
6 ID int64 `json:"-"`
7 Date time.Time `json:"date"`
8 User User `json:"user"`
9 Quantity int `json:"quantity"`
10}
11
12type StatisticPost struct {
13 Date time.Time `json:"date"`
14 Quantity int64 `json:"quantity"`
15 UserID int64 `json:"user_id"`
16}
17
18type WeeklyStatistic struct {
19 Date string `json:"date"`
20 Total int64 `json:"total"`
21}
22
23type DailyUserTotals struct {
24 Name string `json:"name"`
25 Total int64 `json:"total"`
26}
diff --git a/api/internal/models/user.go b/api/internal/models/user.go
new file mode 100644
index 0000000..2a3e6fd
--- /dev/null
+++ b/api/internal/models/user.go
@@ -0,0 +1,10 @@
1package models
2
3import "github.com/google/uuid"
4
5type User struct {
6 ID int64 `json:"-"`
7 Name string `json:"name"`
8 UUID uuid.UUID `json:"uuid"`
9 Password string `json:"-"`
10}