moved fileserver to separate file.

This commit is contained in:
Erik Brakkee 2024-07-23 19:36:35 +02:00
parent 633b79fae4
commit cd477b7b0a
2 changed files with 59 additions and 53 deletions

View 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)
}

View File

@ -1,11 +1,9 @@
package main package main
import ( import (
"bytes"
"converge/pkg/converge" "converge/pkg/converge"
"converge/pkg/websocketutil" "converge/pkg/websocketutil"
"fmt" "fmt"
"io"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -29,9 +27,6 @@ func catchAllHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
// filters to filter html content.
var filters map[string]string = make(map[string]string)
type FileHandlerFilter struct { type FileHandlerFilter struct {
http.Handler http.Handler
} }
@ -64,54 +59,6 @@ func (handler FileHandlerFilter) ServeHTTP(w http.ResponseWriter, r *http.Reques
handler.Handler.ServeHTTP(w, r) 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() { func main() {
downloadDir := "downloads" downloadDir := "downloads"