/
api.go
108 lines (80 loc) · 2.18 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package service
import (
"errors"
"net/http"
"net/url"
"github.com/elahe-dastan/HTTP_monitoring/authentication"
"github.com/elahe-dastan/HTTP_monitoring/config"
"github.com/elahe-dastan/HTTP_monitoring/model"
"github.com/elahe-dastan/HTTP_monitoring/request"
"github.com/elahe-dastan/HTTP_monitoring/store"
"github.com/labstack/echo/v4"
)
var ErrLoggedOut = errors.New("you are not logged in")
type API struct {
User store.User
URL store.URL
Config config.JWT
}
func (a API) Run() {
e := echo.New()
e.POST("/register", a.Register)
e.POST("/login", a.Login)
e.POST("/url", a.Add)
e.Logger.Fatal(e.Start(":8080"))
}
func (a API) Register(c echo.Context) error {
var user model.User
if err := c.Bind(&user); err != nil {
return err
}
if user.Email == "" || user.Password == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Email and password cannot be empty")
}
//nolint: errcheck
if err := a.User.Insert(user); err != nil {
c.JSON(http.StatusConflict, err.Error())
}
return c.JSON(http.StatusCreated, user)
}
func (a API) Login(c echo.Context) error {
var user model.User
if err := c.Bind(&user); err != nil {
return err
}
if user.Email == "" || user.Password == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Email and password cannot be empty")
}
us, err := a.User.Retrieve(user)
if err != nil {
return c.JSON(http.StatusNotFound, err.Error())
}
token, err := authentication.CreateToken(us.ID, a.Config)
if err != nil {
return err
}
return c.JSON(http.StatusOK, token)
}
func (a API) Add(c echo.Context) error {
var newURL request.URL
token := c.Request().Header.Get("Authorization")
if err := c.Bind(&newURL); err != nil {
return err
}
in, id := authentication.ValidateToken(token, a.Config)
if !in {
return echo.NewHTTPError(http.StatusForbidden, ErrLoggedOut.Error())
}
_, err := url.ParseRequestURI(newURL.URL)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
var u model.URL
u.UserID = id
u.URL = newURL.URL
u.Period = newURL.Period
if err := a.URL.Insert(u); err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusCreated, u)
}