wip refactoring

This commit is contained in:
2025-08-10 02:03:27 +02:00
parent 810c5ac627
commit e34dc704ca
6 changed files with 333 additions and 48 deletions

View File

@@ -1,6 +1,7 @@
package api
import (
"cloudsave/pkg/data"
"cloudsave/pkg/repository"
"encoding/json"
"errors"
@@ -19,16 +20,18 @@ import (
type (
HTTPServer struct {
Server *http.Server
Service *data.Service
documentRoot string
}
)
// NewServer start the http server
func NewServer(documentRoot string, creds map[string]string, port int) *HTTPServer {
func NewServer(documentRoot string, srv *data.Service, creds map[string]string, port int) *HTTPServer {
if !filepath.IsAbs(documentRoot) {
panic("the document root is not an absolute path")
}
s := &HTTPServer{
Service: srv,
documentRoot: documentRoot,
}
router := chi.NewRouter()
@@ -194,14 +197,14 @@ func (s HTTPServer) upload(w http.ResponseWriter, r *http.Request) {
defer file.Close()
//TODO make a transaction
if err := data.UpdateMetadata(id, s.documentRoot, m); err != nil {
if err := s.Service.UpdateMetadata(id, m); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to write metadata to disk:", err)
internalServerError(w, r)
return
}
if err := data.Write(id, s.documentRoot, file); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to write file to disk:", err)
if err := s.Service.Copy(id, file); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to write data to disk:", err)
internalServerError(w, r)
return
}
@@ -267,8 +270,8 @@ func (s HTTPServer) histUpload(w http.ResponseWriter, r *http.Request) {
}
defer file.Close()
if err := data.WriteHist(gameID, s.documentRoot, uuid, file); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to write file to disk:", err)
if err := s.Service.CopyBackup(gameID, uuid, file); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to write data to disk:", err)
internalServerError(w, r)
return
}
@@ -323,10 +326,10 @@ func (s HTTPServer) histExists(w http.ResponseWriter, r *http.Request) {
gameID := chi.URLParam(r, "id")
uuid := chi.URLParam(r, "uuid")
finfo, err := data.ArchiveInfo(gameID, s.documentRoot, uuid)
finfo, err := s.Service.Backup(gameID, uuid)
if err != nil {
if errors.Is(err, data.ErrBackupNotExists) {
notFound("backup not found", w, r)
if errors.Is(err, repository.ErrNotFound) {
notFound("not found", w, r)
return
}
fmt.Fprintln(os.Stderr, "error: failed to read data:", err)
@@ -340,10 +343,10 @@ func (s HTTPServer) histExists(w http.ResponseWriter, r *http.Request) {
func (s HTTPServer) hash(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
sum, err := data.Hash(id, s.documentRoot)
m, err := s.Service.One(id)
if err != nil {
if errors.Is(err, data.ErrNotExists) {
notFound("id not found", w, r)
if errors.Is(err, repository.ErrNotFound) {
notFound("not found", w, r)
return
}
fmt.Fprintln(os.Stderr, "error: an error occured while calculating the hash:", err)
@@ -351,7 +354,7 @@ func (s HTTPServer) hash(w http.ResponseWriter, r *http.Request) {
return
}
ok(sum, w, r)
ok(m.MD5, w, r)
}
func (s HTTPServer) metadata(w http.ResponseWriter, r *http.Request) {

View File

@@ -4,6 +4,8 @@ import (
"cloudsave/cmd/server/api"
"cloudsave/cmd/server/security/htpasswd"
"cloudsave/pkg/constants"
"cloudsave/pkg/data"
"cloudsave/pkg/repository"
"flag"
"fmt"
"path/filepath"
@@ -16,16 +18,36 @@ func run() {
var documentRoot string
var port int
var noCache bool
flag.StringVar(&documentRoot, "document-root", defaultDocumentRoot, "Define the path to the document root")
flag.IntVar(&port, "port", 8080, "Define the port of the server")
flag.BoolVar(&noCache, "no-cache", false, "Disable the cache")
flag.Parse()
h, err := htpasswd.Open(filepath.Join(documentRoot, ".htpasswd"))
if err != nil {
fatal("failed to load .htpasswd: "+err.Error(), 1)
}
var repo repository.Repository
if noCache {
r, err := repository.NewEagerRepository(filepath.Join(documentRoot, "data"))
if err != nil {
fatal("failed to load datastore: "+err.Error(), 1)
}
if err := r.Preload(); err != nil {
fatal("failed to load datastore: "+err.Error(), 1)
}
repo = r
} else {
repo, err = repository.NewLazyRepository(filepath.Join(documentRoot, "data"))
if err != nil {
fatal("failed to load datastore: "+err.Error(), 1)
}
}
server := api.NewServer(documentRoot, h.Content(), port)
s := data.NewService(repo)
server := api.NewServer(documentRoot, s, h.Content(), port)
fmt.Println("starting server at :" + strconv.Itoa(port))
if err := server.Server.ListenAndServe(); err != nil {