moved fileserver to separate file.
This commit is contained in:
parent
2f9cead5c3
commit
38869b5faa
59
cmd/converge/fileserver.go
Normal file
59
cmd/converge/fileserver.go
Normal file
@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// filters to filter html content.
|
||||
var filters map[string]string = make(map[string]string)
|
||||
|
||||
type FilteredFileSystem struct {
|
||||
fs http.FileSystem
|
||||
}
|
||||
|
||||
func (ffs FilteredFileSystem) Open(name string) (http.File, error) {
|
||||
f, err := ffs.fs.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !strings.HasSuffix(name, ".html") {
|
||||
return f, nil
|
||||
}
|
||||
return NewFilteredFile(f, filters)
|
||||
}
|
||||
|
||||
type FilteredFile struct {
|
||||
http.File
|
||||
|
||||
// Read bytes 0..(index-1) already
|
||||
index int
|
||||
fullContents *bytes.Buffer
|
||||
}
|
||||
|
||||
func NewFilteredFile(f http.File, filter map[string]string) (FilteredFile, error) {
|
||||
file := FilteredFile{
|
||||
index: 0,
|
||||
fullContents: bytes.NewBuffer(make([]byte, 0)),
|
||||
}
|
||||
file.File = f
|
||||
|
||||
_, err := io.Copy(file.fullContents, file.File)
|
||||
if err != nil {
|
||||
return FilteredFile{}, err
|
||||
}
|
||||
contents := file.fullContents.String()
|
||||
for key, value := range filter {
|
||||
key = "@" + key + "@"
|
||||
contents = strings.ReplaceAll(contents, key, value)
|
||||
}
|
||||
file.fullContents = bytes.NewBufferString(contents)
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (ff FilteredFile) Read(p []byte) (n int, err error) {
|
||||
return ff.fullContents.Read(p)
|
||||
}
|
@ -1,11 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"converge/pkg/converge"
|
||||
"converge/pkg/websocketutil"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
@ -29,9 +27,6 @@ func catchAllHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// filters to filter html content.
|
||||
var filters map[string]string = make(map[string]string)
|
||||
|
||||
type FileHandlerFilter struct {
|
||||
http.Handler
|
||||
}
|
||||
@ -64,54 +59,6 @@ func (handler FileHandlerFilter) ServeHTTP(w http.ResponseWriter, r *http.Reques
|
||||
handler.Handler.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
type FilteredFileSystem struct {
|
||||
fs http.FileSystem
|
||||
}
|
||||
|
||||
func (ffs FilteredFileSystem) Open(name string) (http.File, error) {
|
||||
f, err := ffs.fs.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !strings.HasSuffix(name, ".html") {
|
||||
return f, nil
|
||||
}
|
||||
return NewFilteredFile(f, filters)
|
||||
}
|
||||
|
||||
type FilteredFile struct {
|
||||
http.File
|
||||
|
||||
// Read bytes 0..(index-1) already
|
||||
index int
|
||||
fullContents *bytes.Buffer
|
||||
}
|
||||
|
||||
func NewFilteredFile(f http.File, filter map[string]string) (FilteredFile, error) {
|
||||
file := FilteredFile{
|
||||
index: 0,
|
||||
fullContents: bytes.NewBuffer(make([]byte, 0)),
|
||||
}
|
||||
file.File = f
|
||||
|
||||
_, err := io.Copy(file.fullContents, file.File)
|
||||
if err != nil {
|
||||
return FilteredFile{}, err
|
||||
}
|
||||
contents := file.fullContents.String()
|
||||
for key, value := range filter {
|
||||
key = "@" + key + "@"
|
||||
contents = strings.ReplaceAll(contents, key, value)
|
||||
}
|
||||
file.fullContents = bytes.NewBufferString(contents)
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (ff FilteredFile) Read(p []byte) (n int, err error) {
|
||||
return ff.fullContents.Read(p)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
downloadDir := "downloads"
|
||||
|
Loading…
Reference in New Issue
Block a user