namespace
parent
13c64896eb
commit
4687e9e710
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
@ -19,10 +20,12 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Config struct {
|
Config struct {
|
||||||
Port int
|
Port int
|
||||||
SessionD string
|
Debug bool
|
||||||
Debug bool
|
ChatBot struct {
|
||||||
Semaphore sync.Mutex
|
SessionD string
|
||||||
|
semaphore sync.Mutex
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed template.d/login.html
|
//go:embed template.d/login.html
|
||||||
|
|
@ -77,7 +80,7 @@ func config(ctx context.Context) {
|
||||||
contextWithCleanupFunc(ctx, func() { os.RemoveAll(d) })
|
contextWithCleanupFunc(ctx, func() { os.RemoveAll(d) })
|
||||||
|
|
||||||
flag.IntVar(&Config.Port, "p", 37070, "port to listen on")
|
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.BoolVar(&Config.Debug, "debug", false, "debug mode")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
}
|
}
|
||||||
|
|
@ -232,18 +235,52 @@ func handleNotFound(w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAPIChatBot(w http.ResponseWriter, r *http.Request) error {
|
func handleAPIChatBot(w http.ResponseWriter, r *http.Request) error {
|
||||||
cookie, _ := ParseCookie(r)
|
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
got := r.PostForm.Get("Message")
|
switch r.Method {
|
||||||
if len(got) == 0 {
|
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")
|
return errors.New("empty Message")
|
||||||
}
|
}
|
||||||
|
|
||||||
Config.Semaphore.Lock()
|
Config.ChatBot.semaphore.Lock()
|
||||||
defer Config.Semaphore.Unlock()
|
defer Config.ChatBot.semaphore.Unlock()
|
||||||
|
|
||||||
return errors.New("not impl")
|
return errors.New("not impl")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue