Skip to content

Commit

Permalink
Merge pull request #30 from msfidelis/feature/filesystem
Browse files Browse the repository at this point in the history
Feature: fs operation
  • Loading branch information
msfidelis authored Jan 10, 2025
2 parents 08923a6 + a7d886f commit 4131a80
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,32 @@ Content-Length: 61
{"status":200,"message":"2000 logging events sent to stdout"}
```

## Filesystem

List some directory contents

```bash
curl -X POST "0.0.0.0:8080/filesystem/ls" -i -d '{"path": "./"}'
```

Write file

```bash
curl -X POST "0.0.0.0:8080/filesystem/write" -d '{"path": "./test", "content": "V3JpdGUgVGVzdAo="}';
```

Read files on filesystem

```bash
curl -X POST "0.0.0.0:8080/filesystem/cat" -d '{"path": "./test"}';
```

Delete files on filesystem

```bash
curl -X DELETE "0.0.0.0:8080/filesystem/delete" -d '{"path": "./test"}';
```

## Whoami?

```sh
Expand Down
29 changes: 29 additions & 0 deletions controllers/filesystem/cat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package filesystem

import (
"net/http"
"os"

"github.com/gin-gonic/gin"
)

type RequestCat struct {
Path string `json:"path"`
}

func Cat(c *gin.Context) {
var request RequestCat

if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

content, err := os.ReadFile(request.Path)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.String(http.StatusOK, string(content))
}
25 changes: 25 additions & 0 deletions controllers/filesystem/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package filesystem

import (
"net/http"
"os"

"github.com/gin-gonic/gin"
)

type RequestDelete struct {
Path string `json:"path"`
}

func DeleteFile(c *gin.Context) {
var request RequestDelete
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := os.Remove(request.Path); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "file deleted"})
}
47 changes: 47 additions & 0 deletions controllers/filesystem/ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package filesystem

import (
"fmt"
"net/http"
"os"

"github.com/gin-gonic/gin"
)

type RequestLs struct {
Path string `json:"path"`
}

type ResponseLs struct {
Path string `json:"path"`
Files []string `json:"files"`
}

func Ls(c *gin.Context) {

var request RequestLs
var response ResponseLs

if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

files, err := os.ReadDir(request.Path)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

var fileNames []string
for _, file := range files {
fileNames = append(fileNames, file.Name())
}

fmt.Println(files)

response.Path = request.Path
response.Files = fileNames

c.JSON(200, response)
}
37 changes: 37 additions & 0 deletions controllers/filesystem/write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package filesystem

import (
"encoding/base64"
"net/http"
"os"

"github.com/gin-gonic/gin"
)

type RequestWrite struct {
Path string `json:"path"`
Content string `json:"content"`
}

func WriteFile(c *gin.Context) {
var request RequestWrite

if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

decodedContent, err := base64.StdEncoding.DecodeString(request.Content)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid base64 content"})
return
}

err = os.WriteFile(request.Path, decodedContent, 0644)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusAccepted, gin.H{"message": "file written", "path": request.Path})
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"chip/controllers/burn"
"chip/controllers/filesystem"
"chip/controllers/healthcheck"
"chip/controllers/liveness"
"chip/controllers/logging"
Expand Down Expand Up @@ -164,6 +165,12 @@ func main() {
// Proxy
router.POST("/proxy", proxy.Post)

// Filesystem
router.POST("/filesystem/ls", filesystem.Ls)
router.POST("/filesystem/cat", filesystem.Cat)
router.POST("/filesystem/write", filesystem.WriteFile)
router.DELETE("/filesystem/delete", filesystem.DeleteFile)

// Graceful Shutdown Config
srv := &http.Server{
Addr: ":8080",
Expand Down

0 comments on commit 4131a80

Please sign in to comment.