Skip to content

Commit bab32c6

Browse files
committed
Added bearer auth
1 parent 31de19e commit bab32c6

File tree

6 files changed

+75
-8
lines changed

6 files changed

+75
-8
lines changed

cmdexec/cmdexec.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/HexmosTech/httpie-go"
1515
"github.com/HexmosTech/lama2/utils"
16+
"fmt"
1617
)
1718

1819
// ExecCommand changes directory to the given `apiDir`
@@ -22,6 +23,8 @@ import (
2223
// Once execution finishes, previous CWD is restored,
2324
// and the command output is returned as a string
2425
func ExecCommand(cmdSlice []string, stdinBody string, apiDir string) (httpie.ExResponse, int64, error) {
26+
fmt.Println("Command Slice:", cmdSlice)
27+
fmt.Println("Standard Input Body:", stdinBody)
2528
oldDir, _ := os.Getwd()
2629
utils.ChangeWorkingDir(apiDir)
2730
start := time.Now()

cmdgen/cmdgen.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/HexmosTech/gabs/v2"
1414
"github.com/HexmosTech/lama2/lama2cmd"
15+
"syscall/js"
1516
)
1617

1718
// The assembleCmdString function constructs a httpie
@@ -38,13 +39,6 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header
3839
jsonStr = dst.String()
3940
}
4041

41-
/*
42-
if !multipart && jsonStr != "" {
43-
command = append(command, "echo '")
44-
command = append(command, jsonStr)
45-
command = append(command, "' |")
46-
}*/
47-
4842
command = append(command, "ht ")
4943
if o != nil && o.Nocolor {
5044
command = append(command, "--pretty=none ")
@@ -78,6 +72,30 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header
7872
command = append(command, key+":"+val.Data().(*gabs.Container).Data().(string))
7973
}
8074
}
75+
76+
// Add the Authorization header
77+
// LaBearerAuthToken := js.Global().Get("LaBearerAuthToken").String()
78+
// command = append(command, "Authorization: Bearer " + LaBearerAuthToken)
79+
80+
LaBearerAuthToken := js.Global().Get("LaBearerAuthToken").String()
81+
82+
// Check if Authorization header is already present
83+
authHeaderExists := false
84+
for _, header := range command {
85+
if len(header) >= len("Authorization:") && header[:len("Authorization:")] == "Authorization:" {
86+
authHeaderExists = true
87+
break
88+
}
89+
}
90+
91+
// Append the Authorization header only if it doesn't already exist
92+
if !authHeaderExists {
93+
command = append(command, "Authorization: Bearer "+LaBearerAuthToken)
94+
}
95+
96+
97+
98+
8199
cleanCommand := make([]string, 0)
82100
for _, c := range command {
83101
cleanC := strings.TrimSpace(c)

cmdgen/cmdgen.wasm.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ package cmdgen
55
import (
66
"github.com/HexmosTech/gabs/v2"
77
"github.com/HexmosTech/lama2/lama2cmd"
8+
"fmt"
9+
"syscall/js"
810
)
911

12+
13+
var myPassedValue string
14+
func setMyValue(this js.Value, p []js.Value) interface{} {
15+
myPassedValue = p[0].String() // Get the first parameter
16+
fmt.Println("DBGX: Received value from JS in Go:", myPassedValue)
17+
return nil
18+
}
19+
1020
func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, string) {
1121
httpv, url, jsonObj, headers, multipartBool, formBool := ConstructCommandHelper(parsedInput)
1222
res, stdinBody := assembleCmdString(httpv, url, jsonObj, headers, multipartBool, formBool, nil)

l2.wasm.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
var version string
14-
14+
var LaBearerAuthToken string // Exported global variable
1515
func init() {
1616
// Initialize version if not set
1717
if len(version) == 0 {
@@ -30,6 +30,8 @@ func main() {
3030

3131
func wasmLamaPromise() js.Func {
3232
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
33+
LaBearerAuthToken = args[2].String()
34+
js.Global().Set("LaBearerAuthToken", LaBearerAuthToken)
3335
inputdata := args[0].String()
3436
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
3537
resolve := args[0]

localwasmbuild.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GOOS=js GOARCH=wasm go build -a -gcflags=all="-l -B -wb=false" -ldflags="-w -s" -o static/main.wasm
2+
cp /home/rtp/Projects/Lama2/static/main.wasm /home/rtp/Projects/liveapi/dist/main.wasm

preprocess/webworker.wasm.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,35 @@ func GetFromWorker(variableName string) string {
106106
fmt.Println("WW: getfromworker webworker.go", variableName, result.Get("value").String())
107107
return result.Get("value").String()
108108
}
109+
110+
func GetFromLocalStorage(key string) string {
111+
worker := InitWebWorker() // Initialize the Web Worker
112+
responseChannel := make(chan js.Value, 1) // Single-buffered channel for one response
113+
114+
// Send the request to the Web Worker
115+
worker.Call("postMessage", map[string]interface{}{
116+
"action": "get",
117+
"key": key,
118+
})
119+
120+
// Set up an event listener to handle the Web Worker's response
121+
worker.Call("addEventListener", "message", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
122+
data := args[0].Get("data")
123+
if data.Get("result").IsUndefined() {
124+
log.Error().Msg("WW: LocalStorage value is undefined")
125+
return nil
126+
}
127+
128+
select {
129+
case responseChannel <- data:
130+
log.Debug().Msg("WW: Value received from Web Worker")
131+
default:
132+
log.Error().Msg("WW: responseChannel is full, cannot write data")
133+
}
134+
return nil
135+
}))
136+
137+
// Wait for the response and retrieve the result
138+
result := <-responseChannel
139+
return result.Get("result").String()
140+
}

0 commit comments

Comments
 (0)