github.com/blend/go-sdk@v1.20220411.3/web/view_cache_config.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"context"
    12  
    13  	"github.com/blend/go-sdk/env"
    14  )
    15  
    16  // ViewCacheConfig is a config for the view cache.
    17  type ViewCacheConfig struct {
    18  	// LiveReload indicates if we should store compiled views in memory for re-use (default), or read them from disk each load.
    19  	LiveReload bool `json:"liveReload,omitempty" yaml:"liveReload,omitempty" env:"LIVE_RELOAD"`
    20  	// Paths are a list of view paths to include in the templates list.
    21  	Paths []string `json:"paths,omitempty" yaml:"paths,omitempty"`
    22  	// BufferPoolSize is the size of the re-usable buffer pool for rendering views.
    23  	BufferPoolSize int `json:"bufferPoolSize,omitempty" yaml:"bufferPoolSize,omitempty"`
    24  
    25  	// InternalErrorTemplateName is the template name to use for the view result provider `InternalError` result.
    26  	InternalErrorTemplateName string `json:"internalErrorTemplateName,omitempty" yaml:"internalErrorTemplateName,omitempty"`
    27  	// BadRequestTemplateName is the template name to use for the view result provider `BadRequest` result.
    28  	BadRequestTemplateName string `json:"badRequestTemplateName,omitempty" yaml:"badRequestTemplateName,omitempty"`
    29  	// NotFoundTemplateName is the template name to use for the view result provider `NotFound` result.
    30  	NotFoundTemplateName string `json:"notFoundTemplateName,omitempty" yaml:"notFoundTemplateName,omitempty"`
    31  	// NotAuthorizedTemplateName is the template name to use for the view result provider `NotAuthorized` result.
    32  	NotAuthorizedTemplateName string `json:"notAuthorizedTemplateName,omitempty" yaml:"notAuthorizedTemplateName,omitempty"`
    33  	// StatusTemplateName is the template name to use for the view result provider status result.
    34  	StatusTemplateName string `json:"statusTemplateName,omitempty" yaml:"statusTemplateName,omitempty"`
    35  }
    36  
    37  // Resolve adds extra resolution steps when we setup the config.
    38  func (vcc *ViewCacheConfig) Resolve(ctx context.Context) error {
    39  	return env.GetVars(ctx).ReadInto(vcc)
    40  }
    41  
    42  // BufferPoolSizeOrDefault gets the buffer pool size or a default.
    43  func (vcc ViewCacheConfig) BufferPoolSizeOrDefault() int {
    44  	if vcc.BufferPoolSize > 0 {
    45  		return vcc.BufferPoolSize
    46  	}
    47  	return DefaultViewBufferPoolSize
    48  }
    49  
    50  // InternalErrorTemplateNameOrDefault returns the internal error template name for the app.
    51  func (vcc ViewCacheConfig) InternalErrorTemplateNameOrDefault() string {
    52  	if vcc.InternalErrorTemplateName != "" {
    53  		return vcc.InternalErrorTemplateName
    54  	}
    55  	return DefaultTemplateNameInternalError
    56  }
    57  
    58  // BadRequestTemplateNameOrDefault returns the bad request template name for the app.
    59  func (vcc ViewCacheConfig) BadRequestTemplateNameOrDefault() string {
    60  	if vcc.BadRequestTemplateName != "" {
    61  		return vcc.BadRequestTemplateName
    62  	}
    63  	return DefaultTemplateNameBadRequest
    64  }
    65  
    66  // NotFoundTemplateNameOrDefault returns the not found template name for the app.
    67  func (vcc ViewCacheConfig) NotFoundTemplateNameOrDefault() string {
    68  	if vcc.NotFoundTemplateName != "" {
    69  		return vcc.NotFoundTemplateName
    70  	}
    71  	return DefaultTemplateNameNotFound
    72  }
    73  
    74  // NotAuthorizedTemplateNameOrDefault returns the not authorized template name for the app.
    75  func (vcc ViewCacheConfig) NotAuthorizedTemplateNameOrDefault() string {
    76  	if vcc.NotAuthorizedTemplateName != "" {
    77  		return vcc.NotAuthorizedTemplateName
    78  	}
    79  	return DefaultTemplateNameNotAuthorized
    80  }
    81  
    82  // StatusTemplateNameOrDefault returns the not authorized template name for the app.
    83  func (vcc ViewCacheConfig) StatusTemplateNameOrDefault() string {
    84  	if vcc.StatusTemplateName != "" {
    85  		return vcc.StatusTemplateName
    86  	}
    87  	return DefaultTemplateNameStatus
    88  }