From 4687e9e710073ee442f108446f631b195e20a9ce Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 17 Jun 2023 09:52:35 -0600 Subject: [PATCH] namespace --- vicuna-tools.d/main.go | 57 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/vicuna-tools.d/main.go b/vicuna-tools.d/main.go index a2e98f9..d04bf0c 100644 --- a/vicuna-tools.d/main.go +++ b/vicuna-tools.d/main.go @@ -12,6 +12,7 @@ import ( "net/http" "os" "os/signal" + "path" "strings" "sync" "syscall" @@ -19,10 +20,12 @@ import ( var ( Config struct { - Port int - SessionD string - Debug bool - Semaphore sync.Mutex + Port int + Debug bool + ChatBot struct { + SessionD string + semaphore sync.Mutex + } } //go:embed template.d/login.html @@ -77,7 +80,7 @@ func config(ctx context.Context) { contextWithCleanupFunc(ctx, func() { os.RemoveAll(d) }) flag.IntVar(&Config.Port, "p", 37070, "port to listen on") - flag.StringVar(&Config.SessionD, "d", d, "dir to store sessions") + flag.StringVar(&Config.ChatBot.SessionD, "chatbot-d", d, "dir to store chat bot sessions") flag.BoolVar(&Config.Debug, "debug", false, "debug mode") flag.Parse() } @@ -232,18 +235,52 @@ func handleNotFound(w http.ResponseWriter, r *http.Request) error { } func handleAPIChatBot(w http.ResponseWriter, r *http.Request) error { - cookie, _ := ParseCookie(r) err := r.ParseForm() if err != nil { return err } - got := r.PostForm.Get("Message") - if len(got) == 0 { + switch r.Method { + case http.MethodPost: + return handleAPIChatBotPost(w, r) + case http.MethodPut: + return handleAPIChatBotPut(w, r) + default: + return handleNotFound(w, r) + } +} + +func handleAPIChatBotPost(w http.ResponseWriter, r *http.Request) error { + cookie, _ := ParseCookie(r) + sessionD := path.Join(Config.ChatBot.SessionD, cookie.Name) + if _, err := os.Stat(path.Join(sessionD, "chat.d")); err == nil { + if err := os.RemoveAll(path.Join(sessionD, "chat.d")); err != nil { + return err + } + } + if err := os.MkdirAll(path.Join(sessionD, "chat.d"), os.ModePerm); err != nil { + return err + } + + prompt := r.PostForm.Get("Prompt") + if len(prompt) == 0 { + return errors.New("no prompt") + } + if err := os.WriteFile(path.Join(sessionD, "prompt.txt"), []byte(prompt), os.ModePerm); err != nil { + return err + } + + return handleAPIChatBotPut(w, r) +} + +func handleAPIChatBotPut(w http.ResponseWriter, r *http.Request) error { + message := r.PostForm.Get("Message") + if len(message) == 0 { return errors.New("empty Message") } - Config.Semaphore.Lock() - defer Config.Semaphore.Unlock() + Config.ChatBot.semaphore.Lock() + defer Config.ChatBot.semaphore.Unlock() + return errors.New("not impl") }