wip refactoring
This commit is contained in:
@@ -37,6 +37,10 @@ func (p *RunCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
|
||||
}
|
||||
|
||||
for _, metadata := range datastore {
|
||||
if err := p.Service.MakeBackup(metadata.ID); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: failed to make backup:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
if err := p.Service.Scan(metadata.ID); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: failed to scan:", err)
|
||||
return subcommands.ExitFailure
|
||||
|
||||
@@ -78,13 +78,13 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
||||
|
||||
if !exists {
|
||||
pg.Describe(fmt.Sprintf("[%s] Pushing data...", g.Name))
|
||||
if err := push(g, cli); err != nil {
|
||||
if err := p.push(g, cli); err != nil {
|
||||
destroyPg()
|
||||
fmt.Fprintln(os.Stderr, "failed to push:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
pg.Describe(fmt.Sprintf("[%s] Pushing backup...", g.Name))
|
||||
if err := pushBackup(g, cli); err != nil {
|
||||
if err := p.pushBackup(g, cli); err != nil {
|
||||
destroyPg()
|
||||
slog.Warn("failed to push backup files", "err", err)
|
||||
}
|
||||
@@ -109,12 +109,12 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
||||
}
|
||||
|
||||
pg.Describe(fmt.Sprintf("[%s] Pulling backup...", g.Name))
|
||||
if err := pullBackup(g, cli); err != nil {
|
||||
if err := p.pullBackup(g, cli); err != nil {
|
||||
slog.Warn("failed to pull backup files", "err", err)
|
||||
}
|
||||
|
||||
pg.Describe(fmt.Sprintf("[%s] Pushing backup...", g.Name))
|
||||
if err := pushBackup(g, cli); err != nil {
|
||||
if err := p.pushBackup(g, cli); err != nil {
|
||||
slog.Warn("failed to push backup files", "err", err)
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
||||
|
||||
if g.Version > remoteMetadata.Version {
|
||||
pg.Describe(fmt.Sprintf("[%s] Pushing data...", g.Name))
|
||||
if err := push(g, cli); err != nil {
|
||||
if err := p.push(g, cli); err != nil {
|
||||
destroyPg()
|
||||
fmt.Fprintln(os.Stderr, "failed to push:", err)
|
||||
return subcommands.ExitFailure
|
||||
@@ -145,7 +145,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
||||
|
||||
if g.Version < remoteMetadata.Version {
|
||||
destroyPg()
|
||||
if err := pull(r.GameID, cli); err != nil {
|
||||
if err := p.pull(r.GameID, cli); err != nil {
|
||||
destroyPg()
|
||||
fmt.Fprintln(os.Stderr, "failed to push:", err)
|
||||
return subcommands.ExitFailure
|
||||
@@ -220,7 +220,7 @@ func (p *SyncCmd) conflict(gameID string, m, remoteMetadata repository.Metadata,
|
||||
}
|
||||
|
||||
func (p *SyncCmd) push(m repository.Metadata, cli *client.Client) error {
|
||||
|
||||
return p.Service.PushArchive(m.ID, "", cli)
|
||||
}
|
||||
|
||||
func (p *SyncCmd) pushBackup(m repository.Metadata, cli *client.Client) error {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user