namespace
parent
13c64896eb
commit
4687e9e710
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue