@title LocalAI API @version 2.0.0 @description The LocalAI Rest API. @termsOfService @contact.name LocalAI @contact.url https://localai.io @license.name MIT @license.url https://raw.githubusercontent.com/mudler/LocalAI/master/LICENSE @BasePath / @schemes http https @securityDefinitions.apikey Bearer
(application *application.Application)
| 92 | // @tag.description API instruction discovery — browse instruction areas and get endpoint guides |
| 93 | |
| 94 | func API(application *application.Application) (*echo.Echo, error) { |
| 95 | e := echo.New() |
| 96 | |
| 97 | // Set body limit |
| 98 | if application.ApplicationConfig().UploadLimitMB > 0 { |
| 99 | e.Use(middleware.BodyLimit(fmt.Sprintf("%dM", application.ApplicationConfig().UploadLimitMB))) |
| 100 | } |
| 101 | |
| 102 | // SPA fallback handler, set later when React UI is available |
| 103 | var spaFallback func(echo.Context) error |
| 104 | |
| 105 | // Set error handler |
| 106 | if !application.ApplicationConfig().OpaqueErrors { |
| 107 | e.HTTPErrorHandler = func(err error, c echo.Context) { |
| 108 | code := http.StatusInternalServerError |
| 109 | var he *echo.HTTPError |
| 110 | if errors.As(err, &he) { |
| 111 | code = he.Code |
| 112 | } |
| 113 | |
| 114 | // Handle 404 errors: serve React SPA for HTML requests, JSON otherwise |
| 115 | if code == http.StatusNotFound { |
| 116 | if spaFallback != nil { |
| 117 | accept := c.Request().Header.Get("Accept") |
| 118 | contentType := c.Request().Header.Get("Content-Type") |
| 119 | if strings.Contains(accept, "text/html") && !strings.Contains(contentType, "application/json") { |
| 120 | spaFallback(c) |
| 121 | return |
| 122 | } |
| 123 | } |
| 124 | notFoundHandler(c) |
| 125 | return |
| 126 | } |
| 127 | |
| 128 | // Send custom error page |
| 129 | c.JSON(code, schema.ErrorResponse{ |
| 130 | Error: &schema.APIError{Message: err.Error(), Code: code}, |
| 131 | }) |
| 132 | } |
| 133 | } else { |
| 134 | e.HTTPErrorHandler = func(err error, c echo.Context) { |
| 135 | code := http.StatusInternalServerError |
| 136 | var he *echo.HTTPError |
| 137 | if errors.As(err, &he) { |
| 138 | code = he.Code |
| 139 | } |
| 140 | c.NoContent(code) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Set renderer |
| 145 | e.Renderer = renderEngine() |
| 146 | |
| 147 | // Hide banner |
| 148 | e.HideBanner = true |
| 149 | e.HidePort = true |
| 150 | |
| 151 | // Middleware - StripPathPrefix must be registered early as it uses Rewrite which runs before routing |
no test coverage detected