@@ -12,7 +12,9 @@ import (
1212 "io/ioutil"
1313 "net/url"
1414 "os"
15+ "path"
1516 "path/filepath"
17+ "reflect"
1618 "strconv"
1719 "strings"
1820 "time"
@@ -61,6 +63,36 @@ const (
6163 V2
6264)
6365
66+ // ServerAPIPaths has the custom paths defined for server api
67+ type ServerAPIPaths struct {
68+ Query string `yaml:"query,omitempty"`
69+ GraphQL string `yaml:"graphql,omitempty"`
70+ Config string `yaml:"config,omitempty"`
71+ PGDump string `yaml:"pg_dump,omitempty"`
72+ Version string `yaml:"version,omitempty"`
73+ }
74+
75+ // GetQueryParams - encodes the values in url
76+ func (s ServerAPIPaths ) GetQueryParams () url.Values {
77+ vals := url.Values {}
78+ t := reflect .TypeOf (s )
79+ for i := 0 ; i < t .NumField (); i ++ {
80+ field := t .Field (i )
81+ tag := field .Tag .Get ("yaml" )
82+ splitTag := strings .Split (tag , "," )
83+ if len (splitTag ) == 0 {
84+ continue
85+ }
86+ name := splitTag [0 ]
87+ if name == "-" {
88+ continue
89+ }
90+ v := reflect .ValueOf (s ).Field (i )
91+ vals .Add (name , v .String ())
92+ }
93+ return vals
94+ }
95+
6496// ErrInvalidConfigVersion - if the config version is not valid
6597var ErrInvalidConfigVersion error = fmt .Errorf ("invalid config version" )
6698
@@ -105,10 +137,19 @@ type ServerConfig struct {
105137 AccessKey string `yaml:"access_key,omitempty"`
106138 // AdminSecret (optional) Admin secret required to query the endpoint
107139 AdminSecret string `yaml:"admin_secret,omitempty"`
140+ // APIPaths (optional) API paths for server
141+ APIPaths * ServerAPIPaths `yaml:"api_paths,omitempty"`
108142
109143 ParsedEndpoint * url.URL `yaml:"-"`
110144}
111145
146+ // GetVersionEndpoint provides the url to contact the version API
147+ func (s * ServerConfig ) GetVersionEndpoint () string {
148+ nurl := * s .ParsedEndpoint
149+ nurl .Path = path .Join (nurl .Path , s .APIPaths .Version )
150+ return nurl .String ()
151+ }
152+
112153// ParseEndpoint ensures the endpoint is valid.
113154func (s * ServerConfig ) ParseEndpoint () error {
114155 nurl , err := url .Parse (s .Endpoint )
@@ -398,7 +439,7 @@ func (ec *ExecutionContext) Validate() error {
398439}
399440
400441func (ec * ExecutionContext ) checkServerVersion () error {
401- v , err := version .FetchServerVersion (ec .Config .ServerConfig .Endpoint )
442+ v , err := version .FetchServerVersion (ec .Config .ServerConfig .GetVersionEndpoint () )
402443 if err != nil {
403444 return errors .Wrap (err , "failed to get version from server" )
404445 }
@@ -441,6 +482,11 @@ func (ec *ExecutionContext) readConfig() error {
441482 v .SetDefault ("endpoint" , "http://localhost:8080" )
442483 v .SetDefault ("admin_secret" , "" )
443484 v .SetDefault ("access_key" , "" )
485+ v .SetDefault ("api_paths.query" , "v1/query" )
486+ v .SetDefault ("api_paths.graphql" , "v1/graphql" )
487+ v .SetDefault ("api_paths.config" , "v1alpha1/config" )
488+ v .SetDefault ("api_paths.pg_dump" , "v1alpha1/pg_dump" )
489+ v .SetDefault ("api_paths.version" , "v1/version" )
444490 v .SetDefault ("metadata_directory" , "" )
445491 v .SetDefault ("migrations_directory" , "migrations" )
446492 v .SetDefault ("actions.kind" , "synchronous" )
@@ -462,6 +508,13 @@ func (ec *ExecutionContext) readConfig() error {
462508 ServerConfig : ServerConfig {
463509 Endpoint : v .GetString ("endpoint" ),
464510 AdminSecret : adminSecret ,
511+ APIPaths : & ServerAPIPaths {
512+ Query : v .GetString ("api_paths.query" ),
513+ GraphQL : v .GetString ("api_paths.graphql" ),
514+ Config : v .GetString ("api_paths.config" ),
515+ PGDump : v .GetString ("api_paths.pg_dump" ),
516+ Version : v .GetString ("api_paths.version" ),
517+ },
465518 },
466519 MetadataDirectory : v .GetString ("metadata_directory" ),
467520 MigrationsDirectory : v .GetString ("migrations_directory" ),
0 commit comments