api/routes/routes.go

108 lines
2.9 KiB
Go
Raw Normal View History

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
package routes
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"git.kolaente.de/konrad/list/models"
apiv1 "git.kolaente.de/konrad/list/routes/api/v1"
2018-06-13 11:45:22 +00:00
_ "git.kolaente.de/konrad/list/routes/api/v1/swagger" // for docs generation
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{
Format: "${time_rfc3339}: ${remote_ip} ${method} ${status} ${uri} ${latency_human} - ${user_agent}\n",
}))
return e
}
// RegisterRoutes registers all routes for the application
func RegisterRoutes(e *echo.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
// ===== 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-06-10 12:41:42 +00:00
a.GET("/lists", apiv1.GetListsByUser)
2018-07-07 12:19:34 +00:00
listHandler := &apiv1.CRUDWebHandler{
CObject: &apiv1.DefaultCRUD{
Target: &models.List{},
},
}
a.GET("/lists/:id", listHandler.ReadOneWeb)
2018-06-13 11:45:22 +00:00
a.POST("/lists/:id", apiv1.UpdateList)
a.PUT("/lists/:id", apiv1.AddListItem)
2018-06-12 16:17:21 +00:00
a.DELETE("/lists/:id", apiv1.DeleteListByID)
a.DELETE("/item/:id", apiv1.DeleteListItemByIDtemByID)
a.POST("/item/:id", apiv1.UpdateListItem)
a.GET("/namespaces", apiv1.GetAllNamespacesByCurrentUser)
a.PUT("/namespaces", apiv1.AddNamespace)
2018-07-02 07:09:32 +00:00
a.GET("/namespaces/:id", apiv1.ShowNamespace)
a.POST("/namespaces/:id", apiv1.UpdateNamespace)
a.DELETE("/namespaces/:id", apiv1.DeleteNamespaceByID)
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
a.PUT("/namespaces/:id/lists", apiv1.AddList)
2018-06-10 09:11:41 +00:00
}