chore: bootstrap CHFS NG workspace
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"chfs-ng/internal/api"
|
||||
"chfs-ng/internal/storage"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := &api.Server{Store: storage.Local{Root: "/data"}}
|
||||
log.Println("chfs-ng development server listening on :80")
|
||||
log.Fatal(http.ListenAndServe(":80", s.Handler()))
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module chfs-ng
|
||||
|
||||
go 1.22
|
||||
@@ -0,0 +1,26 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"chfs-ng/internal/storage"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Server struct{ Store storage.Storage }
|
||||
|
||||
func (s *Server) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/chfs/files", s.files)
|
||||
mux.HandleFunc("/chfs/newdir", s.newdir)
|
||||
mux.HandleFunc("/chfs/rename", s.rename)
|
||||
mux.HandleFunc("/chfs/rmfiles", s.remove)
|
||||
mux.HandleFunc("/chfs/exist", s.exist)
|
||||
mux.HandleFunc("/chfs/session", s.session)
|
||||
return mux
|
||||
}
|
||||
func notImpl(w http.ResponseWriter) { http.Error(w, "not implemented", http.StatusNotImplemented) }
|
||||
func (s *Server) files(w http.ResponseWriter, r *http.Request) { notImpl(w) }
|
||||
func (s *Server) newdir(w http.ResponseWriter, r *http.Request) { notImpl(w) }
|
||||
func (s *Server) rename(w http.ResponseWriter, r *http.Request) { notImpl(w) }
|
||||
func (s *Server) remove(w http.ResponseWriter, r *http.Request) { notImpl(w) }
|
||||
func (s *Server) exist(w http.ResponseWriter, r *http.Request) { notImpl(w) }
|
||||
func (s *Server) session(w http.ResponseWriter, r *http.Request) { notImpl(w) }
|
||||
@@ -0,0 +1,19 @@
|
||||
package auth
|
||||
|
||||
type PermMask uint8
|
||||
|
||||
const (
|
||||
None PermMask = 0
|
||||
Read PermMask = 1 << iota
|
||||
Write
|
||||
Delete
|
||||
)
|
||||
|
||||
func (p PermMask) HasRead() bool { return p&Read != 0 }
|
||||
func (p PermMask) HasWrite() bool { return p&Write != 0 }
|
||||
func (p PermMask) HasDelete() bool { return p&Delete != 0 }
|
||||
|
||||
type Account struct {
|
||||
Username, Password string
|
||||
Mask PermMask
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
Stat(path string) (os.FileInfo, error)
|
||||
Open(path string) (*os.File, error)
|
||||
Create(path string) (*os.File, error)
|
||||
Remove(path string) error
|
||||
Rename(oldPath, newPath string) error
|
||||
MkdirAll(path string) error
|
||||
List(path string) ([]os.FileInfo, error)
|
||||
}
|
||||
|
||||
type Local struct{ Root string }
|
||||
|
||||
func (s Local) real(path string) string { return filepath.Join(s.Root, filepath.Clean("/"+path)) }
|
||||
func (s Local) Stat(path string) (os.FileInfo, error) { return os.Stat(s.real(path)) }
|
||||
func (s Local) Open(path string) (*os.File, error) { return os.Open(s.real(path)) }
|
||||
func (s Local) Create(path string) (*os.File, error) {
|
||||
p := s.real(path)
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return os.Create(p)
|
||||
}
|
||||
func (s Local) Remove(path string) error { return os.RemoveAll(s.real(path)) }
|
||||
func (s Local) Rename(a, b string) error { return os.Rename(s.real(a), s.real(b)) }
|
||||
func (s Local) MkdirAll(path string) error { return os.MkdirAll(s.real(path), 0755) }
|
||||
func (s Local) List(path string) ([]os.FileInfo, error) {
|
||||
f, err := os.Open(s.real(path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
return f.Readdir(-1)
|
||||
}
|
||||
Reference in New Issue
Block a user