Skip to content

Commit

Permalink
/POST support
Browse files Browse the repository at this point in the history
  • Loading branch information
thbkrkr committed Jan 18, 2016
1 parent 1372393 commit 943b7a8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
7 changes: 7 additions & 0 deletions example/api/test/post.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash -eu

IN="$(cat /dev/stdin)"

echo '{
"jackpot": '$(jq .o <<< $IN)'
}'
60 changes: 58 additions & 2 deletions handlers/exec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"bytes"
"encoding/json"
"fmt"
"log"
Expand All @@ -18,9 +19,8 @@ func (h *ExecHandler) ExecScript(c *gin.Context) {
var stdout []byte
var err error

path := c.Param("path")

// Build script name
path := c.Param("path")
script := fmt.Sprintf("%s%s%s", *h.ApiDir, path, ".sh")

// Check script exists
Expand Down Expand Up @@ -65,3 +65,59 @@ func (h *ExecHandler) ExecScript(c *gin.Context) {
c.JSON(200, someJson)
//log.Printf("[info] executing `%s`: %s", script, stdout)
}

func (h *ExecHandler) PostExecScript(c *gin.Context) {
var stdout []byte
var err error

// Build script name
path := c.Param("path")
script := fmt.Sprintf("%s%s%s", *h.ApiDir, path, ".sh")

// Check script exists
if _, err := os.Stat(script); os.IsNotExist(err) {
c.JSON(404, gin.H{
"error": "Resource not found",
})
fmt.Printf("[error] resource not found: %s", script)
return
}

// Exec script with or without body

c1 := exec.Command(script)

body := c.Request.Body
var buf bytes.Buffer

c1.Stdin = body
c1.Stdout = &buf
_ = c1.Start()
_ = c1.Wait()

if err != nil {
serr := err.Error()
c.JSON(500, gin.H{
"error": serr,
})
log.Printf("[error] executing `%s`: %s", path, serr)
return
}

stdout = buf.Bytes()

// Try to unmarshal JSON
var someJson interface{}
err = json.Unmarshal(stdout, &someJson)

if err != nil {
c.JSON(400, gin.H{
"error": "Invalid JSON",
})
log.Printf("[error] invalid JSON for `%s`: %s", script, stdout)
return
}

c.JSON(200, someJson)
//log.Printf("[info] executing `%s`: %s", script, stdout)
}
2 changes: 2 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var auth = &test.BasicAuth{"zuperadmin", "42"}

func init() {
gin.SetMode(gin.TestMode)
*apiDir = "example/api"
*password = "42"
server = httptest.NewServer(Router())

test.ServerURL = server.URL
Expand Down
5 changes: 4 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
m "github.com/thbkrkr/go-apish/middlewares"
)

var basicAuthUser = "zuperadmin"

func Router() *gin.Engine {
router := gin.Default()

Expand All @@ -25,7 +27,7 @@ func Router() *gin.Engine {
authorized = router.Group("/", m.AuthMiddleware(
*apiKey,
gin.Accounts{
"zuperadmin": *password,
basicAuthUser: *password,
},
))
}
Expand All @@ -43,6 +45,7 @@ func Router() *gin.Engine {

// API propulsed by shell scripts
authorized.GET("/api/*path", execHandler.ExecScript)
authorized.POST("/api/*path", execHandler.PostExecScript)

// Static files
authorized.Static("/s/", *apiDir+"/_static")
Expand Down

0 comments on commit 943b7a8

Please sign in to comment.