Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions admin/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package api

import (
"encoding/json"
"fmt"
"net/http"
"net/http/pprof"
"strconv"

"github.com/getkin/kin-openapi/openapi3"
"github.com/go-playground/form"
"github.com/gorilla/mux"
"github.com/webhookx-io/webhookx/config"
"github.com/webhookx-io/webhookx/db"
"github.com/webhookx-io/webhookx/db/entities"
dberrs "github.com/webhookx-io/webhookx/db/errs"
"github.com/webhookx-io/webhookx/db/query"
"github.com/webhookx-io/webhookx/dispatcher"
"github.com/webhookx-io/webhookx/pkg/declarative"
"github.com/webhookx-io/webhookx/pkg/errs"
Expand Down Expand Up @@ -58,27 +59,26 @@ func (api *API) param(r *http.Request, variable string) string {
return mux.Vars(r)[variable]
}

// query returns the url query value if it exists.
func (api *API) query(r *http.Request, name string) string {
return r.URL.Query().Get(name)
}

func (api *API) json(code int, w http.ResponseWriter, data interface{}) {
response.JSON(w, code, data)
}

func (api *API) bindQuery(r *http.Request, q *query.Query) {
page, _ := strconv.Atoi(r.URL.Query().Get("page_no"))
if page <= 0 {
page = 1
}

pagesize, _ := strconv.Atoi(r.URL.Query().Get("page_size"))
if pagesize <= 0 {
pagesize = 20
func (api *API) lookupOperation(path string, method string) *openapi3.Operation {
operation := openapi.Spec.Paths.Find(path).GetOperation(method)
if operation == nil {
panic(fmt.Errorf("operation %s.%s not found", path, method))
}
return operation
}

q.Page(uint64(page), uint64(pagesize))
func (api *API) bindQuery(r *http.Request, value interface{}) error {
decoder := form.NewDecoder()
err := decoder.Decode(value, r.URL.Query())
if err != nil {
return errs.NewValidateError(err)
}
return nil
}

func (api *API) error(code int, w http.ResponseWriter, err error) {
Expand Down
26 changes: 14 additions & 12 deletions admin/api/attempts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ package api
import (
"net/http"

"github.com/webhookx-io/webhookx/pkg/openapi"
"github.com/webhookx-io/webhookx/pkg/types"

"github.com/webhookx-io/webhookx/db/query"
"github.com/webhookx-io/webhookx/utils"
)

func (api *API) PageAttempt(w http.ResponseWriter, r *http.Request) {
var q query.AttemptQuery
q.Order("id", query.DESC)
api.bindQuery(r, &q.Query)
if r.URL.Query().Get("event_id") != "" {
q.EventId = utils.Pointer(r.URL.Query().Get("event_id"))
parameters := api.lookupOperation("/workspaces/{ws_id}/attempts", http.MethodGet).Parameters
if err := openapi.ValidateParameters(r, parameters); err != nil {
api.error(400, w, err)
return
}
if r.URL.Query().Get("endpoint_id") != "" {
q.EndpointId = utils.Pointer(r.URL.Query().Get("endpoint_id"))

var params AttemptListParams
if err := api.bindQuery(r, &params); err != nil {
api.error(400, w, err)
return
}
list, total, err := api.db.AttemptsWS.Page(r.Context(), &q)

query := params.Query()
page, err := api.db.AttemptsWS.Cursor(r.Context(), query)
api.assert(err)

api.json(200, w, NewPagination(total, list))
api.json(200, w, NewPagination(query, page))
}

func (api *API) GetAttempt(w http.ResponseWriter, r *http.Request) {
Expand Down
22 changes: 16 additions & 6 deletions admin/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@ import (
"net/http"

"github.com/webhookx-io/webhookx/db/entities"
"github.com/webhookx-io/webhookx/db/query"
"github.com/webhookx-io/webhookx/pkg/contextx"
"github.com/webhookx-io/webhookx/pkg/openapi"
"github.com/webhookx-io/webhookx/pkg/types"
"github.com/webhookx-io/webhookx/utils"
)

func (api *API) PageEndpoint(w http.ResponseWriter, r *http.Request) {
var q query.EndpointQuery
q.Order("id", query.DESC)
api.bindQuery(r, &q.Query)
list, total, err := api.db.EndpointsWS.Page(r.Context(), &q)
parameters := api.lookupOperation("/workspaces/{ws_id}/endpoints", http.MethodGet).Parameters
if err := openapi.ValidateParameters(r, parameters); err != nil {
api.error(400, w, err)
return
}

var params EndpointListParams
if err := api.bindQuery(r, &params); err != nil {
api.error(400, w, err)
return
}

query := params.Query()
page, err := api.db.EndpointsWS.Cursor(r.Context(), query)
api.assert(err)

api.json(200, w, NewPagination(total, list))
api.json(200, w, NewPagination(query, page))
}

func (api *API) GetEndpoint(w http.ResponseWriter, r *http.Request) {
Expand Down
21 changes: 15 additions & 6 deletions admin/api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ import (
"time"

"github.com/webhookx-io/webhookx/db/entities"
"github.com/webhookx-io/webhookx/db/query"
"github.com/webhookx-io/webhookx/pkg/contextx"
"github.com/webhookx-io/webhookx/pkg/openapi"
"github.com/webhookx-io/webhookx/pkg/types"
"github.com/webhookx-io/webhookx/services/eventbus"
"github.com/webhookx-io/webhookx/utils"
)

func (api *API) PageEvent(w http.ResponseWriter, r *http.Request) {
var q query.EventQuery
q.Order("id", query.DESC)
api.bindQuery(r, &q.Query)
parameters := api.lookupOperation("/workspaces/{ws_id}/events", http.MethodGet).Parameters
if err := openapi.ValidateParameters(r, parameters); err != nil {
api.error(400, w, err)
return
}

var params EventListParams
if err := api.bindQuery(r, &params); err != nil {
api.error(400, w, err)
return
}

list, total, err := api.db.EventsWS.Page(r.Context(), &q)
query := params.Query()
page, err := api.db.EventsWS.Cursor(r.Context(), query)
api.assert(err)

api.json(200, w, NewPagination(total, list))
api.json(200, w, NewPagination(query, page))
}

func (api *API) GetEvent(w http.ResponseWriter, r *http.Request) {
Expand Down
21 changes: 17 additions & 4 deletions admin/api/models.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package api

import "github.com/webhookx-io/webhookx/db/dao"

type Pagination[T any] struct {
Total int64 `json:"total"`
Data []T `json:"data"`
}

func NewPagination[T any](total int64, data []T) *Pagination[T] {
return &Pagination[T]{
Total: total,
Data: data,
type PaginationCursor[T any] struct {
Next *string `json:"next"`
Data []T `json:"data"`
}

func NewPagination[T any](q *dao.Query, cursor dao.CursorResult[T]) interface{} {
if q.CursorModel {
return PaginationCursor[T]{
Next: cursor.Cursor,
Data: cursor.Data,
}
}
return Pagination[T]{
Total: cursor.Total,
Data: cursor.Data,
}
}
Loading
Loading