github.com/avenga/couper@v1.12.2/config/runtime/server/options.go (about) 1 package server 2 3 import ( 4 "path" 5 6 "github.com/sirupsen/logrus" 7 8 "github.com/avenga/couper/config" 9 "github.com/avenga/couper/errors" 10 "github.com/avenga/couper/utils" 11 ) 12 13 type Context interface { 14 Options() *Options 15 } 16 17 type Options struct { 18 APIErrTpls map[*config.API]*errors.Template 19 FilesErrTpls []*errors.Template 20 ServerErrTpl *errors.Template 21 APIBasePaths map[*config.API]string 22 FilesBasePaths []string 23 SPABasePaths []string 24 SrvBasePath string 25 ServerName string 26 TLS *config.ServerTLS 27 } 28 29 func NewServerOptions(conf *config.Server, logger *logrus.Entry) (*Options, error) { 30 options := &Options{ 31 ServerErrTpl: errors.DefaultHTML, 32 } 33 34 if conf == nil { 35 return options, nil 36 } 37 38 options.TLS = conf.TLS 39 40 options.FilesErrTpls = make([]*errors.Template, len(conf.Files)) 41 for i := range conf.Files { 42 options.FilesErrTpls[i] = errors.DefaultHTML 43 } 44 45 options.ServerName = conf.Name 46 options.SrvBasePath = path.Join("/", conf.BasePath) 47 48 if conf.ErrorFile != "" { 49 tpl, err := errors.NewTemplateFromFile(conf.ErrorFile, logger) 50 if err != nil { 51 return nil, err 52 } 53 54 options.ServerErrTpl = tpl 55 for i := range conf.Files { 56 options.FilesErrTpls[i] = tpl 57 } 58 } 59 60 if len(conf.APIs) > 0 { 61 options.APIBasePaths = make(map[*config.API]string) 62 options.APIErrTpls = make(map[*config.API]*errors.Template) 63 } 64 65 for _, api := range conf.APIs { 66 options.APIBasePaths[api] = path.Join(options.SrvBasePath, api.BasePath) 67 68 if api.ErrorFile != "" { 69 tpl, err := errors.NewTemplateFromFile(api.ErrorFile, logger) 70 if err != nil { 71 return nil, err 72 } 73 options.APIErrTpls[api] = tpl 74 } else { 75 options.APIErrTpls[api] = errors.DefaultJSON 76 } 77 } 78 79 options.FilesBasePaths = make([]string, len(conf.Files)) 80 for i, f := range conf.Files { 81 if f.ErrorFile != "" { 82 tpl, err := errors.NewTemplateFromFile(f.ErrorFile, logger) 83 if err != nil { 84 return nil, err 85 } 86 87 options.FilesErrTpls[i] = tpl 88 } 89 90 options.FilesBasePaths[i] = utils.JoinOpenAPIPath(options.SrvBasePath, f.BasePath) 91 } 92 93 for _, s := range conf.SPAs { 94 options.SPABasePaths = append(options.SPABasePaths, utils.JoinOpenAPIPath(options.SrvBasePath, s.BasePath)) 95 } 96 97 return options, nil 98 }