-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(caddy): handle API directly using the caddy admin api, solves #585
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package httpcache | ||
|
||
import ( | ||
"fmt" | ||
"github.com/caddyserver/caddy/v2" | ||
"github.com/darkweak/souin/pkg/api" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/darkweak/storages/core" | ||
) | ||
|
||
func init() { | ||
caddy.RegisterModule(new(adminAPI)) | ||
} | ||
|
||
// adminAPI is a module that serves PKI endpoints to retrieve | ||
// information about the CAs being managed by Caddy. | ||
type adminAPI struct { | ||
ctx caddy.Context | ||
logger core.Logger | ||
app *SouinApp | ||
InternalEndpointHandlers *api.MapHandler | ||
} | ||
|
||
// CaddyModule returns the Caddy module information. | ||
func (adminAPI) CaddyModule() caddy.ModuleInfo { | ||
return caddy.ModuleInfo{ | ||
ID: "admin.api.souin", | ||
New: func() caddy.Module { return new(adminAPI) }, | ||
} | ||
} | ||
|
||
func (a *adminAPI) handleAPIEndpoints(writer http.ResponseWriter, request *http.Request) error { | ||
if a.InternalEndpointHandlers != nil { | ||
for k, handler := range *a.InternalEndpointHandlers.Handlers { | ||
fmt.Printf("current route for the key %+v => %+v\n", request.RequestURI, k) | ||
if strings.Contains(request.RequestURI, k) { | ||
handler(writer, request) | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return caddy.APIError{ | ||
HTTPStatus: http.StatusNotFound, | ||
Err: fmt.Errorf("resource not found: %v", request.URL.Path), | ||
} | ||
} | ||
|
||
// Provision sets up the adminAPI module. | ||
func (a *adminAPI) Provision(ctx caddy.Context) error { | ||
a.ctx = ctx | ||
a.logger = ctx.Logger(a).Sugar() | ||
|
||
app, err := ctx.App(moduleName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
a.app = app.(*SouinApp) | ||
config := Configuration{ | ||
API: a.app.API, | ||
} | ||
a.InternalEndpointHandlers = api.GenerateHandlerMap(&config, a.app.Storers, a.app.SurrogateStorage) | ||
fmt.Printf("endpoints => %+v\n", a.InternalEndpointHandlers.Handlers) | ||
|
||
return nil | ||
} | ||
|
||
// Routes returns the admin routes for the PKI app. | ||
func (a *adminAPI) Routes() []caddy.AdminRoute { | ||
basepath := "/souin-api" | ||
if a.app != nil && a.app.API.BasePath != "" { | ||
basepath = a.app.API.BasePath | ||
} | ||
|
||
return []caddy.AdminRoute{ | ||
{ | ||
Pattern: basepath + "/{params...}", | ||
Handler: caddy.AdminHandlerFunc(a.handleAPIEndpoints), | ||
}, | ||
} | ||
} |