api: Begin implementing an API

This commit is contained in:
Tom 2023-04-28 10:25:36 +02:00
parent 1a5e83be10
commit 2fac0390b1
11 changed files with 186 additions and 23 deletions

View file

@ -0,0 +1,43 @@
package news
import (
"context"
"encoding/json"
"net/http"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/tkw1536/pkglib/httpx"
)
type API struct {
component.Base
}
var (
_ component.Routeable = (*API)(nil)
)
func (api *API) Routes() component.Routes {
return component.Routes{
Prefix: "/api/v1/news/",
Exact: true,
Decorator: api.Config.HTTP.APIDecorator("GET"),
}
}
func (api *API) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
items, err := Items()
if err != nil {
return nil, err
}
data, err := json.Marshal(items)
if err != nil {
return nil, err
}
return httpx.Response{
ContentType: "application/json",
Body: data,
}, nil
}