Support additional headers for the ghost http client

This commit is contained in:
2021-12-16 00:33:40 +01:00
parent 760bcf6c94
commit 0b003c200a
3 changed files with 9 additions and 4 deletions
+4 -3
View File
@@ -10,8 +10,9 @@ import (
// Backend contains backend connection-specific configuration // Backend contains backend connection-specific configuration
type Backend struct { type Backend struct {
Addr string `mapstructure:"addr"` Addr string `mapstructure:"addr"`
Secured bool `mapstructure:"secured"` Secured bool `mapstructure:"secured"`
Headers map[string]string `mapstructure:"headers"`
} }
// Content contains content-specific configuration // Content contains content-specific configuration
@@ -42,9 +43,9 @@ func init() {
pflag.String("unix", "", "unix socket path to listen") pflag.String("unix", "", "unix socket path to listen")
pflag.String("base", "", "http URI prefix") pflag.String("base", "", "http URI prefix")
pflag.StringToString("content.backend.headers", nil, "map of additional headers to send")
pflag.String("content.backend.addr", "demo.ghost.io:443", "ghost backend addr") pflag.String("content.backend.addr", "demo.ghost.io:443", "ghost backend addr")
pflag.Bool("content.backend.secured", true, "is ghost backend secured") pflag.Bool("content.backend.secured", true, "is ghost backend secured")
pflag.String("content.backend", "demo.ghost.io:443", "ghost backend addr")
pflag.String("content.key", "22444f78447824223cefc48062", "ghost content api key") pflag.String("content.key", "22444f78447824223cefc48062", "ghost content api key")
pflag.String("content.pinned", "contact", "pinned page slug") pflag.String("content.pinned", "contact", "pinned page slug")
pflag.Int("content.postsPerPage", 5, "amount of posts per page") pflag.Int("content.postsPerPage", 5, "amount of posts per page")
+4 -1
View File
@@ -25,6 +25,7 @@ type HTTPClient struct {
ContentKey string ContentKey string
Addr string Addr string
Secured bool Secured bool
Headers map[string]string
client *fasthttp.HostClient client *fasthttp.HostClient
@@ -63,6 +64,9 @@ func (g *HTTPClient) doQuery(method string, v easyjson.Unmarshaler, params ...Qu
uri.SetScheme("https") uri.SetScheme("https")
} }
for hKey, hValue := range g.Headers {
req.Header.Add(hKey, hValue)
}
for _, param := range params { for _, param := range params {
param.Apply(&req.Header, uri.QueryArgs()) param.Apply(&req.Header, uri.QueryArgs())
} }
@@ -78,7 +82,6 @@ func (g *HTTPClient) doQuery(method string, v easyjson.Unmarshaler, params ...Qu
resBytes := res.Body() resBytes := res.Body()
if resBytes == nil && v == nil { if resBytes == nil && v == nil {
return fmt.Errorf("nothing to unmarshal") return fmt.Errorf("nothing to unmarshal")
} }
if resBytes == nil { if resBytes == nil {
return return
+1
View File
@@ -56,6 +56,7 @@ func main() {
ghostClient := &ghost.HTTPClient{ ghostClient := &ghost.HTTPClient{
Addr: config.Content.Backend.Addr, Addr: config.Content.Backend.Addr,
Secured: config.Content.Backend.Secured, Secured: config.Content.Backend.Secured,
Headers: config.Content.Backend.Headers,
ContentKey: config.Content.Key, ContentKey: config.Content.Key,
QueryTimeout: time.Second, QueryTimeout: time.Second,
} }