Skip to content

Commit

Permalink
11 local setup (#27)
Browse files Browse the repository at this point in the history
* chore(core): refactoring - handler is moved from lambda to be reused for means of deployment other than aws lambda.

Signed-off-by: Dmitry Kisler <admin@dkisler.com>

* chore(lambda): refactor utils funcs.

Signed-off-by: Dmitry Kisler <admin@dkisler.com>

* chore(httpserver): add httpserver def.

Signed-off-by: Dmitry Kisler <admin@dkisler.com>

* chore(readme): update.

Signed-off-by: Dmitry Kisler <admin@dkisler.com>

* chore(localenv): add local dev setup provisioning scripts.

Signed-off-by: Dmitry Kisler <admin@dkisler.com>

* chore(ci): fixes pkg list for unittest.

Signed-off-by: Dmitry Kisler <admin@dkisler.com>

---------

Signed-off-by: Dmitry Kisler <admin@dkisler.com>
  • Loading branch information
kislerdm committed Feb 12, 2023
1 parent 38c15c2 commit 39aa244
Show file tree
Hide file tree
Showing 14 changed files with 649 additions and 284 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: |
cd core
go mod tidy
go test -race -coverprofile=coverage.txt -covermode=atomic
go test -race -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.DEFAULT_GOAL := help

help: ## Prints help message.
@ grep -h -E '^[a-zA-Z0-9_-].+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[1m%-30s\033[0m %s\n", $$1, $$2}'

PORT_SERVER := 9000
PORT_CLIENT := 9001
GIT_SHA := `git log --pretty=format:"%H" -1`

localenv: ## Provisions local development environment.
@ if [ "${OPENAI_API_KEY}" == "" ]; then echo "set OPENAI_API_KEY environment variable"; exit 137; fi
@ echo "access webclient on http://localhost:${PORT_CLIENT}"
@ VERSION=${GIT_SHA} PORT_SERVER=${PORT_SERVER} PORT_CLIENT=${PORT_CLIENT} docker compose up
76 changes: 54 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Thank you! 🙏
+ [Bets/Panning/Commitment](#betspanningcommitment)
* [Tech details](#tech-details)
+ [Architecture](#architecture)
+ [Tech stack](#tech-stack)
+ [Local setup](#local-setup)
* [License](#license)
+ [Codebase](#codebase)
+ [Images and diagrams](#images-and-diagrams)
Expand Down Expand Up @@ -80,38 +80,70 @@ Thank you! 🙏
<summary><strong>Tech stack</strong></summary>

- Languages:
- Go 1.19
- JavaScript ES2021
- Python 3.9
- Go 1.19
- JavaScript ES2021
- Python 3.9
- Markup:
- Markdown
- HTML5
- CSS3
- Markdown
- HTML5
- CSS3
- CI:
- GitHub Actions
- GitHub Actions
- Infra:
- AWS:
- IAM
- Lambda
- API Gateway
- GitHub Pages
- [Neon](https://neon.tech/)
- Cloudflare
- namecheap
- godaddy
- AWS:
- IAM
- Lambda
- API Gateway
- GitHub Pages
- [Neon](https://neon.tech/)
- Cloudflare
- namecheap
- godaddy
- Tools:
- gnuMake
- Docker
- terraform
- gnuMake
- Docker
- terraform
- Logic:
- PlantUML
- OpenAI
- PlantUML
- OpenAI

</details>

### Architecture

![architecture](https://www.plantuml.com/plantuml/svg/RP3DRk8m58NtUGfFf151cf3DhAggeeK8gFY9-56NaHDV4akE7TatBRnzxM1eKPjDh7FEPvzxnmQfnguHmHykIz4n83LYQnwIHDEFKSMnxehEW2wLH90uAbMJj89AnyG6cU15ClaVPquwh9P9Gms2jb8-SSG9HwsxFK2E0aZ8EAqqTI5dCNWdvIL6l1C6mL4f14t2HuIo7lyWdaXC_ZAA40tEzejNgvYnmT226MYZP9wUC7AL_v7mO7_XC0XsPuitKSUzHXOIGHzf2TRrPW7Md2Zz9VKtgHOaTTp67fuNz-Pr4zQ-Ri0zjmMHRts7_iqfc98NO0ZMS9sKS4bIMGbkwj16zdOyothKGdsVJAkMLXGzschLjEZYy_q-IrvtcxLd3dt_Mzc5F8A-C8rY87v3fZtoROGPID0KxslUo1SkgJxtsvnltl9bEalNqsWOZ44ooty2)

### Local setup

#### Requirements

- git
- gnuMake
- docker ~> 20.10

#### Steps

Follow the steps to provision local development environment.

1. Clone the repo

```commandline
git clone git@github.com:kislerdm/diagramastext.git
```

2. Export OpenAI access [token](https://platform.openai.com/docs/api-reference/authentication) as the environment
variable `OPENAI_API_KEY`:

```commandline
export OPENAI_API_KEY=##TOKEN##
```

3. Run to command to provision the local environemnt:

```commandline
make localenv
```

## License

### Codebase
Expand Down
11 changes: 9 additions & 2 deletions core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ help: ## Prints help message.

test: ## Run tests of the core module.
@ go mod tidy && \
go test -timeout 3m --tags=unittest -v -coverprofile=.coverage.out . -coverpkg=. && \
go test -timeout 3m ./... --tags=unittest -v -coverprofile=.coverage.out . -coverpkg=. && \
go tool cover -func .coverage.out && rm .coverage.out

test-lambda: ## Run tests for the lambda function.
@ cd cmd/lambda && \
go test -timeout 3m --tags=unittest -v -coverprofile=.coverage.out . -coverpkg=. && \
go mod tidy && \
go test -timeout 3m -v -coverprofile=.coverage.out . -coverpkg=. && \
go tool cover -func .coverage.out && rm .coverage.out

compile:
Expand All @@ -24,3 +25,9 @@ build: compile ## Builds the lambda binary and archives it.

deploy-lambda: ## Deploys the core logic as AWS lambda.
@ cd deployment && terraform plan && terraform apply -auto-approve

compile-httpserver: ## Compiles httpserver
@ test -d bin || mkdir -p bin && \
cd cmd/httpserver && \
go mod tidy && \
CGO_ENABLED=0 go build -o ../../bin/httpserver -ldflags="-s -w" .
117 changes: 117 additions & 0 deletions core/cmd/httpserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"context"
"encoding/json"
"io"
"log"
"net/http"
"os"
"strconv"

coreHandler "github.com/kislerdm/diagramastext/core/handler"
"github.com/kislerdm/diagramastext/core/utils"

"github.com/kislerdm/diagramastext/core"
)

type httpHandler struct {
clientModel core.ClientInputToGraph
clientDiagram core.ClientGraphToDiagram
reportErrorFn func(err error)
corsHeaders corsHeaders
}

func (h httpHandler) response(w http.ResponseWriter, body []byte, status int, err error) {
if err != nil {
h.reportErrorFn(err)
}
h.corsHeaders.setHeaders(w.Header())
w.WriteHeader(status)
_, _ = w.Write(body)
}

func (h httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
h.response(w, nil, http.StatusOK, nil)
return
}

defer func() { _ = r.Body.Close() }()
body, err := io.ReadAll(r.Body)
if err != nil {
h.response(w, []byte("could not recognise the prompt format"), http.StatusUnprocessableEntity, err)
return
}

prompt, err := coreHandler.ReadPrompt(body)
if err != nil {
h.response(w, []byte("could not recognise the prompt format"), http.StatusUnprocessableEntity, err)
return
}

if err := coreHandler.ValidatePrompt(prompt); err != nil {
h.response(w, []byte(err.Error()), http.StatusUnprocessableEntity, err)
return
}

graph, err := h.clientModel.Do(context.Background(), prompt)
if err != nil {
e := coreHandler.ParseClientError(err)
h.response(w, e.Body, e.StatusCode, err)
return
}

svg, err := h.clientDiagram.Do(context.Background(), graph)
if err != nil {
e := coreHandler.ParseClientError(err)
h.response(w, e.Body, e.StatusCode, err)
return
}

h.response(w, svg.MustMarshal(), http.StatusOK, nil)
}

func main() {
clientOpenAI, err := core.NewOpenAIClient(
core.ConfigOpenAI{
Token: os.Getenv("OPENAI_API_KEY"),
MaxTokens: utils.MustParseInt(os.Getenv("OPENAI_MAX_TOKENS")),
Temperature: utils.MustParseFloat32(os.Getenv("OPENAI_TEMPERATURE")),
},
)
if err != nil {
log.Fatalln(err)
}

handler := httpHandler{
clientModel: clientOpenAI,
clientDiagram: core.NewPlantUMLClient(),
reportErrorFn: func(err error) { log.Println(err) },
}

if v := os.Getenv("CORS_HEADERS"); v != "" {
_ = json.Unmarshal([]byte(v), &handler.corsHeaders)
}

port := 9000
if v := utils.MustParseInt(os.Getenv("PORT")); v > 0 {
port = v
}

if err := http.ListenAndServe(":"+strconv.Itoa(port), handler); err != nil {
log.Println(err)
}
}

type corsHeaders map[string]string

func (h corsHeaders) setHeaders(header http.Header) {
for k, v := range h {
header.Set(k, v)

if k == "Access-Control-Allow-Origin" && (v == "" || v == "'*'") {
header.Set(k, "*")
}
}
}
129 changes: 0 additions & 129 deletions core/cmd/lambda/handler.go

This file was deleted.

0 comments on commit 39aa244

Please sign in to comment.