feat: Start implementation of Afero-based filesystem abstraction
This commit is contained in:
24
cmd/stsrv/main.go
Normal file
24
cmd/stsrv/main.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/pojntfx/stfs/internal/fs"
|
||||
"github.com/pojntfx/stfs/internal/handlers"
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
func main() {
|
||||
laddr := flag.String("laddr", "localhost:1337", "Listen address")
|
||||
dir := flag.String("dir", "/", "Directory to use as the root directory")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
stfs := afero.NewHttpFs(fs.NewSTFS())
|
||||
|
||||
log.Println("Listening on", *laddr)
|
||||
|
||||
log.Fatal(http.ListenAndServe(*laddr, handlers.PanicHandler(http.FileServer(stfs.Dir(*dir)))))
|
||||
}
|
||||
124
internal/fs/afero.go
Normal file
124
internal/fs/afero.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotImplemented = errors.New("not implemented")
|
||||
)
|
||||
|
||||
type STFS struct{}
|
||||
|
||||
func NewSTFS() afero.Fs {
|
||||
return &STFS{}
|
||||
}
|
||||
|
||||
func (STFS) Name() string { return "STFS" }
|
||||
|
||||
func (STFS) Create(name string) (afero.File, error) {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// f, e := os.Create(name)
|
||||
// if f == nil {
|
||||
// return nil, e
|
||||
// }
|
||||
// return f, e
|
||||
}
|
||||
|
||||
func (STFS) Mkdir(name string, perm os.FileMode) error {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.Mkdir(name, perm)
|
||||
}
|
||||
|
||||
func (STFS) MkdirAll(path string, perm os.FileMode) error {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.MkdirAll(path, perm)
|
||||
}
|
||||
|
||||
func (STFS) Open(name string) (afero.File, error) {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// f, e := os.Open(name)
|
||||
// if f == nil {
|
||||
// return nil, e
|
||||
// }
|
||||
// return f, e
|
||||
}
|
||||
|
||||
func (STFS) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// f, e := os.OpenFile(name, flag, perm)
|
||||
// if f == nil {
|
||||
// return nil, e
|
||||
// }
|
||||
// return f, e
|
||||
}
|
||||
|
||||
func (STFS) Remove(name string) error {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.Remove(name)
|
||||
}
|
||||
|
||||
func (STFS) RemoveAll(path string) error {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
func (STFS) Rename(oldname, newname string) error {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.Rename(oldname, newname)
|
||||
}
|
||||
|
||||
func (STFS) Stat(name string) (os.FileInfo, error) {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.Stat(name)
|
||||
}
|
||||
|
||||
func (STFS) Chmod(name string, mode os.FileMode) error {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.Chmod(name, mode)
|
||||
}
|
||||
|
||||
func (STFS) Chown(name string, uid, gid int) error {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.Chown(name, uid, gid)
|
||||
}
|
||||
|
||||
func (STFS) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.Chtimes(name, atime, mtime)
|
||||
}
|
||||
|
||||
func (STFS) LstatIfPossible(name string) (os.FileInfo, bool, error) {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// fi, err := os.Lstat(name)
|
||||
// return fi, true, err
|
||||
}
|
||||
|
||||
func (STFS) SymlinkIfPossible(oldname, newname string) error {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.Symlink(oldname, newname)
|
||||
}
|
||||
|
||||
func (STFS) ReadlinkIfPossible(name string) (string, error) {
|
||||
panic(ErrNotImplemented)
|
||||
|
||||
// return os.Readlink(name)
|
||||
}
|
||||
22
internal/handlers/panic.go
Normal file
22
internal/handlers/panic.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
func PanicHandler(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
http.Error(w, fmt.Sprintf("%v", r), http.StatusInternalServerError)
|
||||
|
||||
log.Println("Error:", r, "\nStack:", string(debug.Stack()))
|
||||
}
|
||||
}()
|
||||
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user