Skip to content

Commit

Permalink
Update landing request API with customizable response text and option…
Browse files Browse the repository at this point in the history
…al redirect, controlled by environment variables
  • Loading branch information
jspaleta committed Jun 4, 2024
1 parent 1dcf7e5 commit b80bff3
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ Deathstar as a Service

Build a Docker container locally with `make build`.

Build and push to a registry with `make publish`. You can specify the image tag with the env var `IMAGE` (defaults to `cilium/starwars`).
Build and push to a registry with `make publish`. You can specify the image tag with the env var `IMAGE` (defaults to `cilium/starwars`)

## Set custom landing response msg
Set the `LANDING_RESPONSE_MSG` environment variable to override default landing request response message string.

## Enable landing service redirect
Set the `LANDING_REQUEST_URL` environment variable to redirect landing request to different location.

47 changes: 46 additions & 1 deletion restapi/configure_deathstar.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package restapi

import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"

errors "github.com/go-openapi/errors"
Expand Down Expand Up @@ -55,6 +59,14 @@ var info = fmt.Sprintf(`{
}
`, hostname)

func landingResponse() string {
var landingResponseMsg, ok = os.LookupEnv("LANDING_RESPONSE_MSG")
if !ok {
landingResponseMsg = "Ship landed!\n"
}
return landingResponseMsg
}

func configureFlags(api *operations.DeathstarAPI) {
// api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... }
}
Expand Down Expand Up @@ -84,7 +96,40 @@ func configureAPI(api *operations.DeathstarAPI) http.Handler {
return operations.NewPutExhaustPortServiceUnavailable().WithPayload(backtrace)
})
api.PostRequestLandingHandler = operations.PostRequestLandingHandlerFunc(func(params operations.PostRequestLandingParams) middleware.Responder {
return operations.NewPostRequestLandingOK().WithPayload("Ship landed\n")
var landingRequestURL, ok = os.LookupEnv("LANDING_REQUEST_URL")
if !ok {
return operations.NewPostRequestLandingOK().WithPayload(landingResponse() + "\n")
} else {
jsonBody := []byte(``)
bodyReader := bytes.NewReader(jsonBody)

nr, _ := http.NewRequest(http.MethodPost, landingRequestURL, bodyReader)
nr.Header = params.HTTPRequest.Header.Clone()
client := http.Client{
Timeout: 30 * time.Second,
}

res, err := client.Do(nr)

if err != nil {
fmt.Printf("client: error making http request: %s\n", err)
return operations.NewPostRequestLandingServiceUnavailable().WithPayload(
"Request Forward to " + landingRequestURL + " Failed\nUnknon Request Error")
}

resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("client: could not read response body: %s\n", err)
return operations.NewPostRequestLandingServiceUnavailable().WithPayload(
"Request Forward to " + landingRequestURL + " Failed\nError Reading Response Body")
}
if res.StatusCode == 200 {
return operations.NewPostRequestLandingOK().WithPayload(strings.TrimRight(string(resBody), "\r\n") + "\nRequest Forwarded to " + landingRequestURL + "\n")
} else {
return operations.NewPostRequestLandingServiceUnavailable().WithPayload("Request Forward to " +
landingRequestURL + " Failed\nRemote StatusCode:" + strconv.Itoa(res.StatusCode) + "\n")
}
}
})

api.ServerShutdown = func() {}
Expand Down
41 changes: 41 additions & 0 deletions restapi/operations/post_request_landing_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,44 @@ func (o *PostRequestLandingOK) WriteResponse(rw http.ResponseWriter, producer ru
}

}

// HTTP code for type PostRequestLandingServiceUnavailable
const PostRequestLandingServiceUnavailableCode int = 503

/*PostRequestLandingServiceUnavailable Landing Request Service Not available
swagger:response postRequestLandingServiceUnavailable
*/
type PostRequestLandingServiceUnavailable struct {

/*
In: Body
*/
Payload string `json:"body,omitempty"`
}

// NewPostRequestLandingServiceUnavailable creates PostRequestLandingServiceUnavailable with default headers values
func NewPostRequestLandingServiceUnavailable() *PostRequestLandingServiceUnavailable {
return &PostRequestLandingServiceUnavailable{}
}

// WithPayload adds the payload to the post request landing service unavailable response
func (o *PostRequestLandingServiceUnavailable) WithPayload(payload string) *PostRequestLandingServiceUnavailable {
o.Payload = payload
return o
}

// SetPayload sets the payload to the post request landing service unavailable response
func (o *PostRequestLandingServiceUnavailable) SetPayload(payload string) {
o.Payload = payload
}

// WriteResponse to the client
func (o *PostRequestLandingServiceUnavailable) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {

rw.WriteHeader(503)
payload := o.Payload
if err := producer.Produce(rw, payload); err != nil {
panic(err) // let the recovery middleware deal with this
}
}
4 changes: 4 additions & 0 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ paths:
description: OK
schema:
type: string
'503':
description: Landing Request Service Not available
schema:
type: string

0 comments on commit b80bff3

Please sign in to comment.