github.com/gocaveman/caveman@v0.0.0-20191211162744-0ddf99dbdf6e/renderer/viewregistry/viewregistry.go (about)

     1  // Registry for views to automatically be made available on the site.
     2  package viewregistry
     3  
     4  import (
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/gocaveman/caveman/filesystem/fsutil"
     9  	"github.com/gocaveman/caveman/webutil"
    10  )
    11  
    12  // OnlyReadableFromMain determines if `Contents()` can be called from packages other than main.
    13  // By default we prevent this in order to enforce that object wiring is done in the main package
    14  // and not from other components.  The intention is to prevent hidden code dependencies from
    15  // being introduced.
    16  // (TODO: link to doc which explains why in more detail.)
    17  var OnlyReadableFromMain = true
    18  
    19  var reg webutil.NamedSequence
    20  
    21  // MustRegister adds a new FileSystem to the view registry.  Duplicates (seq, name or value) are not detected or prevented.
    22  func MustRegister(seq float64, name string, viewFS http.FileSystem) {
    23  	log.Printf("viewregistry is deprecated, use tmplregistry instead")
    24  	reg = append(reg, webutil.NamedSequenceItem{Sequence: seq, Name: name, Value: viewFS})
    25  }
    26  
    27  // Contents returns the current contents of the registry as a NamedSequence.
    28  func Contents() webutil.NamedSequence {
    29  	if OnlyReadableFromMain {
    30  		webutil.MainOnly(1)
    31  	}
    32  	return reg.SortedCopy()
    33  }
    34  
    35  // MakeFS sorts the NamedSequence you provide and returns a FileSystem which is
    36  // the combination of the Values in this list.   Will panic if any value is
    37  // not a http.FileSystem. If overrideFS is not nil, it will be included as the
    38  // first FileSystem in the stack and will take priority over the rest.
    39  // If debug is true then opened files will output debug info to the log.
    40  func MakeFS(overrideFS http.FileSystem, contents webutil.NamedSequence, debug bool) http.FileSystem {
    41  	l := contents.SortedCopy()
    42  
    43  	fslist := make([]http.FileSystem, 0, len(l))
    44  	if overrideFS != nil {
    45  
    46  		fs := overrideFS
    47  
    48  		if debug {
    49  			fs = fsutil.NewHTTPFuncFS(func(name string) (http.File, error) {
    50  				f, err := overrideFS.Open(name)
    51  				if f != nil {
    52  					log.Printf("View file opened from override: %s", name)
    53  				}
    54  				return f, err
    55  			})
    56  		}
    57  
    58  		fslist = append(fslist, fs)
    59  	}
    60  
    61  	for i := range l {
    62  
    63  		item := l[i]
    64  
    65  		itemFS := item.Value.(http.FileSystem)
    66  		fs := itemFS
    67  
    68  		if debug {
    69  			fs = fsutil.NewHTTPFuncFS(func(name string) (http.File, error) {
    70  				f, err := itemFS.Open(name)
    71  				if f != nil {
    72  					log.Printf("View file opened (sequence=%v, name=%q): %s", item.Sequence, item.Name, name)
    73  				}
    74  				return f, err
    75  			})
    76  		}
    77  
    78  		fslist = append(fslist, fs)
    79  	}
    80  
    81  	return fsutil.NewHTTPStackedFileSystem(fslist...)
    82  }