aboutsummaryrefslogtreecommitdiff
path: root/http_server.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-11-14 22:55:29 +0700
committerShulhan <ms@kilabit.info>2023-11-15 02:26:52 +0700
commitaced8dda50746fc349691f932ae32619688334d4 (patch)
treea945b757cb7984000f4877fb3fdc968daac2f91b /http_server.go
parent61ece8913599fa79c3fced73efb3973eab91d024 (diff)
downloadawwan-aced8dda50746fc349691f932ae32619688334d4.tar.xz
all: implement HTTP API to encrypt file
Similar to the CLI, the HTTP API accept the path of file and return the path to encrypted file.
Diffstat (limited to 'http_server.go')
-rw-r--r--http_server.go97
1 files changed, 96 insertions, 1 deletions
diff --git a/http_server.go b/http_server.go
index a618058..1c9a4ad 100644
--- a/http_server.go
+++ b/http_server.go
@@ -6,7 +6,9 @@ package awwan
import (
"bytes"
"encoding/json"
+ "errors"
"fmt"
+ "io/fs"
"net/http"
"os"
"path"
@@ -20,8 +22,9 @@ import (
)
const (
- pathAwwanApiFs = `/awwan/api/fs`
+ pathAwwanApiEncrypt = `/awwan/api/encrypt`
pathAwwanApiExecute = `/awwan/api/execute`
+ pathAwwanApiFs = `/awwan/api/fs`
)
const paramNamePath = `path`
@@ -133,6 +136,18 @@ func (httpd *httpServer) registerEndpoints() (err error) {
return fmt.Errorf("%s: %w", logp, err)
}
+ var ep = libhttp.Endpoint{
+ Method: libhttp.RequestMethodPost,
+ Path: pathAwwanApiEncrypt,
+ RequestType: libhttp.RequestTypeJSON,
+ ResponseType: libhttp.ResponseTypeJSON,
+ Call: httpd.Encrypt,
+ }
+ err = httpd.RegisterEndpoint(&ep)
+ if err != nil {
+ return fmt.Errorf(`%s %q: %w`, logp, ep.Path, err)
+ }
+
err = httpd.RegisterEndpoint(&libhttp.Endpoint{
Method: libhttp.RequestMethodPost,
Path: pathAwwanApiExecute,
@@ -154,6 +169,86 @@ func (httpd *httpServer) start() (err error) {
return httpd.Server.Start()
}
+// Encrypt the file by path.
+//
+// Request format,
+//
+// POST /awwan/api/encrypt
+// Content-Type: application/json
+//
+// {"path":<string>}
+//
+// On success it will return the path to encrypted file in "path_vault",
+//
+// Content-Type: application/json
+//
+// {
+// "code": 200,
+// "data": {
+// "path": <string>,
+// "path_vault": <string>
+// }
+// }
+func (httpd *httpServer) Encrypt(epr *libhttp.EndpointRequest) (resb []byte, err error) {
+ var (
+ logp = `Encrypt`
+ httpRes = &libhttp.EndpointResponse{}
+
+ encRes encryptResponse
+ )
+
+ err = json.Unmarshal(epr.RequestBody, &encRes)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+
+ httpRes.Code = http.StatusBadRequest
+
+ encRes.Path = strings.TrimSpace(encRes.Path)
+ if len(encRes.Path) == 0 {
+ httpRes.Message = fmt.Sprintf(`%s: empty path`, logp)
+ return nil, httpRes
+ }
+
+ var node *memfs.Node
+
+ node, err = httpd.memfsBase.Get(encRes.Path)
+ if err != nil {
+ if errors.Is(err, fs.ErrNotExist) {
+ httpRes.Message = fmt.Sprintf(`%s %q: %s`, logp, encRes.Path, fs.ErrNotExist)
+ } else {
+ httpRes.Message = fmt.Sprintf(`%s: %s`, logp, err)
+ }
+ return nil, httpRes
+ }
+ if node.IsDir() {
+ httpRes.Message = fmt.Sprintf(`%s: %q is a directory`, logp, encRes.Path)
+ return nil, httpRes
+ }
+
+ httpRes.Code = http.StatusInternalServerError
+
+ encRes.PathVault, err = httpd.aww.Encrypt(node.SysPath)
+ if err != nil {
+ httpRes.Message = fmt.Sprintf(`%s: %s`, logp, err)
+ return nil, httpRes
+ }
+
+ node.Parent.Update(nil, -1)
+
+ encRes.PathVault, _ = strings.CutPrefix(encRes.PathVault, httpd.aww.BaseDir)
+
+ httpRes.Code = http.StatusOK
+ httpRes.Data = encRes
+
+ resb, err = json.Marshal(&httpRes)
+ if err != nil {
+ return nil, err
+ }
+
+ return resb, nil
+}
+
// awwanApiFsGet get the list of files or specific file using query
// parameter "path".
func (httpd *httpServer) awwanApiFsGet(epr *libhttp.EndpointRequest) (resb []byte, err error) {