sync
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"cloudsave/pkg/game"
|
||||
"cloudsave/pkg/remote"
|
||||
"cloudsave/pkg/remote/client"
|
||||
"cloudsave/pkg/tools/prompt/credentials"
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
@@ -29,37 +33,99 @@ func (p *SyncCmd) SetFlags(f *flag.FlagSet) {
|
||||
}
|
||||
|
||||
func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
_, err := remote.All()
|
||||
remotes, err := remote.All()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: failed to load datastore:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
for _, remote := range remotes {
|
||||
|
||||
username, password, err := credentials.Read()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: failed to read std output:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
for _, r := range remotes {
|
||||
if !ping(r.URL, username, password) {
|
||||
slog.Warn("remote is unavailable", "url", r.URL)
|
||||
continue
|
||||
}
|
||||
|
||||
client := client.New(r.URL, username, password)
|
||||
|
||||
hlocal, err := game.Hash(r.GameID)
|
||||
if err != nil {
|
||||
slog.Error(err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
hremote, _ := client.Hash(r.GameID)
|
||||
|
||||
vlocal, err := game.Version(r.GameID)
|
||||
if err != nil {
|
||||
slog.Error(err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
vremote, _ := client.Version(r.GameID)
|
||||
|
||||
if hlocal == hremote {
|
||||
fmt.Println("already up-to-date")
|
||||
continue
|
||||
}
|
||||
|
||||
if vremote == 0 {
|
||||
fmt.Println("push")
|
||||
continue
|
||||
}
|
||||
|
||||
if vlocal > vremote {
|
||||
fmt.Println("push")
|
||||
continue
|
||||
}
|
||||
|
||||
if vlocal < vremote {
|
||||
fmt.Println("pull")
|
||||
continue
|
||||
}
|
||||
|
||||
if vlocal == vremote {
|
||||
fmt.Println("conflict")
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
func hash(path string) string {
|
||||
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
||||
func ping(remote, username, password string) bool {
|
||||
cli := http.Client{}
|
||||
|
||||
hburl, err := url.JoinPath(remote, "heartbeat")
|
||||
if err != nil {
|
||||
notFound("id not found", w, r)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// Create MD5 hasher
|
||||
hasher := md5.New()
|
||||
|
||||
// Copy file content into hasher
|
||||
if _, err := io.Copy(hasher, f); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: an error occured while reading data:", err)
|
||||
internalServerError(w, r)
|
||||
return
|
||||
fmt.Fprintln(os.Stderr, "cannot connect to remote:", err)
|
||||
return false
|
||||
}
|
||||
|
||||
// Get checksum result
|
||||
sum := hasher.Sum(nil)
|
||||
req, err := http.NewRequest("GET", hburl, nil)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "cannot connect to remote:", err)
|
||||
return false
|
||||
}
|
||||
|
||||
req.SetBasicAuth(username, password)
|
||||
|
||||
res, err := cli.Do(req)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "cannot connect to remote:", err)
|
||||
return false
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
fmt.Fprintln(os.Stderr, "cannot connect to remote: server return code", res.StatusCode)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@ func NewServer(documentRoot string, creds map[string]string, port int) *HTTPServ
|
||||
saveRouter.Post("/{id}/data", s.upload)
|
||||
saveRouter.Get("/{id}/data", s.download)
|
||||
saveRouter.Get("/{id}/hash", s.hash)
|
||||
saveRouter.Get("/{id}/version", s.version)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -220,3 +221,39 @@ func (s HTTPServer) hash(w http.ResponseWriter, r *http.Request) {
|
||||
sum := hasher.Sum(nil)
|
||||
ok(sum, w, r)
|
||||
}
|
||||
|
||||
func (s HTTPServer) version(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
path := filepath.Clean(filepath.Join(s.documentRoot, "data", id))
|
||||
|
||||
sdir, err := os.Stat(path)
|
||||
if err != nil {
|
||||
notFound("id not found", w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if !sdir.IsDir() {
|
||||
notFound("id not found", w, r)
|
||||
return
|
||||
}
|
||||
|
||||
path = filepath.Join(path, "metadata.json")
|
||||
|
||||
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
notFound("id not found", w, r)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var metadata game.Metadata
|
||||
d := json.NewDecoder(f)
|
||||
err = d.Decode(&metadata)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: an error occured while reading data:", err)
|
||||
internalServerError(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
ok(metadata.Version, w, r)
|
||||
}
|
||||
|
||||
@@ -1,34 +1,16 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"cloudsave/pkg/remote/obj"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
httpCore struct {
|
||||
Status int `json:"status"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
httpError struct {
|
||||
httpCore
|
||||
Error string `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
httpObject struct {
|
||||
httpCore
|
||||
Data any `json:"data"`
|
||||
}
|
||||
)
|
||||
|
||||
func internalServerError(w http.ResponseWriter, r *http.Request) {
|
||||
e := httpError{
|
||||
httpCore: httpCore{
|
||||
e := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusInternalServerError,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
@@ -50,8 +32,8 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func notFound(message string, w http.ResponseWriter, r *http.Request) {
|
||||
e := httpError{
|
||||
httpCore: httpCore{
|
||||
e := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusNotFound,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
@@ -73,8 +55,8 @@ func notFound(message string, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
||||
e := httpError{
|
||||
httpCore: httpCore{
|
||||
e := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusMethodNotAllowed,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
@@ -96,8 +78,8 @@ func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func unauthorized(w http.ResponseWriter, r *http.Request) {
|
||||
e := httpError{
|
||||
httpCore: httpCore{
|
||||
e := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusUnauthorized,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
@@ -120,8 +102,8 @@ func unauthorized(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func forbidden(w http.ResponseWriter, r *http.Request) {
|
||||
e := httpError{
|
||||
httpCore: httpCore{
|
||||
e := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusForbidden,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
@@ -142,14 +124,14 @@ func forbidden(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func ok(obj interface{}, w http.ResponseWriter, r *http.Request) {
|
||||
e := httpObject{
|
||||
httpCore: httpCore{
|
||||
func ok(o interface{}, w http.ResponseWriter, r *http.Request) {
|
||||
e := obj.HTTPObject{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusOK,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
Data: obj,
|
||||
Data: o,
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(e)
|
||||
@@ -164,8 +146,8 @@ func ok(obj interface{}, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func badRequest(message string, w http.ResponseWriter, r *http.Request) {
|
||||
e := httpError{
|
||||
httpCore: httpCore{
|
||||
e := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusBadRequest,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
|
||||
Reference in New Issue
Block a user