2025-08-05 03:56:23 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2025-08-10 16:38:02 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
2025-08-05 03:56:23 +02:00
|
|
|
|
|
|
|
|
"github.com/coalaura/logger"
|
|
|
|
|
adapter "github.com/coalaura/logger/http"
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-18 05:05:07 +02:00
|
|
|
var Version = "dev"
|
2025-08-11 01:38:16 +02:00
|
|
|
|
2025-08-05 03:56:23 +02:00
|
|
|
var log = logger.New().DetectTerminal().WithOptions(logger.Options{
|
|
|
|
|
NoLevel: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
models, err := LoadModels()
|
|
|
|
|
log.MustPanic(err)
|
|
|
|
|
|
2025-08-11 01:59:26 +02:00
|
|
|
log.Info("Preparing router...")
|
2025-08-05 03:56:23 +02:00
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
|
|
r.Use(middleware.Recoverer)
|
|
|
|
|
r.Use(adapter.Middleware(log))
|
|
|
|
|
|
|
|
|
|
fs := http.FileServer(http.Dir("./static"))
|
2025-08-10 16:38:02 +02:00
|
|
|
r.Handle("/*", cache(http.StripPrefix("/", fs)))
|
2025-08-05 03:56:23 +02:00
|
|
|
|
2025-08-11 01:38:16 +02:00
|
|
|
r.Get("/-/data", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
RespondJson(w, http.StatusOK, map[string]any{
|
2025-08-16 17:18:48 +02:00
|
|
|
"authentication": env.Authentication.Enabled,
|
|
|
|
|
"authenticated": IsAuthenticated(r),
|
|
|
|
|
"search": env.Tokens.Exa != "",
|
|
|
|
|
"models": models,
|
2025-08-18 04:46:17 +02:00
|
|
|
"prompts": Prompts,
|
2025-08-16 17:18:48 +02:00
|
|
|
"version": Version,
|
2025-08-11 01:38:16 +02:00
|
|
|
})
|
2025-08-05 03:56:23 +02:00
|
|
|
})
|
|
|
|
|
|
2025-08-16 17:18:48 +02:00
|
|
|
r.Post("/-/auth", HandleAuthentication)
|
|
|
|
|
|
|
|
|
|
r.Group(func(gr chi.Router) {
|
|
|
|
|
gr.Use(Authenticate)
|
|
|
|
|
|
|
|
|
|
gr.Get("/-/stats/{id}", HandleStats)
|
|
|
|
|
gr.Post("/-/chat", HandleChat)
|
|
|
|
|
})
|
2025-08-05 03:56:23 +02:00
|
|
|
|
2025-08-11 01:59:26 +02:00
|
|
|
log.Info("Listening at http://localhost:3443/")
|
2025-08-05 03:56:23 +02:00
|
|
|
http.ListenAndServe(":3443", r)
|
|
|
|
|
}
|
2025-08-10 16:38:02 +02:00
|
|
|
|
|
|
|
|
func cache(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
path := strings.ToLower(r.URL.Path)
|
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
|
|
2025-08-11 00:15:58 +02:00
|
|
|
if ext == ".svg" || ext == ".ttf" || strings.HasSuffix(path, ".min.js") || strings.HasSuffix(path, ".min.css") {
|
2025-08-10 16:38:02 +02:00
|
|
|
w.Header().Set("Cache-Control", "public, max-age=3024000, immutable")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|