MCPcopy Index your code
hub / github.com/coder/coder / newBinHandler

Function newBinHandler

site/bin.go:110–146  ·  view source on GitHub ↗
(options *Options)

Source from the content-addressed store, hash-verified

108}
109
110func newBinHandler(options *Options) (*binHandler, error) {
111 cacheDir := options.CacheDir
112 compressedCacheDir := ""
113 if cacheDir != "" {
114 // split the cache dir into ./compressed and ./orig containing the compressed files and the original
115 // uncompressed files respectively.
116 compressedCacheDir = filepath.Join(cacheDir, "compressed")
117 err := os.MkdirAll(compressedCacheDir, 0o700)
118 if err != nil {
119 // cached dir was provided, but we can't write to it
120 return nil, xerrors.Errorf("failed to create compressed directory in cache dir: %w", err)
121 }
122 cacheDir = filepath.Join(cacheDir, "orig")
123 err = os.MkdirAll(cacheDir, 0o700)
124 if err != nil {
125 return nil, xerrors.Errorf("failed to create orig directory in cache dir: %w", err)
126 }
127 }
128 // note that ExtractOrReadBinFS handles an empty cacheDir; this often arises in testing.
129 binFS, binHashes, err := ExtractOrReadBinFS(cacheDir, options.SiteFS)
130 if err != nil {
131 return nil, xerrors.Errorf("extract or read bin filesystem: %w", err)
132 }
133 h := &binHandler{
134 metadataCache: newBinMetadataCache(binFS, binHashes),
135 }
136 if compressedCacheDir != "" {
137 cmp := cachecompress.NewCompressor(options.Logger, CompressionLevel, compressedCacheDir, binFS)
138 for encoding, fn := range StandardEncoders {
139 cmp.SetEncoder(encoding, fn)
140 }
141 h.handler = cmp
142 } else {
143 h.handler = http.FileServer(binFS)
144 }
145 return h, nil
146}
147
148// ExtractOrReadBinFS checks the provided fs for compressed coder binaries and
149// extracts them into dest/bin if found. As a fallback, the provided FS is

Callers 1

NewFunction · 0.85

Calls 6

SetEncoderMethod · 0.95
NewCompressorFunction · 0.92
ExtractOrReadBinFSFunction · 0.85
newBinMetadataCacheFunction · 0.85
MkdirAllMethod · 0.80
ErrorfMethod · 0.45

Tested by

no test coverage detected