ai/vicuna-tools.d/main.go

236 lines
4.6 KiB
Go

package main
import (
"context"
_ "embed"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
)
var (
Config struct {
Port int
SessionD string
Debug bool
}
//go:embed template.d/login.html
htmlLogin []byte
//go:embed template.d/index.html
htmlIndex []byte
//go:embed template.d/chatbot.html
htmlChatBot []byte
)
func main() {
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
defer can()
ctx, cleanup := contextWithCleanup(ctx)
defer cleanup()
config(ctx)
log.Printf("%+v", Config)
listenAndServe(ctx)
log.Println("done")
}
func contextWithCleanup(ctx context.Context) (context.Context, func()) {
m := map[int]func(){}
ctx = context.WithValue(ctx, "__cleanup__", m)
return ctx, func() {
defer func() {
recover()
}()
for _, v := range m {
v()
}
}
}
func contextWithCleanupFunc(ctx context.Context, foo func()) {
v := ctx.Value("__cleanup__")
if v == nil {
panic("cannot get context with cleanup func that doesnt have cleanup init")
}
m := v.(map[int]func())
m[len(m)] = foo
}
func config(ctx context.Context) {
d, err := os.MkdirTemp(os.TempDir(), "ai.*")
if err != nil {
panic(err)
}
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.BoolVar(&Config.Debug, "debug", false, "debug mode")
flag.Parse()
}
func listenAndServe(ctx context.Context) {
wg := &sync.WaitGroup{}
s := &http.Server{
Addr: fmt.Sprintf(":%d", Config.Port),
Handler: http.HandlerFunc(handle),
}
wg.Add(1)
go func() {
defer wg.Done()
if err := s.ListenAndServe(); err != nil && ctx.Err() == nil {
panic(err)
}
}()
<-ctx.Done()
s.Close()
wg.Wait()
}
func handle(w http.ResponseWriter, r *http.Request) {
cookie, _ := ParseCookie(r)
if err := _handle(w, r); err != nil {
log.Printf("%s: %s %s: %v", cookie.Name, r.Method, r.URL.Path, err)
} else {
log.Printf("%s: %s %s", cookie.Name, r.Method, r.URL.Path)
}
}
func _handle(w http.ResponseWriter, r *http.Request) error {
first := strings.Split(strings.TrimLeft(r.URL.Path, "/"), "/")[0]
switch first {
case "login":
return handleLogin(w, r)
case "api":
return handleAPI(w, r)
default:
return handleUI(w, r)
}
}
func handleLogin(w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case http.MethodGet:
w.Write(htmlLogin)
return nil
case http.MethodPost:
err := r.ParseForm()
if err != nil {
return err
}
cookie, err := ParseCookie(r)
if err != nil {
return err
}
cookie.Serialize(w)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return nil
default:
return handleNotFound(w, r)
}
}
func handleUI(w http.ResponseWriter, r *http.Request) error {
if _, err := ParseCookie(r); err != nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return err
}
switch r.URL.Path {
case "/":
w.Write(htmlIndex)
return nil
case "/chatbot":
w.Write(htmlChatBot)
return nil
default:
return handleNotFound(w, r)
}
}
type Cookie struct {
Name string
}
func ParseCookie(r *http.Request) (Cookie, error) {
if r.URL.Path != "/login" {
return parseCookieFromCookie(r)
}
cookie := Cookie{
Name: r.PostForm.Get("Name"),
}
return cookie, cookie.Verify()
}
func parseCookieFromCookie(r *http.Request) (Cookie, error) {
cookie, err := r.Cookie("root")
if err != nil {
return Cookie{}, err
}
decoded, err := base64.URLEncoding.DecodeString(cookie.Value)
if err != nil {
return Cookie{}, err
}
var result Cookie
if err := json.Unmarshal(decoded, &result); err != nil {
return Cookie{}, err
}
return result, result.Verify()
}
func (cookie Cookie) Verify() error {
if cookie.Name == "" {
return fmt.Errorf("incomplete cookie")
}
return nil
}
func (cookie Cookie) Serialize(w http.ResponseWriter) {
b, _ := json.Marshal(cookie)
encoded := base64.URLEncoding.EncodeToString(b)
c := &http.Cookie{
Name: "root",
Value: encoded,
}
http.SetCookie(w, c)
}
func handleAPI(w http.ResponseWriter, r *http.Request) error {
if _, err := ParseCookie(r); err != nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return err
}
switch r.URL.Path {
case "/api/v0/chatbot":
return handleAPIChatBot(w, r)
default:
return handleNotFound(w, r)
}
}
func handleNotFound(w http.ResponseWriter, r *http.Request) error {
http.NotFound(w, r)
return fmt.Errorf("not found: %s %s", r.Method, r.URL.Path)
}
func handleAPIChatBot(w http.ResponseWriter, r *http.Request) error {
return errors.New("not impl")
}