2018-11-12 16:46:35 +01:00
|
|
|
// @title Vikunja API
|
|
|
|
// @license.name GPLv3
|
|
|
|
// @BasePath /api/v1
|
|
|
|
|
|
|
|
// @securityDefinitions.basic BasicAuth
|
|
|
|
|
|
|
|
// @securityDefinitions.apikey ApiKeyAuth
|
|
|
|
// @in header
|
|
|
|
// @name Authorization
|
2018-06-10 11:11:41 +02:00
|
|
|
|
2018-08-29 15:22:17 +02:00
|
|
|
package routes
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
import (
|
2018-11-12 16:46:35 +01:00
|
|
|
_ "code.vikunja.io/api/docs" // To generate swagger docs
|
2018-11-17 00:17:37 +01:00
|
|
|
"code.vikunja.io/api/pkg/models"
|
2018-10-31 13:42:38 +01:00
|
|
|
apiv1 "code.vikunja.io/api/pkg/routes/api/v1"
|
|
|
|
"code.vikunja.io/api/pkg/routes/crud"
|
2018-11-17 00:17:37 +01:00
|
|
|
"github.com/asaskevich/govalidator"
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/middleware"
|
2018-09-08 13:29:35 +02:00
|
|
|
"github.com/spf13/viper"
|
2018-11-17 00:17:37 +01:00
|
|
|
"github.com/swaggo/echo-swagger"
|
2018-06-10 11:11:41 +02:00
|
|
|
)
|
|
|
|
|
2018-11-17 00:17:37 +01:00
|
|
|
// CustomValidator is a dummy struct to use govalidator with echo
|
|
|
|
type CustomValidator struct{}
|
|
|
|
|
|
|
|
// Validate validates stuff
|
|
|
|
func (cv *CustomValidator) Validate(i interface{}) error {
|
|
|
|
if _, err := govalidator.ValidateStruct(i); err != nil {
|
|
|
|
|
|
|
|
var errs []string
|
|
|
|
for field, e := range govalidator.ErrorsByField(err) {
|
|
|
|
errs = append(errs, field+": "+e)
|
|
|
|
}
|
|
|
|
|
|
|
|
httperr := models.ValidationHTTPError{
|
|
|
|
models.HTTPError{
|
|
|
|
Code: models.ErrCodeInvalidData,
|
|
|
|
Message: "Invalid Data",
|
|
|
|
},
|
|
|
|
errs,
|
|
|
|
}
|
|
|
|
|
|
|
|
return httperr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// NewEcho registers a new Echo instance
|
|
|
|
func NewEcho() *echo.Echo {
|
|
|
|
e := echo.New()
|
|
|
|
|
2018-11-03 16:05:45 +01:00
|
|
|
e.HideBanner = true
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// Logger
|
|
|
|
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
2018-07-09 19:49:27 +02:00
|
|
|
Format: "${time_rfc3339_nano}: ${remote_ip} ${method} ${status} ${uri} ${latency_human} - ${user_agent}\n",
|
2018-06-10 11:11:41 +02:00
|
|
|
}))
|
|
|
|
|
2018-11-17 00:17:37 +01:00
|
|
|
// Validation
|
|
|
|
e.Validator = &CustomValidator{}
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterRoutes registers all routes for the application
|
|
|
|
func RegisterRoutes(e *echo.Echo) {
|
|
|
|
|
2018-09-07 22:49:16 +02:00
|
|
|
// CORS_SHIT
|
|
|
|
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
|
|
|
AllowOrigins: []string{"*"},
|
|
|
|
}))
|
2018-06-10 11:11:41 +02:00
|
|
|
|
|
|
|
// API Routes
|
|
|
|
a := e.Group("/api/v1")
|
|
|
|
|
2018-09-17 18:34:46 +02:00
|
|
|
// Swagger UI
|
2018-11-12 16:46:35 +01:00
|
|
|
a.GET("/swagger/*", echoSwagger.WrapHandler)
|
2018-09-17 18:34:46 +02:00
|
|
|
|
2018-06-10 11:34:59 +02:00
|
|
|
a.POST("/login", apiv1.Login)
|
2018-06-13 13:45:22 +02:00
|
|
|
a.POST("/register", apiv1.RegisterUser)
|
2018-10-27 11:33:28 +02:00
|
|
|
a.POST("/user/password/token", apiv1.UserRequestResetPasswordToken)
|
|
|
|
a.POST("/user/password/reset", apiv1.UserResetPassword)
|
2018-10-27 15:14:55 +02:00
|
|
|
a.POST("/user/confirm", apiv1.UserConfirmEmail)
|
2018-06-10 11:11:41 +02:00
|
|
|
|
2018-11-03 16:05:45 +01:00
|
|
|
// Caldav, with auth
|
|
|
|
a.GET("/tasks/caldav", apiv1.Caldav)
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// ===== Routes with Authetification =====
|
|
|
|
// Authetification
|
2018-09-08 13:29:35 +02:00
|
|
|
a.Use(middleware.JWT([]byte(viper.GetString("service.JWTSecret"))))
|
2018-06-10 11:11:41 +02:00
|
|
|
a.POST("/tokenTest", apiv1.CheckToken)
|
2018-06-10 14:14:10 +02:00
|
|
|
|
2018-09-20 19:42:01 +02:00
|
|
|
// User stuff
|
|
|
|
a.GET("/user", apiv1.UserShow)
|
2018-10-03 19:28:17 +02:00
|
|
|
a.POST("/user/password", apiv1.UserChangePassword)
|
2018-09-20 19:42:01 +02:00
|
|
|
a.GET("/users", apiv1.UserList)
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
listHandler := &crud.WebHandler{
|
2018-10-11 17:53:59 +02:00
|
|
|
EmptyStruct: func() crud.CObject {
|
|
|
|
return &models.List{}
|
|
|
|
},
|
2018-07-07 14:19:34 +02:00
|
|
|
}
|
2018-07-08 22:50:01 +02:00
|
|
|
a.GET("/lists", listHandler.ReadAllWeb)
|
2018-07-21 15:08:46 +02:00
|
|
|
a.GET("/lists/:list", listHandler.ReadOneWeb)
|
|
|
|
a.POST("/lists/:list", listHandler.UpdateWeb)
|
|
|
|
a.DELETE("/lists/:list", listHandler.DeleteWeb)
|
|
|
|
a.PUT("/namespaces/:namespace/lists", listHandler.CreateWeb)
|
2018-06-12 18:07:47 +02:00
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
taskHandler := &crud.WebHandler{
|
2018-10-11 17:53:59 +02:00
|
|
|
EmptyStruct: func() crud.CObject {
|
|
|
|
return &models.ListTask{}
|
|
|
|
},
|
2018-07-11 02:13:53 +02:00
|
|
|
}
|
2018-08-30 08:09:17 +02:00
|
|
|
a.PUT("/lists/:list", taskHandler.CreateWeb)
|
2018-11-12 16:46:35 +01:00
|
|
|
a.GET("/tasks", taskHandler.ReadAllWeb)
|
2018-08-30 08:09:17 +02:00
|
|
|
a.DELETE("/tasks/:listtask", taskHandler.DeleteWeb)
|
|
|
|
a.POST("/tasks/:listtask", taskHandler.UpdateWeb)
|
2018-07-02 08:40:24 +02:00
|
|
|
|
2018-07-24 17:29:13 +02:00
|
|
|
listTeamHandler := &crud.WebHandler{
|
2018-10-11 17:53:59 +02:00
|
|
|
EmptyStruct: func() crud.CObject {
|
|
|
|
return &models.TeamList{}
|
|
|
|
},
|
2018-07-24 17:29:13 +02:00
|
|
|
}
|
|
|
|
a.GET("/lists/:list/teams", listTeamHandler.ReadAllWeb)
|
|
|
|
a.PUT("/lists/:list/teams", listTeamHandler.CreateWeb)
|
|
|
|
a.DELETE("/lists/:list/teams/:team", listTeamHandler.DeleteWeb)
|
2018-09-19 08:08:41 +02:00
|
|
|
a.POST("/lists/:list/teams/:team", listTeamHandler.UpdateWeb)
|
2018-07-24 17:29:13 +02:00
|
|
|
|
2018-08-30 08:58:09 +02:00
|
|
|
listUserHandler := &crud.WebHandler{
|
2018-10-11 17:53:59 +02:00
|
|
|
EmptyStruct: func() crud.CObject {
|
|
|
|
return &models.ListUser{}
|
|
|
|
},
|
2018-08-30 08:58:09 +02:00
|
|
|
}
|
|
|
|
a.GET("/lists/:list/users", listUserHandler.ReadAllWeb)
|
|
|
|
a.PUT("/lists/:list/users", listUserHandler.CreateWeb)
|
|
|
|
a.DELETE("/lists/:list/users/:user", listUserHandler.DeleteWeb)
|
2018-09-19 07:54:47 +02:00
|
|
|
a.POST("/lists/:list/users/:user", listUserHandler.UpdateWeb)
|
2018-08-30 08:58:09 +02:00
|
|
|
|
2018-07-11 13:00:00 +02:00
|
|
|
namespaceHandler := &crud.WebHandler{
|
2018-10-11 17:53:59 +02:00
|
|
|
EmptyStruct: func() crud.CObject {
|
|
|
|
return &models.Namespace{}
|
|
|
|
},
|
2018-07-11 13:00:00 +02:00
|
|
|
}
|
|
|
|
a.GET("/namespaces", namespaceHandler.ReadAllWeb)
|
2018-07-12 00:09:16 +02:00
|
|
|
a.PUT("/namespaces", namespaceHandler.CreateWeb)
|
2018-07-21 15:08:46 +02:00
|
|
|
a.GET("/namespaces/:namespace", namespaceHandler.ReadOneWeb)
|
|
|
|
a.POST("/namespaces/:namespace", namespaceHandler.UpdateWeb)
|
|
|
|
a.DELETE("/namespaces/:namespace", namespaceHandler.DeleteWeb)
|
|
|
|
a.GET("/namespaces/:namespace/lists", apiv1.GetListsByNamespaceID)
|
2018-07-14 17:34:59 +02:00
|
|
|
|
2018-07-17 08:44:21 +02:00
|
|
|
namespaceTeamHandler := &crud.WebHandler{
|
2018-10-11 17:53:59 +02:00
|
|
|
EmptyStruct: func() crud.CObject {
|
|
|
|
return &models.TeamNamespace{}
|
|
|
|
},
|
2018-07-17 08:44:21 +02:00
|
|
|
}
|
2018-07-18 21:54:04 +02:00
|
|
|
a.GET("/namespaces/:namespace/teams", namespaceTeamHandler.ReadAllWeb)
|
|
|
|
a.PUT("/namespaces/:namespace/teams", namespaceTeamHandler.CreateWeb)
|
|
|
|
a.DELETE("/namespaces/:namespace/teams/:team", namespaceTeamHandler.DeleteWeb)
|
2018-09-19 08:16:04 +02:00
|
|
|
a.POST("/namespaces/:namespace/teams/:team", namespaceTeamHandler.UpdateWeb)
|
2018-07-17 08:44:21 +02:00
|
|
|
|
2018-09-04 20:15:24 +02:00
|
|
|
namespaceUserHandler := &crud.WebHandler{
|
2018-10-11 17:53:59 +02:00
|
|
|
EmptyStruct: func() crud.CObject {
|
|
|
|
return &models.NamespaceUser{}
|
|
|
|
},
|
2018-09-04 20:15:24 +02:00
|
|
|
}
|
|
|
|
a.GET("/namespaces/:namespace/users", namespaceUserHandler.ReadAllWeb)
|
|
|
|
a.PUT("/namespaces/:namespace/users", namespaceUserHandler.CreateWeb)
|
|
|
|
a.DELETE("/namespaces/:namespace/users/:user", namespaceUserHandler.DeleteWeb)
|
2018-09-19 08:03:39 +02:00
|
|
|
a.POST("/namespaces/:namespace/users/:user", namespaceUserHandler.UpdateWeb)
|
2018-09-04 20:15:24 +02:00
|
|
|
|
2018-07-14 17:34:59 +02:00
|
|
|
teamHandler := &crud.WebHandler{
|
2018-10-11 17:53:59 +02:00
|
|
|
EmptyStruct: func() crud.CObject {
|
|
|
|
return &models.Team{}
|
|
|
|
},
|
2018-07-14 17:34:59 +02:00
|
|
|
}
|
|
|
|
a.GET("/teams", teamHandler.ReadAllWeb)
|
2018-07-21 15:08:46 +02:00
|
|
|
a.GET("/teams/:team", teamHandler.ReadOneWeb)
|
2018-07-14 17:34:59 +02:00
|
|
|
a.PUT("/teams", teamHandler.CreateWeb)
|
2018-07-21 15:08:46 +02:00
|
|
|
a.POST("/teams/:team", teamHandler.UpdateWeb)
|
|
|
|
a.DELETE("/teams/:team", teamHandler.DeleteWeb)
|
2018-07-26 09:53:32 +02:00
|
|
|
|
|
|
|
teamMemberHandler := &crud.WebHandler{
|
2018-10-11 17:53:59 +02:00
|
|
|
EmptyStruct: func() crud.CObject {
|
|
|
|
return &models.TeamMember{}
|
|
|
|
},
|
2018-07-26 09:53:32 +02:00
|
|
|
}
|
|
|
|
a.PUT("/teams/:team/members", teamMemberHandler.CreateWeb)
|
|
|
|
a.DELETE("/teams/:team/members/:user", teamMemberHandler.DeleteWeb)
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|