Skip to content

Commit

Permalink
Working out a sandwich issue
Browse files Browse the repository at this point in the history
  • Loading branch information
suhay committed Nov 28, 2021
1 parent a131543 commit c735aaa
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 37 deletions.
File renamed without changes.
26 changes: 1 addition & 25 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
GOCMD=go
GOINSTALL=$(GOCMD) install
GOCLEAN=$(GOCMD) clean
GOWICHINSTALL=$(GOINSTALL) ${GOPATH}/src/github.com/suhay/sandwich-shop/gowich/gowich.go
NODEWICH=cd ${GOPATH}/src/github.com/suhay/sandwich-shop/nodewich && yarn install --production && cd $(shell pwd)

install:
$(GOINSTALL) ${GOPATH}/src/github.com/suhay/sandwich-shop/shop/sandwich-shop.go

sandwiches:
$(GOWICHINSTALL)
$(NODEWICH)

nodewich:
$(NODEWICH)

gowich:
$(GOWICHINSTALL)

clean:
$(GOCLEAN)
rm -f ${GOPATH}/bin/sandwich-shop
rm -f ${GOPATH}/bin/gowich

gqlgen:
go run github.com/99designs/gqlgen

Expand All @@ -33,4 +9,4 @@ dev:

build:
go build
cd sandwiches/gowich; go build
cd sandwiches/gowich; go build
4 changes: 2 additions & 2 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func mongoAuth(tenantID string, mongodbURL string) (string, error) {
}

func localAuth(tenantID string) (string, error) {
tenants := "../tenants"
tenants := "tenants"
if envTenants, ok := os.LookupEnv("TENANTS"); ok {
tenants = envTenants
}
Expand All @@ -109,5 +109,5 @@ func localAuth(tenantID string) (string, error) {
return "", err
}

return string(dat), nil
return strings.TrimSpace(string(dat)), nil
}
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: "3.1"

services:
shop:
image: golang:1.17
ports:
- "3002:3002"
volumes:
- .:/code
working_dir: /code
command: "./sandwich-shop"
environment:
- MODE=DEV
links:
- "gowich"

gowich:
image: golang:1.17
ports:
- "4007:4007"
volumes:
- .:/code
working_dir: /code/sandwiches/gowich
command: "./gowich --env /code/.node1.env --port 4007"
1 change: 0 additions & 1 deletion sandwich-shop.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func main() {
}

port := setPort(*flagPort)

srv := handler.NewDefaultServer(models.NewExecutableSchema(models.Config{Resolvers: &shop.Resolver{}}))
r := chi.NewRouter()

Expand Down
17 changes: 15 additions & 2 deletions sandwiches/gowich/gowich.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func main() {
if !claims.Authorized {
if len(claims.Auth) > 0 {
out, err := placeOrder(claims.Auth, claims, body, header)
log.Println(out)
if err != nil || strings.ToLower(out) != "true" {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
Expand All @@ -118,7 +117,9 @@ func main() {

order := chi.URLParam(r, "order")
out, err := placeOrder(order, claims, body, header)

log.Println(out)
log.Println(err)

if err != nil {
log.Println(err.Error())
Expand Down Expand Up @@ -150,7 +151,14 @@ func placeOrder(order string, claims *order, body string, header string) (string
if claims.Runtime == "binary" {
cmd = exec.Command(order, body, header)
} else {
cmd = exec.Command(os.Getenv(strings.ToUpper(claims.Runtime)), order, body, header)
runtime := strings.ToUpper(claims.Runtime)
name := os.Getenv(runtime)

if strings.HasPrefix(runtime, "GO") {
cmd = exec.Command(name, "run", "/code/tenants/b78682b3-36c8-4759-b8d1-5e62f029a1bc/make_sandwich.go", body, header)
} else {
cmd = exec.Command(name, order, body, header)
}
}

if len(claims.Env) > 0 {
Expand All @@ -166,6 +174,9 @@ func placeOrder(order string, claims *order, body string, header string) (string

cmd.Dir = tenants + "/" + claims.Tenant

log.Println(os.Getwd())
log.Println(cmd)

// cmd.SysProcAttr = &syscall.SysProcAttr{}

// uid, uerr := strconv.ParseUint(os.Getenv("UID"), 10, 32)
Expand All @@ -185,5 +196,7 @@ func placeOrder(order string, claims *order, body string, header string) (string
// hold := &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}

out, err := cmd.Output()
log.Println(out)
log.Println(err)
return strings.TrimSpace(string(out)), err
}
File renamed without changes.
4 changes: 2 additions & 2 deletions sandwiches/nodewich/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodewich",
"version": "1.0.0",
"version": "0.3.0",
"description": "Node based shop for https://github.com/suhay/sandwich-shop",
"main": "index.js",
"scripts": {
Expand All @@ -20,4 +20,4 @@
"bin": {
"nodewich": "bin/nodewich.js"
}
}
}
5 changes: 5 additions & 0 deletions shop/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func PlaceOrder(w http.ResponseWriter, r *http.Request) {
}

if body, _ := ioutil.ReadAll(resp.Body); len(body) > 0 {
if resp.StatusCode != 200 {
http.Error(w, http.StatusText(resp.StatusCode), resp.StatusCode)
return
}

w.Header().Add("Content-Type", "application/json")
w.Write(body)
return
Expand Down
7 changes: 2 additions & 5 deletions tenants/sandwiches.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
{
"_id": "ciabatta",
"name": "Ciabatta",
"host": "http://localhost",
"host": "http://gowich",
"port": 4007,
"runtimes": [
"go1_17",
"node14",
"node16",
"python3"
"go1_17"
]
}
]

0 comments on commit c735aaa

Please sign in to comment.