-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (46 loc) · 1.32 KB
/
main.go
File metadata and controls
55 lines (46 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
"github.com/supabase-community/supabase-go"
)
func main() {
config, err := setConfig()
if err != nil {
log.Fatal("Failed to set configuration: ", err)
}
sb, err := supabase.NewClient(config.SUPABASE_URL, config.SUPABASE_KEY, &supabase.ClientOptions{})
if err != nil {
log.Println("Failed to initalize the client: ", err)
}
oai := openai.NewClient(option.WithAPIKey(config.OPENAI_API_KEY))
srv := &http.Server{
Addr: ":8090",
Handler: NewApp(sb, &oai, &config).Handler,
ReadHeaderTimeout: 3 * time.Second,
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second,
}
log.Print("Starting server on :8090\n")
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %v", err)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("graceful shutdown failed: %v", err)
}
}