Files

100 lines
1.9 KiB
Go
Raw Permalink Normal View History

2018-10-19 15:31:41 -04:00
package api
import (
"github.com/ant0ine/go-json-rest/rest"
)
2018-11-05 09:37:08 -05:00
/**
* Changelog
*
2020-01-14 14:14:16 -05:00
* 1.4.0
* - add `GET /orgs/:orgId/budget`
* - add `POST /orgs/:orgId/budget`
* - add `DELETE /orgs/:orgId/budget`
*
2020-01-14 14:22:15 -05:00
* 1.4.0
2019-06-27 16:54:24 -04:00
* - add org.timezone
*
* 1.2.0
* - add `GET /orgs/:orgId/accounts/:accountId
*
* 1.1.0
* - add `GET /health-check`
*
2018-11-05 09:37:08 -05:00
* 1.0.1
* - add user.signupSource
*
*/
2018-10-19 15:31:41 -04:00
/**
* @apiDefine NotAuthorizedError
*
* @apiError NotAuthorized API request does not have proper credentials
*
* @apiErrorExample Error-Response:
* HTTP/1.1 403 Not Authorized
*/
/**
* @apiDefine InternalServerError
*
* @apiError InternalServer An internal error occurred
*
* @apiErrorExample Error-Response:
* HTTP/1.1 500 Internal Server Error
* {
* "error": "id required"
* }
*
*/
2018-11-08 11:35:11 -05:00
func Init(prefix string) (*rest.Api, error) {
2018-10-19 15:31:41 -04:00
rest.ErrorFieldName = "error"
app := rest.NewApi()
logger := &LoggerMiddleware{}
var stack = []rest.Middleware{
logger,
&rest.RecorderMiddleware{},
&rest.TimerMiddleware{},
&rest.PoweredByMiddleware{},
&rest.RecoverMiddleware{},
&rest.GzipMiddleware{},
&rest.ContentTypeCheckerMiddleware{},
}
app.Use(stack...)
app.Use(&rest.CorsMiddleware{
RejectNonCorsRequests: false,
OriginValidator: func(origin string, request *rest.Request) bool {
//return origin == "http://localhost:4200"
return true
},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{
"Accept", "Content-Type", "X-Custom-Header", "Origin", "Authorization", "Accept-Version"},
AccessControlAllowCredentials: true,
AccessControlMaxAge: 3600,
})
auth := &AuthMiddleware{
Realm: "openaccounting",
}
version := &VersionMiddleware{}
app.Use(auth)
app.Use(version)
2018-11-08 11:35:11 -05:00
router, err := GetRouter(auth, prefix)
2018-10-19 15:31:41 -04:00
if err != nil {
return nil, err
}
app.SetApp(router)
return app, nil
}