api/routes/routes.go

161 lines
4.4 KiB
Go
Raw Normal View History

2018-07-10 12:02:23 +00:00
package routes
2018-06-13 11:45:22 +00:00
// Package v1 List API.
//
// This documentation describes the List API.
//
// Schemes: http, https
// BasePath: /api/v1
// Version: 0.1
// License: GPLv3
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Security:
// - AuthorizationHeaderToken :
//
// SecurityDefinitions:
// AuthorizationHeaderToken:
// type: apiKey
// name: Authorization
// in: header
//
// swagger:meta
2018-06-10 09:11:41 +00:00
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
2018-07-25 14:24:46 +00:00
"code.vikunja.io/api/models"
apiv1 "code.vikunja.io/api/routes/api/v1"
_ "code.vikunja.io/api/routes/api/v1/swagger" // for docs generation
"code.vikunja.io/api/routes/crud"
2018-07-18 19:54:04 +00:00
"net/http"
2018-06-10 09:11:41 +00:00
)
// NewEcho registers a new Echo instance
func NewEcho() *echo.Echo {
e := echo.New()
// Logger
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
2018-07-09 17:49:27 +00:00
Format: "${time_rfc3339_nano}: ${remote_ip} ${method} ${status} ${uri} ${latency_human} - ${user_agent}\n",
2018-06-10 09:11:41 +00:00
}))
return e
}
// RegisterRoutes registers all routes for the application
func RegisterRoutes(e *echo.Echo) {
2018-07-09 17:49:27 +00:00
// TODO: Use proper cors middleware by echo
2018-06-13 11:45:22 +00:00
// Middleware for cors
2018-06-10 09:11:41 +00:00
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
res := c.Response()
res.Header().Set("Access-Control-Allow-Origin", "*")
res.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
res.Header().Set("Access-Control-Allow-Headers", "authorization,content-type")
res.Header().Set("Access-Control-Expose-Headers", "authorization,content-type")
return next(c)
}
})
2018-06-14 12:19:19 +00:00
// Swagger UI
e.Static("/swagger", "public/swagger")
2018-06-10 09:11:41 +00:00
// API Routes
a := e.Group("/api/v1")
// CORS_SHIT
a.OPTIONS("/login", SetCORSHeader)
2018-06-10 09:34:59 +00:00
a.OPTIONS("/register", SetCORSHeader)
2018-06-10 09:11:41 +00:00
a.OPTIONS("/users", SetCORSHeader)
a.OPTIONS("/users/:id", SetCORSHeader)
2018-06-10 12:41:42 +00:00
a.OPTIONS("/lists", SetCORSHeader)
a.OPTIONS("/lists/:id", SetCORSHeader)
2018-06-10 09:11:41 +00:00
2018-06-10 09:34:59 +00:00
a.POST("/login", apiv1.Login)
2018-06-13 11:45:22 +00:00
a.POST("/register", apiv1.RegisterUser)
2018-06-10 09:11:41 +00:00
2018-07-18 19:54:04 +00:00
a.POST("/test/:infi/:Käsebrot/blub/:gedöns", func(c echo.Context) error {
type testStruct struct {
Integ int64 `param:"infi" form:"infi"`
Cheese string `param:"Käsebrot"`
Kram string `param:"gedöns"`
Other string
Whooo int64
Blub float64
Test string `form:"test"`
}
t := testStruct{}
if err := crud.ParamBinder(&t, c); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
}
return c.JSON(http.StatusOK, &t)
})
2018-06-10 09:11:41 +00:00
// ===== Routes with Authetification =====
// Authetification
a.Use(middleware.JWT(models.Config.JWTLoginSecret))
a.POST("/tokenTest", apiv1.CheckToken)
2018-06-10 12:14:10 +00:00
2018-07-10 12:02:23 +00:00
listHandler := &crud.WebHandler{
CObject: &models.List{},
2018-07-07 12:19:34 +00:00
}
a.GET("/lists", listHandler.ReadAllWeb)
2018-07-21 13:08:46 +00: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)
itemHandler := &crud.WebHandler{
CObject: &models.ListItem{},
}
2018-07-21 13:08:46 +00:00
a.PUT("/lists/:list", itemHandler.CreateWeb)
a.DELETE("/items/:listitem", itemHandler.DeleteWeb)
a.POST("/items/:listitem", itemHandler.UpdateWeb)
listTeamHandler := &crud.WebHandler{
CObject: &models.TeamList{},
}
a.GET("/lists/:list/teams", listTeamHandler.ReadAllWeb)
a.PUT("/lists/:list/teams", listTeamHandler.CreateWeb)
a.DELETE("/lists/:list/teams/:team", listTeamHandler.DeleteWeb)
namespaceHandler := &crud.WebHandler{
CObject: &models.Namespace{},
}
a.GET("/namespaces", namespaceHandler.ReadAllWeb)
a.PUT("/namespaces", namespaceHandler.CreateWeb)
2018-07-21 13:08:46 +00: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 15:34:59 +00:00
namespaceTeamHandler := &crud.WebHandler{
CObject: &models.TeamNamespace{},
}
2018-07-18 19:54:04 +00:00
a.GET("/namespaces/:namespace/teams", namespaceTeamHandler.ReadAllWeb)
a.PUT("/namespaces/:namespace/teams", namespaceTeamHandler.CreateWeb)
a.DELETE("/namespaces/:namespace/teams/:team", namespaceTeamHandler.DeleteWeb)
2018-07-14 15:34:59 +00:00
teamHandler := &crud.WebHandler{
CObject: &models.Team{},
}
a.GET("/teams", teamHandler.ReadAllWeb)
2018-07-21 13:08:46 +00:00
a.GET("/teams/:team", teamHandler.ReadOneWeb)
2018-07-14 15:34:59 +00:00
a.PUT("/teams", teamHandler.CreateWeb)
2018-07-21 13:08:46 +00:00
a.POST("/teams/:team", teamHandler.UpdateWeb)
a.DELETE("/teams/:team", teamHandler.DeleteWeb)
2018-06-10 09:11:41 +00:00
}