Skip to content

Commit

Permalink
add get endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
adlandh committed Sep 1, 2023
1 parent 2f63fb2 commit 23f15f4
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 44 deletions.
1 change: 1 addition & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ tasks:
deps:
- namespace
cmds:
- kubectl delete secret post-forwarder --namespace=post-forwarder || true
- kubectl create secret generic post-forwarder --from-env-file=local-secrets.env --namespace=post-forwarder
run:
desc: run app locally in kubernetes
Expand Down
23 changes: 21 additions & 2 deletions api/post-forwarder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.3
info:
title: Port Forwarder webhook
description: Port Forwarder webhook
version: 1.0.0
version: 1.1.0
servers:
- url: 'http://localhost:8080'
paths:
Expand All @@ -20,7 +20,7 @@ paths:
default: Ok
/api/{token}/{service}:
post:
operationId: webhook
operationId: postWebhook
description: POST webhook endpoint
parameters:
- in: path
Expand All @@ -38,4 +38,23 @@ paths:
responses:
'200':
description: ok
get:
operationId: getWebhook
description: GET webhook endpoint
parameters:
- in: path
name: token
required: true
schema:
type: string
description: api token
- in: path
name: service
required: true
schema:
type: string
description: name of service
responses:
'200':
description: ok

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 42 additions & 16 deletions internal/post-forwarder/driver/http.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package driver

import (
"fmt"
"io"
"net/http"
"strings"

"github.com/adlandh/post-forwarder/internal/post-forwarder/config"
"github.com/adlandh/post-forwarder/internal/post-forwarder/domain"
Expand All @@ -15,34 +17,58 @@ type HttpServer struct {
app domain.ApplicationInterface
}

func (h *HttpServer) Webhook(ctx echo.Context, token string, service string) error {
var _ ServerInterface = (*HttpServer)(nil)

func NewHttpServer(cfg *config.Config, app domain.ApplicationInterface) *HttpServer {
return &HttpServer{
token: cfg.AuthToken,
app: app,
}
}

func (h HttpServer) HealthCheck(ctx echo.Context) error {
return ctx.String(http.StatusOK, "Ok")
}

func (h HttpServer) PostWebhook(ctx echo.Context, token string, service string) error {
return h.webhook(ctx, token, service)
}

func (h HttpServer) GetWebhook(ctx echo.Context, token string, service string) error {
return h.webhook(ctx, token, service)
}

func (h HttpServer) webhook(ctx echo.Context, token string, service string) error {
// checking if the token is valid
if token != h.token {
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid auth token")
}

var msg string

// checking query parameters first
for key, values := range ctx.QueryParams() {
msg += fmt.Sprintf("%s=%s\n", key, strings.Join(values, ","))
}

// checking body parameters
body, err := io.ReadAll(ctx.Request().Body)

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
// if parameters were empty, just throw error
if msg == "" {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
} else { // add error to msg
msg += fmt.Sprintf("error reading body: %s", err.Error())
}
} else { // if no error add body to msg
msg += string(body)
}

err = h.app.ProcessRequest(ctx.Request().Context(), service, string(body))
err = h.app.ProcessRequest(ctx.Request().Context(), service, msg)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return ctx.NoContent(http.StatusOK)
}

var _ ServerInterface = (*HttpServer)(nil)

func NewHttpServer(cfg *config.Config, app domain.ApplicationInterface) *HttpServer {
return &HttpServer{
token: cfg.AuthToken,
app: app,
}
}

func (h *HttpServer) HealthCheck(ctx echo.Context) error {
return ctx.String(http.StatusOK, "Ok")
}
32 changes: 28 additions & 4 deletions internal/post-forwarder/driver/open_api_sentry_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 25 additions & 7 deletions internal/post-forwarder/driver/open_api_zap_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 33 additions & 5 deletions internal/post-forwarder/driver/openapi_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 23f15f4

Please sign in to comment.