-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
14 changed files
with
649 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "*") | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.