mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
Major refactoring of the code base into Backend structure. Increase version number to Alpha 6 to prepare for the upcoming release.
This commit is contained in:
@@ -58,40 +58,40 @@ func Start(Backend *core.Backend, ListenAddresses []string, UseSSL bool, Certifi
|
||||
}
|
||||
|
||||
api.Router.HandleFunc("/test", apiTest).Methods("GET")
|
||||
api.Router.HandleFunc("/status", apiStatus).Methods("GET")
|
||||
api.Router.HandleFunc("/account/info", apiAccountInfo).Methods("GET")
|
||||
api.Router.HandleFunc("/account/delete", apiAccountDelete).Methods("GET")
|
||||
api.Router.HandleFunc("/blockchain/header", apiBlockchainHeaderFunc).Methods("GET")
|
||||
api.Router.HandleFunc("/blockchain/append", apiBlockchainAppend).Methods("POST")
|
||||
api.Router.HandleFunc("/blockchain/read", apiBlockchainRead).Methods("GET")
|
||||
api.Router.HandleFunc("/blockchain/file/add", apiBlockchainFileAdd).Methods("POST")
|
||||
api.Router.HandleFunc("/blockchain/file/list", apiBlockchainFileList).Methods("GET")
|
||||
api.Router.HandleFunc("/blockchain/file/delete", apiBlockchainFileDelete).Methods("POST")
|
||||
api.Router.HandleFunc("/blockchain/file/update", apiBlockchainFileUpdate).Methods("POST")
|
||||
api.Router.HandleFunc("/profile/list", apiProfileList).Methods("GET")
|
||||
api.Router.HandleFunc("/profile/read", apiProfileRead).Methods("GET")
|
||||
api.Router.HandleFunc("/profile/write", apiProfileWrite).Methods("POST")
|
||||
api.Router.HandleFunc("/profile/delete", apiProfileDelete).Methods("POST")
|
||||
api.Router.HandleFunc("/status", api.apiStatus).Methods("GET")
|
||||
api.Router.HandleFunc("/account/info", api.apiAccountInfo).Methods("GET")
|
||||
api.Router.HandleFunc("/account/delete", api.apiAccountDelete).Methods("GET")
|
||||
api.Router.HandleFunc("/blockchain/header", api.apiBlockchainHeaderFunc).Methods("GET")
|
||||
api.Router.HandleFunc("/blockchain/append", api.apiBlockchainAppend).Methods("POST")
|
||||
api.Router.HandleFunc("/blockchain/read", api.apiBlockchainRead).Methods("GET")
|
||||
api.Router.HandleFunc("/blockchain/file/add", api.apiBlockchainFileAdd).Methods("POST")
|
||||
api.Router.HandleFunc("/blockchain/file/list", api.apiBlockchainFileList).Methods("GET")
|
||||
api.Router.HandleFunc("/blockchain/file/delete", api.apiBlockchainFileDelete).Methods("POST")
|
||||
api.Router.HandleFunc("/blockchain/file/update", api.apiBlockchainFileUpdate).Methods("POST")
|
||||
api.Router.HandleFunc("/profile/list", api.apiProfileList).Methods("GET")
|
||||
api.Router.HandleFunc("/profile/read", api.apiProfileRead).Methods("GET")
|
||||
api.Router.HandleFunc("/profile/write", api.apiProfileWrite).Methods("POST")
|
||||
api.Router.HandleFunc("/profile/delete", api.apiProfileDelete).Methods("POST")
|
||||
api.Router.HandleFunc("/search", api.apiSearch).Methods("POST")
|
||||
api.Router.HandleFunc("/search/result", apiSearchResult).Methods("GET")
|
||||
api.Router.HandleFunc("/search/result/ws", apiSearchResultStream).Methods("GET")
|
||||
api.Router.HandleFunc("/search/statistic", apiSearchStatistic).Methods("GET")
|
||||
api.Router.HandleFunc("/search/terminate", apiSearchTerminate).Methods("GET")
|
||||
api.Router.HandleFunc("/search/result", api.apiSearchResult).Methods("GET")
|
||||
api.Router.HandleFunc("/search/result/ws", api.apiSearchResultStream).Methods("GET")
|
||||
api.Router.HandleFunc("/search/statistic", api.apiSearchStatistic).Methods("GET")
|
||||
api.Router.HandleFunc("/search/terminate", api.apiSearchTerminate).Methods("GET")
|
||||
api.Router.HandleFunc("/explore", api.apiExplore).Methods("GET")
|
||||
api.Router.HandleFunc("/file/format", apiFileFormat).Methods("GET")
|
||||
api.Router.HandleFunc("/download/start", apiDownloadStart).Methods("GET")
|
||||
api.Router.HandleFunc("/download/status", apiDownloadStatus).Methods("GET")
|
||||
api.Router.HandleFunc("/download/action", apiDownloadAction).Methods("GET")
|
||||
api.Router.HandleFunc("/warehouse/create", apiWarehouseCreateFile).Methods("POST")
|
||||
api.Router.HandleFunc("/warehouse/create/path", apiWarehouseCreateFilePath).Methods("GET")
|
||||
api.Router.HandleFunc("/warehouse/read", apiWarehouseReadFile).Methods("GET")
|
||||
api.Router.HandleFunc("/warehouse/read/path", apiWarehouseReadFilePath).Methods("GET")
|
||||
api.Router.HandleFunc("/warehouse/delete", apiWarehouseDeleteFile).Methods("GET")
|
||||
api.Router.HandleFunc("/file/read", apiFileRead).Methods("GET")
|
||||
api.Router.HandleFunc("/file/view", apiFileView).Methods("GET")
|
||||
api.Router.HandleFunc("/file/format", api.apiFileFormat).Methods("GET")
|
||||
api.Router.HandleFunc("/download/start", api.apiDownloadStart).Methods("GET")
|
||||
api.Router.HandleFunc("/download/status", api.apiDownloadStatus).Methods("GET")
|
||||
api.Router.HandleFunc("/download/action", api.apiDownloadAction).Methods("GET")
|
||||
api.Router.HandleFunc("/warehouse/create", api.apiWarehouseCreateFile).Methods("POST")
|
||||
api.Router.HandleFunc("/warehouse/create/path", api.apiWarehouseCreateFilePath).Methods("GET")
|
||||
api.Router.HandleFunc("/warehouse/read", api.apiWarehouseReadFile).Methods("GET")
|
||||
api.Router.HandleFunc("/warehouse/read/path", api.apiWarehouseReadFilePath).Methods("GET")
|
||||
api.Router.HandleFunc("/warehouse/delete", api.apiWarehouseDeleteFile).Methods("GET")
|
||||
api.Router.HandleFunc("/file/read", api.apiFileRead).Methods("GET")
|
||||
api.Router.HandleFunc("/file/view", api.apiFileView).Methods("GET")
|
||||
|
||||
for _, listen := range ListenAddresses {
|
||||
go startWebAPI(listen, UseSSL, CertificateFile, CertificateKey, api.Router, "API", TimeoutRead, TimeoutWrite)
|
||||
go startWebAPI(Backend, listen, UseSSL, CertificateFile, CertificateKey, api.Router, "API", TimeoutRead, TimeoutWrite)
|
||||
}
|
||||
|
||||
return api
|
||||
@@ -99,8 +99,8 @@ func Start(Backend *core.Backend, ListenAddresses []string, UseSSL bool, Certifi
|
||||
|
||||
// startWebAPI starts a web-server with given parameters and logs the status. If may block forever and only returns if there is an error.
|
||||
// The certificate file and key are only used if SSL is enabled. The read and write timeout may be 0 for no timeout.
|
||||
func startWebAPI(WebListen string, UseSSL bool, CertificateFile, CertificateKey string, Handler http.Handler, Info string, ReadTimeout, WriteTimeout time.Duration) {
|
||||
core.Filters.LogError("startWebAPI", "Start API at '%s'\n", WebListen)
|
||||
func startWebAPI(Backend *core.Backend, WebListen string, UseSSL bool, CertificateFile, CertificateKey string, Handler http.Handler, Info string, ReadTimeout, WriteTimeout time.Duration) {
|
||||
Backend.Filters.LogError("startWebAPI", "Start API at '%s'\n", WebListen)
|
||||
|
||||
tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12} // for security reasons disable TLS 1.0/1.1
|
||||
|
||||
@@ -116,23 +116,23 @@ func startWebAPI(WebListen string, UseSSL bool, CertificateFile, CertificateKey
|
||||
if UseSSL {
|
||||
// HTTPS
|
||||
if err := server.ListenAndServeTLS(CertificateFile, CertificateKey); err != nil {
|
||||
core.Filters.LogError("startWebAPI", "Error listening on '%s': %v\n", WebListen, err)
|
||||
Backend.Filters.LogError("startWebAPI", "Error listening on '%s': %v\n", WebListen, err)
|
||||
}
|
||||
} else {
|
||||
// HTTP
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
core.Filters.LogError("startWebAPI", "Error listening on '%s': %v\n", WebListen, err)
|
||||
Backend.Filters.LogError("startWebAPI", "Error listening on '%s': %v\n", WebListen, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EncodeJSON encodes the data as JSON
|
||||
func EncodeJSON(w http.ResponseWriter, r *http.Request, data interface{}) (err error) {
|
||||
func EncodeJSON(Backend *core.Backend, w http.ResponseWriter, r *http.Request, data interface{}) (err error) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
err = json.NewEncoder(w).Encode(data)
|
||||
if err != nil {
|
||||
core.Filters.LogError("EncodeJSON", "Error writing data for route '%s': %v\n", r.URL.Path, err)
|
||||
Backend.Filters.LogError("EncodeJSON", "Error writing data for route '%s': %v\n", r.URL.Path, err)
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/blockchain"
|
||||
)
|
||||
|
||||
@@ -27,10 +26,10 @@ apiBlockchainHeaderFunc returns the current blockchain header information
|
||||
Request: GET /blockchain/header
|
||||
Result: 200 with JSON structure apiResponsePeerSelf
|
||||
*/
|
||||
func apiBlockchainHeaderFunc(w http.ResponseWriter, r *http.Request) {
|
||||
publicKey, height, version := core.UserBlockchain.Header()
|
||||
func (api *WebapiInstance) apiBlockchainHeaderFunc(w http.ResponseWriter, r *http.Request) {
|
||||
publicKey, height, version := api.backend.UserBlockchain.Header()
|
||||
|
||||
EncodeJSON(w, r, apiBlockchainHeader{Version: version, Height: height, PeerID: hex.EncodeToString(publicKey.SerializeCompressed())})
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainHeader{Version: version, Height: height, PeerID: hex.EncodeToString(publicKey.SerializeCompressed())})
|
||||
}
|
||||
|
||||
type apiBlockRecordRaw struct {
|
||||
@@ -56,7 +55,7 @@ Do not use this function. Adding invalid data to the blockchain may corrupt it w
|
||||
Request: POST /blockchain/append with JSON structure apiBlockchainBlockRaw
|
||||
Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
*/
|
||||
func apiBlockchainAppend(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiBlockchainAppend(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiBlockchainBlockRaw
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
@@ -68,9 +67,9 @@ func apiBlockchainAppend(w http.ResponseWriter, r *http.Request) {
|
||||
records = append(records, blockchain.BlockRecordRaw{Type: record.Type, Data: record.Data})
|
||||
}
|
||||
|
||||
newHeight, newVersion, status := core.UserBlockchain.Append(records)
|
||||
newHeight, newVersion, status := api.backend.UserBlockchain.Append(records)
|
||||
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
}
|
||||
|
||||
type apiBlockchainBlock struct {
|
||||
@@ -89,7 +88,7 @@ apiBlockchainRead reads a block and returns the decoded information.
|
||||
Request: GET /blockchain/read?block=[number]
|
||||
Result: 200 with JSON structure apiBlockchainBlock
|
||||
*/
|
||||
func apiBlockchainRead(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiBlockchainRead(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
blockN, err := strconv.Atoi(r.Form.Get("block"))
|
||||
if err != nil || blockN < 0 {
|
||||
@@ -97,7 +96,7 @@ func apiBlockchainRead(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
block, status, _ := core.UserBlockchain.Read(uint64(blockN))
|
||||
block, status, _ := api.backend.UserBlockchain.Read(uint64(blockN))
|
||||
result := apiBlockchainBlock{Status: status}
|
||||
|
||||
if status == 0 {
|
||||
@@ -119,5 +118,5 @@ func apiBlockchainRead(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, result)
|
||||
EncodeJSON(api.backend, w, r, result)
|
||||
}
|
||||
|
||||
@@ -13,20 +13,19 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/warehouse"
|
||||
)
|
||||
|
||||
// Starts the download.
|
||||
func (info *downloadInfo) Start() {
|
||||
// current user?
|
||||
if bytes.Equal(info.nodeID, core.SelfNodeID()) {
|
||||
if bytes.Equal(info.nodeID, info.backend.SelfNodeID()) {
|
||||
info.DownloadSelf()
|
||||
return
|
||||
}
|
||||
|
||||
for n := 0; n < 3 && info.peer == nil; n++ {
|
||||
_, info.peer, _ = core.FindNode(info.nodeID, time.Second*5)
|
||||
_, info.peer, _ = info.backend.FindNode(info.nodeID, time.Second*5)
|
||||
|
||||
if info.status == DownloadCanceled {
|
||||
return
|
||||
@@ -179,7 +178,7 @@ func (info *downloadInfo) storeDownloadData(data []byte, offset uint64) (status
|
||||
|
||||
func (info *downloadInfo) DownloadSelf() {
|
||||
// Check if the file is available in the local warehouse.
|
||||
_, fileInfo, status, _ := core.UserWarehouse.FileExists(info.hash)
|
||||
_, fileInfo, status, _ := info.backend.UserWarehouse.FileExists(info.hash)
|
||||
if status != warehouse.StatusOK {
|
||||
info.status = DownloadCanceled
|
||||
return
|
||||
@@ -189,7 +188,7 @@ func (info *downloadInfo) DownloadSelf() {
|
||||
info.status = DownloadActive
|
||||
|
||||
// read the file
|
||||
status, bytesRead, _ := core.UserWarehouse.ReadFile(info.hash, 0, int64(fileInfo.Size()), info.DiskFile.Handle)
|
||||
status, bytesRead, _ := info.backend.UserWarehouse.ReadFile(info.hash, 0, int64(fileInfo.Size()), info.DiskFile.Handle)
|
||||
|
||||
info.DiskFile.StoredSize = uint64(bytesRead)
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ The hash parameter identifies the file to download. The node ID identifies the b
|
||||
Request: GET /download/start?path=[target path on disk]&hash=[file hash to download]&node=[node ID]
|
||||
Result: 200 with JSON structure apiResponseDownloadStatus
|
||||
*/
|
||||
func apiDownloadStart(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiDownloadStart(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
// validate hashes, must be blake3
|
||||
@@ -76,11 +76,11 @@ func apiDownloadStart(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
info := &downloadInfo{id: uuid.New(), created: time.Now(), hash: hash, nodeID: nodeID}
|
||||
info := &downloadInfo{backend: api.backend, id: uuid.New(), created: time.Now(), hash: hash, nodeID: nodeID}
|
||||
|
||||
// create the file immediately
|
||||
if info.initDiskFile(filePath) != nil {
|
||||
EncodeJSON(w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseFileInvalid})
|
||||
EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseFileInvalid})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ func apiDownloadStart(w http.ResponseWriter, r *http.Request) {
|
||||
// start the download!
|
||||
go info.Start()
|
||||
|
||||
EncodeJSON(w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseSuccess, ID: info.id, DownloadStatus: DownloadWaitMetadata})
|
||||
EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseSuccess, ID: info.id, DownloadStatus: DownloadWaitMetadata})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -99,7 +99,7 @@ apiDownloadStatus returns the status of an active download.
|
||||
Request: GET /download/status?id=[download ID]
|
||||
Result: 200 with JSON structure apiResponseDownloadStatus
|
||||
*/
|
||||
func apiDownloadStatus(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiDownloadStatus(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
id, err := uuid.Parse(r.Form.Get("id"))
|
||||
if err != nil {
|
||||
@@ -109,7 +109,7 @@ func apiDownloadStatus(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
info := downloadLookup(id)
|
||||
if info == nil {
|
||||
EncodeJSON(w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseIDNotFound})
|
||||
EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseIDNotFound})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ func apiDownloadStatus(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
info.RUnlock()
|
||||
|
||||
EncodeJSON(w, r, response)
|
||||
EncodeJSON(api.backend, w, r, response)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -143,7 +143,7 @@ Action: 0 = Pause, 1 = Resume, 2 = Cancel.
|
||||
Request: GET /download/action?id=[download ID]&action=[action]
|
||||
Result: 200 with JSON structure apiResponseDownloadStatus (using APIStatus and DownloadStatus)
|
||||
*/
|
||||
func apiDownloadAction(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiDownloadAction(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
id, err := uuid.Parse(r.Form.Get("id"))
|
||||
action, err2 := strconv.Atoi(r.Form.Get("action"))
|
||||
@@ -154,7 +154,7 @@ func apiDownloadAction(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
info := downloadLookup(id)
|
||||
if info == nil {
|
||||
EncodeJSON(w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseIDNotFound})
|
||||
EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseIDNotFound})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ func apiDownloadAction(w http.ResponseWriter, r *http.Request) {
|
||||
apiStatus = info.Cancel()
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, apiResponseDownloadStatus{APIStatus: apiStatus, ID: info.id, DownloadStatus: info.status})
|
||||
EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: apiStatus, ID: info.id, DownloadStatus: info.status})
|
||||
}
|
||||
|
||||
// ---- download tracking ----
|
||||
@@ -203,6 +203,8 @@ type downloadInfo struct {
|
||||
|
||||
// live connections, to be changed
|
||||
peer *core.PeerInfo
|
||||
|
||||
backend *core.Backend
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -203,7 +203,7 @@ It will primarily use the file extension for detection. If unavailable, it uses
|
||||
Request: GET /file/format?path=[file path on disk]
|
||||
Result: 200 with JSON structure apiResponseFileFormat
|
||||
*/
|
||||
func apiFileFormat(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiFileFormat(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
filePath := r.Form.Get("path")
|
||||
if filePath == "" {
|
||||
@@ -213,9 +213,9 @@ func apiFileFormat(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
fileType, fileFormat, err := FileDetectType(filePath)
|
||||
if err != nil {
|
||||
EncodeJSON(w, r, apiResponseFileFormat{Status: 1})
|
||||
EncodeJSON(api.backend, w, r, apiResponseFileFormat{Status: 1})
|
||||
return
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, apiResponseFileFormat{Status: 0, FileType: fileType, FileFormat: fileFormat})
|
||||
EncodeJSON(api.backend, w, r, apiResponseFileFormat{Status: 0, FileType: fileType, FileFormat: fileFormat})
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ Response: 200 with the content
|
||||
404 if the file was not found or other error on transfer initiate
|
||||
502 if unable to find or connect to the remote peer in time
|
||||
*/
|
||||
func apiFileRead(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiFileRead(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
var err error
|
||||
|
||||
@@ -69,7 +69,7 @@ func apiFileRead(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Is the file available in the local warehouse? In that case requesting it from the remote is unnecessary.
|
||||
if serveFileFromWarehouse(w, fileHash, uint64(offset), uint64(limit), ranges) {
|
||||
if serveFileFromWarehouse(api.backend, w, fileHash, uint64(offset), uint64(limit), ranges) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -77,9 +77,9 @@ func apiFileRead(w http.ResponseWriter, r *http.Request) {
|
||||
var peer *core.PeerInfo
|
||||
|
||||
if valid2 {
|
||||
peer, err = PeerConnectNode(nodeID, timeout)
|
||||
peer, err = PeerConnectNode(api.backend, nodeID, timeout)
|
||||
} else if err3 == nil {
|
||||
peer, err = PeerConnectPublicKey(publicKey, timeout)
|
||||
peer, err = PeerConnectPublicKey(api.backend, publicKey, timeout)
|
||||
}
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
@@ -105,9 +105,9 @@ func apiFileRead(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// serveFileFromWarehouse serves the file from the warehouse. If it is not available, it returns false and does not use the writer.
|
||||
// Limit is optional, 0 means the entire file.
|
||||
func serveFileFromWarehouse(w http.ResponseWriter, fileHash []byte, offset, limit uint64, ranges []HTTPRange) (valid bool) {
|
||||
func serveFileFromWarehouse(backend *core.Backend, w http.ResponseWriter, fileHash []byte, offset, limit uint64, ranges []HTTPRange) (valid bool) {
|
||||
// Check if the file is available in the local warehouse.
|
||||
_, fileInfo, status, _ := core.UserWarehouse.FileExists(fileHash)
|
||||
_, fileInfo, status, _ := backend.UserWarehouse.FileExists(fileHash)
|
||||
if status != warehouse.StatusOK {
|
||||
return false
|
||||
}
|
||||
@@ -126,7 +126,7 @@ func serveFileFromWarehouse(w http.ResponseWriter, fileHash []byte, offset, limi
|
||||
|
||||
setContentLengthRangeHeader(w, offset, limit, uint64(fileInfo.Size()), ranges)
|
||||
|
||||
status, _, _ = core.UserWarehouse.ReadFile(fileHash, int64(offset), int64(limit), w)
|
||||
status, _, _ = backend.UserWarehouse.ReadFile(fileHash, int64(offset), int64(limit), w)
|
||||
|
||||
// StatusErrorReadFile must be considered success, since parts of the file may have been transferred already and recovery is not possible.
|
||||
return status == warehouse.StatusErrorReadFile || status == warehouse.StatusOK
|
||||
@@ -148,7 +148,7 @@ Response: 200 with the content
|
||||
404 if the file was not found or other error on transfer initiate
|
||||
502 if unable to find or connect to the remote peer in time
|
||||
*/
|
||||
func apiFileView(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiFileView(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
var err error
|
||||
|
||||
@@ -194,7 +194,7 @@ func apiFileView(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Is the file available in the local warehouse? In that case requesting it from the remote is unnecessary.
|
||||
if !localCacheDisable {
|
||||
if serveFileFromWarehouse(w, fileHash, uint64(offset), uint64(limit), ranges) {
|
||||
if serveFileFromWarehouse(api.backend, w, fileHash, uint64(offset), uint64(limit), ranges) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -203,9 +203,9 @@ func apiFileView(w http.ResponseWriter, r *http.Request) {
|
||||
var peer *core.PeerInfo
|
||||
|
||||
if valid2 {
|
||||
peer, err = PeerConnectNode(nodeID, timeout)
|
||||
peer, err = PeerConnectNode(api.backend, nodeID, timeout)
|
||||
} else if err3 == nil {
|
||||
peer, err = PeerConnectPublicKey(publicKey, timeout)
|
||||
peer, err = PeerConnectPublicKey(api.backend, publicKey, timeout)
|
||||
}
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
@@ -230,19 +230,19 @@ func apiFileView(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// PeerConnectPublicKey attempts to connect to the peer specified by its public key (= peer ID).
|
||||
func PeerConnectPublicKey(publicKey *btcec.PublicKey, timeout time.Duration) (peer *core.PeerInfo, err error) {
|
||||
func PeerConnectPublicKey(backend *core.Backend, publicKey *btcec.PublicKey, timeout time.Duration) (peer *core.PeerInfo, err error) {
|
||||
if publicKey == nil {
|
||||
return nil, errors.New("invalid public key")
|
||||
}
|
||||
|
||||
// First look up in the peer list.
|
||||
if peer = core.PeerlistLookup(publicKey); peer != nil {
|
||||
if peer = backend.PeerlistLookup(publicKey); peer != nil {
|
||||
return peer, nil
|
||||
}
|
||||
|
||||
// Try to connect via DHT.
|
||||
nodeID := protocol.PublicKey2NodeID(publicKey)
|
||||
if _, peer, _ = core.FindNode(nodeID, timeout); peer != nil {
|
||||
if _, peer, _ = backend.FindNode(nodeID, timeout); peer != nil {
|
||||
return peer, nil
|
||||
}
|
||||
|
||||
@@ -251,13 +251,13 @@ func PeerConnectPublicKey(publicKey *btcec.PublicKey, timeout time.Duration) (pe
|
||||
}
|
||||
|
||||
// PeerConnectNode tries to connect via the node ID
|
||||
func PeerConnectNode(nodeID []byte, timeout time.Duration) (peer *core.PeerInfo, err error) {
|
||||
func PeerConnectNode(backend *core.Backend, nodeID []byte, timeout time.Duration) (peer *core.PeerInfo, err error) {
|
||||
if len(nodeID) == 256/8 {
|
||||
return nil, errors.New("invalid node ID")
|
||||
}
|
||||
|
||||
// Try to connect via DHT.
|
||||
if _, peer, _ = core.FindNode(nodeID, timeout); peer != nil {
|
||||
if _, peer, _ = backend.FindNode(nodeID, timeout); peer != nil {
|
||||
return peer, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -132,7 +132,7 @@ Request: POST /blockchain/file/add with JSON structure apiBlockAddFiles
|
||||
Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
400 if invalid input
|
||||
*/
|
||||
func apiBlockchainFileAdd(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiBlockchainFileAdd(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiBlockAddFiles
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
@@ -154,8 +154,8 @@ func apiBlockchainFileAdd(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := warehouse.ValidateHash(file.Hash); err != nil {
|
||||
http.Error(w, "", http.StatusBadRequest)
|
||||
return
|
||||
} else if _, fileInfo, status, _ := core.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
|
||||
} else if _, fileInfo, status, _ := api.backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
|
||||
return
|
||||
} else {
|
||||
file.Size = uint64(fileInfo.Size())
|
||||
@@ -168,17 +168,17 @@ func apiBlockchainFileAdd(w http.ResponseWriter, r *http.Request) {
|
||||
blockRecord := blockRecordFileFromAPI(file)
|
||||
|
||||
// Set the merkle tree info as appropriate.
|
||||
if !setFileMerkleInfo(&blockRecord) {
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
|
||||
if !setFileMerkleInfo(api.backend, &blockRecord) {
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
|
||||
return
|
||||
}
|
||||
|
||||
filesAdd = append(filesAdd, blockRecord)
|
||||
}
|
||||
|
||||
newHeight, newVersion, status := core.UserBlockchain.AddFiles(filesAdd)
|
||||
newHeight, newVersion, status := api.backend.UserBlockchain.AddFiles(filesAdd)
|
||||
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -187,8 +187,8 @@ apiBlockchainFileList lists all files stored on the blockchain.
|
||||
Request: GET /blockchain/file/list
|
||||
Response: 200 with JSON structure apiBlockAddFiles
|
||||
*/
|
||||
func apiBlockchainFileList(w http.ResponseWriter, r *http.Request) {
|
||||
files, status := core.UserBlockchain.ListFiles()
|
||||
func (api *WebapiInstance) apiBlockchainFileList(w http.ResponseWriter, r *http.Request) {
|
||||
files, status := api.backend.UserBlockchain.ListFiles()
|
||||
|
||||
var result apiBlockAddFiles
|
||||
|
||||
@@ -198,7 +198,7 @@ func apiBlockchainFileList(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
result.Status = status
|
||||
|
||||
EncodeJSON(w, r, result)
|
||||
EncodeJSON(api.backend, w, r, result)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -208,7 +208,7 @@ It will automatically delete the file in the Warehouse if there are no other ref
|
||||
Request: POST /blockchain/file/delete with JSON structure apiBlockAddFiles
|
||||
Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
*/
|
||||
func apiBlockchainFileDelete(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiBlockchainFileDelete(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiBlockAddFiles
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
@@ -220,18 +220,18 @@ func apiBlockchainFileDelete(w http.ResponseWriter, r *http.Request) {
|
||||
deleteIDs = append(deleteIDs, input.Files[n].ID)
|
||||
}
|
||||
|
||||
newHeight, newVersion, deletedFiles, status := core.UserBlockchain.DeleteFiles(deleteIDs)
|
||||
newHeight, newVersion, deletedFiles, status := api.backend.UserBlockchain.DeleteFiles(deleteIDs)
|
||||
|
||||
// If successfully deleted from the blockchain, delete from the Warehouse in case there are no other references.
|
||||
if status == blockchain.StatusOK {
|
||||
for n := range deletedFiles {
|
||||
if files, status := core.UserBlockchain.FileExists(deletedFiles[n].Hash); status == blockchain.StatusOK && len(files) == 0 {
|
||||
core.UserWarehouse.DeleteFile(deletedFiles[n].Hash)
|
||||
if files, status := api.backend.UserBlockchain.FileExists(deletedFiles[n].Hash); status == blockchain.StatusOK && len(files) == 0 {
|
||||
api.backend.UserWarehouse.DeleteFile(deletedFiles[n].Hash)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -241,7 +241,7 @@ Request: POST /blockchain/file/update with JSON structure apiBlockAddFiles
|
||||
Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
400 if invalid input
|
||||
*/
|
||||
func apiBlockchainFileUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiBlockchainFileUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiBlockAddFiles
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
@@ -263,8 +263,8 @@ func apiBlockchainFileUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := warehouse.ValidateHash(file.Hash); err != nil {
|
||||
http.Error(w, "", http.StatusBadRequest)
|
||||
return
|
||||
} else if _, fileInfo, status, _ := core.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
|
||||
} else if _, fileInfo, status, _ := api.backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
|
||||
return
|
||||
} else {
|
||||
file.Size = uint64(fileInfo.Size())
|
||||
@@ -277,17 +277,17 @@ func apiBlockchainFileUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
blockRecord := blockRecordFileFromAPI(file)
|
||||
|
||||
// Set the merkle tree info as appropriate.
|
||||
if !setFileMerkleInfo(&blockRecord) {
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
|
||||
if !setFileMerkleInfo(api.backend, &blockRecord) {
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
|
||||
return
|
||||
}
|
||||
|
||||
filesAdd = append(filesAdd, blockRecord)
|
||||
}
|
||||
|
||||
newHeight, newVersion, status := core.UserBlockchain.ReplaceFiles(filesAdd)
|
||||
newHeight, newVersion, status := api.backend.UserBlockchain.ReplaceFiles(filesAdd)
|
||||
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
}
|
||||
|
||||
// ---- metadata functions ----
|
||||
@@ -318,14 +318,14 @@ func (file *apiFile) IsVirtualFolder() bool {
|
||||
}
|
||||
|
||||
// setFileMerkleInfo sets the merkle fields in the BlockRecordFile
|
||||
func setFileMerkleInfo(file *blockchain.BlockRecordFile) (valid bool) {
|
||||
func setFileMerkleInfo(backend *core.Backend, file *blockchain.BlockRecordFile) (valid bool) {
|
||||
if file.Size <= merkle.MinimumFragmentSize {
|
||||
// If smaller or equal than the minimum fragment size, the merkle tree is not used.
|
||||
file.MerkleRootHash = file.Hash
|
||||
file.FragmentSize = merkle.MinimumFragmentSize
|
||||
} else {
|
||||
// Get the information from the Warehouse .merkle companion file.
|
||||
tree, status, _ := core.UserWarehouse.ReadMerkleTree(file.Hash, true)
|
||||
tree, status, _ := backend.UserWarehouse.ReadMerkleTree(file.Hash, true)
|
||||
if status != warehouse.StatusOK {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/blockchain"
|
||||
)
|
||||
|
||||
@@ -35,15 +34,15 @@ apiProfileList lists all users profile fields.
|
||||
Request: GET /profile/list
|
||||
Response: 200 with JSON structure apiProfileData
|
||||
*/
|
||||
func apiProfileList(w http.ResponseWriter, r *http.Request) {
|
||||
fields, status := core.UserBlockchain.ProfileList()
|
||||
func (api *WebapiInstance) apiProfileList(w http.ResponseWriter, r *http.Request) {
|
||||
fields, status := api.backend.UserBlockchain.ProfileList()
|
||||
|
||||
result := apiProfileData{Status: status}
|
||||
for n := range fields {
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(fields[n]))
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, result)
|
||||
EncodeJSON(api.backend, w, r, result)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -52,7 +51,7 @@ apiProfileRead reads a specific users profile field. See core.ProfileX for recog
|
||||
Request: GET /profile/read?field=[index]
|
||||
Response: 200 with JSON structure apiProfileData
|
||||
*/
|
||||
func apiProfileRead(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiProfileRead(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
fieldN, err1 := strconv.Atoi(r.Form.Get("field"))
|
||||
|
||||
@@ -64,11 +63,11 @@ func apiProfileRead(w http.ResponseWriter, r *http.Request) {
|
||||
var result apiProfileData
|
||||
|
||||
var data []byte
|
||||
if data, result.Status = core.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.StatusOK {
|
||||
if data, result.Status = api.backend.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.StatusOK {
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: uint16(fieldN), Data: data}))
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, result)
|
||||
EncodeJSON(api.backend, w, r, result)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -77,7 +76,7 @@ apiProfileWrite writes profile fields. See core.ProfileX for recognized fields.
|
||||
Request: POST /profile/write with JSON structure apiProfileData
|
||||
Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
*/
|
||||
func apiProfileWrite(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiProfileWrite(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiProfileData
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
@@ -89,9 +88,9 @@ func apiProfileWrite(w http.ResponseWriter, r *http.Request) {
|
||||
fields = append(fields, blockRecordProfileFromAPI(input.Fields[n]))
|
||||
}
|
||||
|
||||
newHeight, newVersion, status := core.UserBlockchain.ProfileWrite(fields)
|
||||
newHeight, newVersion, status := api.backend.UserBlockchain.ProfileWrite(fields)
|
||||
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -100,7 +99,7 @@ apiProfileDelete deletes profile fields identified by the types. See core.Profil
|
||||
Request: POST /profile/delete with JSON structure apiProfileData
|
||||
Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
*/
|
||||
func apiProfileDelete(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiProfileDelete(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiProfileData
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
@@ -112,9 +111,9 @@ func apiProfileDelete(w http.ResponseWriter, r *http.Request) {
|
||||
fields = append(fields, input.Fields[n].Type)
|
||||
}
|
||||
|
||||
newHeight, newVersion, status := core.UserBlockchain.ProfileDelete(fields)
|
||||
newHeight, newVersion, status := api.backend.UserBlockchain.ProfileDelete(fields)
|
||||
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
}
|
||||
|
||||
// --- conversion from core to API data ---
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/blockchain"
|
||||
)
|
||||
|
||||
@@ -20,7 +19,7 @@ func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob)
|
||||
Filter := input.ToSearchFilter()
|
||||
|
||||
// create the search job
|
||||
job = CreateSearchJob(Timeout, input.MaxResults, Filter)
|
||||
job = CreateSearchJob(api.backend, Timeout, input.MaxResults, Filter)
|
||||
|
||||
// todo: create actual search clients!
|
||||
job.Status = SearchStatusLive
|
||||
@@ -56,10 +55,10 @@ resultLoop:
|
||||
}
|
||||
}
|
||||
|
||||
if bytes.Equal(file.NodeID, core.SelfNodeID()) {
|
||||
if bytes.Equal(file.NodeID, job.backend.SelfNodeID()) {
|
||||
// Indicates data from the current user.
|
||||
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
|
||||
} else if peer := core.NodelistLookup(file.NodeID); peer != nil {
|
||||
} else if peer := job.backend.NodelistLookup(file.NodeID); peer != nil {
|
||||
// add the tags 'Shared By Count' and 'Shared By GeoIP'
|
||||
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
|
||||
if latitude, longitude, valid := api.Peer2GeoIP(peer); valid {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/blockchain"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -29,6 +30,8 @@ type SearchFilter struct {
|
||||
|
||||
// SearchJob is a collection of search jobs
|
||||
type SearchJob struct {
|
||||
backend *core.Backend
|
||||
|
||||
// input settings
|
||||
id uuid.UUID // The job id
|
||||
timeout time.Duration // timeout set for all searches
|
||||
@@ -79,8 +82,8 @@ const (
|
||||
|
||||
// CreateSearchJob creates a new search job and adds it to the lookup list.
|
||||
// Timeout and MaxResults must be set and must not be 0.
|
||||
func CreateSearchJob(Timeout time.Duration, MaxResults int, Filter SearchFilter) (job *SearchJob) {
|
||||
job = &SearchJob{}
|
||||
func CreateSearchJob(Backend *core.Backend, Timeout time.Duration, MaxResults int, Filter SearchFilter) (job *SearchJob) {
|
||||
job = &SearchJob{backend: Backend}
|
||||
job.Status = SearchStatusNotStarted
|
||||
job.id = uuid.New()
|
||||
job.timeout = Timeout
|
||||
|
||||
@@ -106,7 +106,7 @@ func (api *WebapiInstance) apiSearch(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
job := api.dispatchSearch(input)
|
||||
|
||||
EncodeJSON(w, r, SearchRequestResponse{Status: 0, ID: job.id})
|
||||
EncodeJSON(api.backend, w, r, SearchRequestResponse{Status: 0, ID: job.id})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -124,7 +124,7 @@ Optional parameters:
|
||||
&sort=[sort order]
|
||||
Result: 200 with JSON structure SearchResult. Check the field status.
|
||||
*/
|
||||
func apiSearchResult(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiSearchResult(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
jobID, err := uuid.Parse(r.Form.Get("id"))
|
||||
if err != nil {
|
||||
@@ -139,7 +139,7 @@ func apiSearchResult(w http.ResponseWriter, r *http.Request) {
|
||||
// find the job ID
|
||||
job := JobLookup(jobID)
|
||||
if job == nil {
|
||||
EncodeJSON(w, r, SearchResult{Status: 2})
|
||||
EncodeJSON(api.backend, w, r, SearchResult{Status: 2})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -194,7 +194,7 @@ func apiSearchResult(w http.ResponseWriter, r *http.Request) {
|
||||
result.Statistic = job.Statistics()
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, result)
|
||||
EncodeJSON(api.backend, w, r, result)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -204,7 +204,7 @@ Request: GET /search/result/ws?id=[UUID]&limit=[optional max records]
|
||||
Result: If successful, upgrades to a websocket and sends JSON structure SearchResult messages.
|
||||
Limit is optional. Not used if ommitted or 0.
|
||||
*/
|
||||
func apiSearchResultStream(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiSearchResultStream(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
jobID, err := uuid.Parse(r.Form.Get("id"))
|
||||
if err != nil {
|
||||
@@ -217,7 +217,7 @@ func apiSearchResultStream(w http.ResponseWriter, r *http.Request) {
|
||||
// look up the job
|
||||
job := JobLookup(jobID)
|
||||
if job == nil {
|
||||
EncodeJSON(w, r, SearchResult{Status: 2})
|
||||
EncodeJSON(api.backend, w, r, SearchResult{Status: 2})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -289,7 +289,7 @@ Response: 204 Empty
|
||||
400 Invalid input
|
||||
404 ID not found
|
||||
*/
|
||||
func apiSearchTerminate(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiSearchTerminate(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
r.ParseForm()
|
||||
jobID, err := uuid.Parse(r.Form.Get("id"))
|
||||
@@ -318,7 +318,7 @@ apiSearchStatistic returns search result statistics. Statistics are always calcu
|
||||
Request: GET /search/result?id=[UUID]
|
||||
Result: 200 with JSON structure SearchStatistic. Check the field status (0 = Success, 2 = ID not found).
|
||||
*/
|
||||
func apiSearchStatistic(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiSearchStatistic(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
jobID, err := uuid.Parse(r.Form.Get("id"))
|
||||
if err != nil {
|
||||
@@ -329,13 +329,13 @@ func apiSearchStatistic(w http.ResponseWriter, r *http.Request) {
|
||||
// find the job ID
|
||||
job := JobLookup(jobID)
|
||||
if job == nil {
|
||||
EncodeJSON(w, r, SearchStatistic{Status: 2})
|
||||
EncodeJSON(api.backend, w, r, SearchStatistic{Status: 2})
|
||||
return
|
||||
}
|
||||
|
||||
stats := job.Statistics()
|
||||
|
||||
EncodeJSON(w, r, SearchStatistic{SearchStatisticData: stats, Status: 0, IsTerminated: job.IsTerminated()})
|
||||
EncodeJSON(api.backend, w, r, SearchStatistic{SearchStatisticData: stats, Status: 0, IsTerminated: job.IsTerminated()})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -372,7 +372,7 @@ func (api *WebapiInstance) apiExplore(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
result.Status = 1 // No more results to expect
|
||||
|
||||
EncodeJSON(w, r, result)
|
||||
EncodeJSON(api.backend, w, r, result)
|
||||
}
|
||||
|
||||
func (input *SearchRequest) Parse() (Timeout time.Duration) {
|
||||
|
||||
@@ -19,7 +19,7 @@ func (api *WebapiInstance) queryRecentShared(backend *core.Backend, fileType int
|
||||
}
|
||||
|
||||
// Use the peer list to know about active peers. Random order!
|
||||
peerList := core.PeerlistGet()
|
||||
peerList := api.backend.PeerlistGet()
|
||||
|
||||
// Files from peers exceeding the limit. It is used if from all peers the total limit is not reached.
|
||||
var filesSeconday []blockchain.BlockRecordFile
|
||||
|
||||
@@ -10,8 +10,6 @@ import (
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
)
|
||||
|
||||
func apiTest(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -33,8 +31,8 @@ apiStatus returns the current connectivity status to the network
|
||||
Request: GET /status
|
||||
Result: 200 with JSON structure Status
|
||||
*/
|
||||
func apiStatus(w http.ResponseWriter, r *http.Request) {
|
||||
status := apiResponseStatus{Status: 0, CountPeerList: core.PeerlistCount()}
|
||||
func (api *WebapiInstance) apiStatus(w http.ResponseWriter, r *http.Request) {
|
||||
status := apiResponseStatus{Status: 0, CountPeerList: api.backend.PeerlistCount()}
|
||||
status.CountNetwork = status.CountPeerList // For now always same as CountPeerList, until native Statistics message to root peers is available.
|
||||
|
||||
// Connected: If at leat 2 peers.
|
||||
@@ -42,7 +40,7 @@ func apiStatus(w http.ResponseWriter, r *http.Request) {
|
||||
// Instead, the core should keep a count of "active peers".
|
||||
status.IsConnected = status.CountPeerList >= 2
|
||||
|
||||
EncodeJSON(w, r, status)
|
||||
EncodeJSON(api.backend, w, r, status)
|
||||
}
|
||||
|
||||
type apiResponsePeerSelf struct {
|
||||
@@ -55,14 +53,14 @@ apiAccountInfo provides information about the current account.
|
||||
Request: GET /account/info
|
||||
Result: 200 with JSON structure apiResponsePeerSelf
|
||||
*/
|
||||
func apiAccountInfo(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiAccountInfo(w http.ResponseWriter, r *http.Request) {
|
||||
response := apiResponsePeerSelf{}
|
||||
response.NodeID = hex.EncodeToString(core.SelfNodeID())
|
||||
response.NodeID = hex.EncodeToString(api.backend.SelfNodeID())
|
||||
|
||||
_, publicKey := core.ExportPrivateKey()
|
||||
_, publicKey := api.backend.ExportPrivateKey()
|
||||
response.PeerID = hex.EncodeToString(publicKey.SerializeCompressed())
|
||||
|
||||
EncodeJSON(w, r, response)
|
||||
EncodeJSON(api.backend, w, r, response)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -71,14 +69,14 @@ Request: GET /account/delete?confirm=[0 or 1]
|
||||
Result: 204 if the user choses not to delete the account
|
||||
200 if successfully deleted
|
||||
*/
|
||||
func apiAccountDelete(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiAccountDelete(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
if confirm, _ := strconv.ParseBool(r.Form.Get("confirm")); !confirm {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
core.DeleteAccount()
|
||||
api.backend.DeleteAccount()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/warehouse"
|
||||
)
|
||||
|
||||
@@ -26,14 +25,14 @@ apiWarehouseCreateFile creates a file in the warehouse.
|
||||
Request: POST /warehouse/create with raw data to create as new file
|
||||
Response: 200 with JSON structure WarehouseResult
|
||||
*/
|
||||
func apiWarehouseCreateFile(w http.ResponseWriter, r *http.Request) {
|
||||
hash, status, err := core.UserWarehouse.CreateFile(r.Body, 0)
|
||||
func (api *WebapiInstance) apiWarehouseCreateFile(w http.ResponseWriter, r *http.Request) {
|
||||
hash, status, err := api.backend.UserWarehouse.CreateFile(r.Body, 0)
|
||||
|
||||
if err != nil {
|
||||
core.Filters.LogError("warehouse.CreateFile", "status %d error: %v", status, err)
|
||||
api.backend.Filters.LogError("warehouse.CreateFile", "status %d error: %v", status, err)
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, WarehouseResult{Status: status, Hash: hash})
|
||||
EncodeJSON(api.backend, w, r, WarehouseResult{Status: status, Hash: hash})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -44,7 +43,7 @@ In the future the API should be secured using a random API key and setting the C
|
||||
Request: GET /warehouse/create/path?path=[target path on disk]
|
||||
Response: 200 with JSON structure WarehouseResult
|
||||
*/
|
||||
func apiWarehouseCreateFilePath(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiWarehouseCreateFilePath(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
filePath := r.Form.Get("path")
|
||||
if filePath == "" {
|
||||
@@ -52,13 +51,13 @@ func apiWarehouseCreateFilePath(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
hash, status, err := core.UserWarehouse.CreateFileFromPath(filePath)
|
||||
hash, status, err := api.backend.UserWarehouse.CreateFileFromPath(filePath)
|
||||
|
||||
if err != nil {
|
||||
core.Filters.LogError("warehouse.CreateFile", "status %d error: %v", status, err)
|
||||
api.backend.Filters.LogError("warehouse.CreateFile", "status %d error: %v", status, err)
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, WarehouseResult{Status: status, Hash: hash})
|
||||
EncodeJSON(api.backend, w, r, WarehouseResult{Status: status, Hash: hash})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -70,7 +69,7 @@ Response: 200 with the raw file data
|
||||
404 if file was not found
|
||||
500 in case of internal error opening the file
|
||||
*/
|
||||
func apiWarehouseReadFile(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiWarehouseReadFile(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
hash, valid1 := DecodeBlake3Hash(r.Form.Get("hash"))
|
||||
if !valid1 {
|
||||
@@ -81,7 +80,7 @@ func apiWarehouseReadFile(w http.ResponseWriter, r *http.Request) {
|
||||
offset, _ := strconv.Atoi(r.Form.Get("offset"))
|
||||
limit, _ := strconv.Atoi(r.Form.Get("limit"))
|
||||
|
||||
status, bytesRead, err := core.UserWarehouse.ReadFile(hash, int64(offset), int64(limit), w)
|
||||
status, bytesRead, err := api.backend.UserWarehouse.ReadFile(hash, int64(offset), int64(limit), w)
|
||||
|
||||
switch status {
|
||||
case warehouse.StatusFileNotFound:
|
||||
@@ -95,7 +94,7 @@ func apiWarehouseReadFile(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
core.Filters.LogError("warehouse.ReadFile", "status %d read %d error: %v", status, bytesRead, err)
|
||||
api.backend.Filters.LogError("warehouse.ReadFile", "status %d read %d error: %v", status, bytesRead, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +104,7 @@ apiWarehouseDeleteFile deletes a file in the warehouse.
|
||||
Request: GET /warehouse/delete?hash=[hash]
|
||||
Response: 200 with JSON structure WarehouseResult
|
||||
*/
|
||||
func apiWarehouseDeleteFile(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiWarehouseDeleteFile(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
hash, valid1 := DecodeBlake3Hash(r.Form.Get("hash"))
|
||||
if !valid1 {
|
||||
@@ -113,13 +112,13 @@ func apiWarehouseDeleteFile(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
status, err := core.UserWarehouse.DeleteFile(hash)
|
||||
status, err := api.backend.UserWarehouse.DeleteFile(hash)
|
||||
|
||||
if err != nil {
|
||||
core.Filters.LogError("warehouse.DeleteFile", "status %d error: %v", status, err)
|
||||
api.backend.Filters.LogError("warehouse.DeleteFile", "status %d error: %v", status, err)
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, WarehouseResult{Status: status, Hash: hash})
|
||||
EncodeJSON(api.backend, w, r, WarehouseResult{Status: status, Hash: hash})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -130,7 +129,7 @@ Request: GET /warehouse/read/path?hash=[hash]&path=[target path on disk]
|
||||
Optional parameters &offset=[file offset]&limit=[read limit in bytes]
|
||||
Response: 200 with JSON structure WarehouseResult
|
||||
*/
|
||||
func apiWarehouseReadFilePath(w http.ResponseWriter, r *http.Request) {
|
||||
func (api *WebapiInstance) apiWarehouseReadFilePath(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
hash, valid1 := DecodeBlake3Hash(r.Form.Get("hash"))
|
||||
if !valid1 {
|
||||
@@ -142,11 +141,11 @@ func apiWarehouseReadFilePath(w http.ResponseWriter, r *http.Request) {
|
||||
offset, _ := strconv.Atoi(r.Form.Get("offset"))
|
||||
limit, _ := strconv.Atoi(r.Form.Get("limit"))
|
||||
|
||||
status, bytesRead, err := core.UserWarehouse.ReadFileToDisk(hash, int64(offset), int64(limit), targetFile)
|
||||
status, bytesRead, err := api.backend.UserWarehouse.ReadFileToDisk(hash, int64(offset), int64(limit), targetFile)
|
||||
|
||||
if err != nil {
|
||||
core.Filters.LogError("warehouse.ReadFileToDisk", "status %d read %d error: %v", status, bytesRead, err)
|
||||
api.backend.Filters.LogError("warehouse.ReadFileToDisk", "status %d read %d error: %v", status, bytesRead, err)
|
||||
}
|
||||
|
||||
EncodeJSON(w, r, WarehouseResult{Status: status, Hash: hash})
|
||||
EncodeJSON(api.backend, w, r, WarehouseResult{Status: status, Hash: hash})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user