Skip to content

Commit

Permalink
feat: added beforeResponse and afterResponse hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Dec 30, 2021
1 parent e40977d commit 311b961
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config/v001.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "gopkg.in/yaml.v2"

// ConfigV001 describes the V 0.0.1 configuration yaml
type ConfigV001 struct {
Hooks Hooks `yaml:"hooks"`
Routes []ConfigV001Route `yaml:"routes"`
}

Expand All @@ -14,6 +15,17 @@ type ConfigV001Route struct {
Size string `yaml:"size"`
Template string `yaml:"template"`
CacheControl string `yaml:"cache-control"`
Hooks []Hooks `yaml:"hooks"`
}

type Hooks struct {
BeforeResponse ResponseHook `yaml:"beforeResponse"`
AfterResponse ResponseHook `yaml:"afterResponse"`
}

type ResponseHook struct {
Url string `yaml:"url"`
Headers []map[string]string `yaml:"headers"`
}

// ReadV001Config returns a parsed V 0.0.1 configuration struct
Expand Down
54 changes: 54 additions & 0 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -21,6 +22,15 @@ type ImageSize struct {
height float64
}

type WebHook struct {
Action string `json:"action"`
Route string `json:"route"`
Template string `json:"template"`
CacheControl string `json:"cache-control"`
Size string `json:"size"`
Params map[string]string `json:"params"`
}

// ConfigError shows an error at frontend if configuration has some errors
func ConfigError(c *gin.Context) {
c.Data(
Expand Down Expand Up @@ -74,10 +84,20 @@ func HandleRoutes(c *gin.Context) {
image := chromium.GenerateImage(tpl.String(), sizes.width, sizes.height)
img := bytes.NewReader(image)

conf := config.Config

if conf.Hooks.BeforeResponse.Url != "" && !isDev {
callHook(conf.Hooks.BeforeResponse, "beforeResponse", route, params)
}

if route.CacheControl != "" {
c.Header("Cache-Control", fmt.Sprintf(route.CacheControl))
}
c.Render(http.StatusOK, render.Reader{ContentType: "image/jpeg", ContentLength: int64(img.Len()), Reader: img})

if conf.Hooks.AfterResponse.Url != "" && !isDev {
callHook(conf.Hooks.AfterResponse, "afterResponse", route, params)
}
}

func getCurrentRouteConfig(c *gin.Context) config.ConfigV001Route {
Expand Down Expand Up @@ -114,3 +134,37 @@ func getImageSize(str string) ImageSize {
height: height,
}
}

func callHook(hook config.ResponseHook, action string, confRoute config.ConfigV001Route, params map[string]string) {
client := &http.Client{}

body, _ := json.Marshal(WebHook{
Action: action,
Route: confRoute.Path,
Template: confRoute.Template,
CacheControl: confRoute.CacheControl,
Size: confRoute.Size,
Params: params,
})

responseBody := bytes.NewBuffer(body)
req, err := http.NewRequest("POST", hook.Url, responseBody)
if err != nil {
fmt.Printf("Unable to POST hook to %s. Reason: %s", hook.Url, err)
}

req.Header.Set("Content-Type", "application/json")

for _, header := range hook.Headers {
for k, v := range header {
req.Header.Set(k, v)
}
}

resp, err := client.Do(req)
if err != nil {
fmt.Printf("Unable to POST hook to %s. Reason: %s", hook.Url, err)
return
}
fmt.Println(resp.Status)
}

0 comments on commit 311b961

Please sign in to comment.