-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_server.go
62 lines (48 loc) · 1.43 KB
/
cmd_server.go
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
56
57
58
59
60
61
62
package main
import (
"fmt"
"net/http"
"net/url"
"github.com/philippgille/chromem-go"
"github.com/shopwarelabs/copilot-extension/agent"
"github.com/shopwarelabs/copilot-extension/config"
"github.com/shopwarelabs/copilot-extension/oauth"
"github.com/spf13/cobra"
)
var serverCmd = &cobra.Command{
Use: "server",
Short: "Starts the server",
RunE: func(cmd *cobra.Command, args []string) error {
pubKey, err := fetchPublicKey()
if err != nil {
return fmt.Errorf("failed to fetch public key: %w", err)
}
config, err := config.New()
if err != nil {
return fmt.Errorf("error fetching config: %w", err)
}
me, err := url.Parse(config.FQDN)
if err != nil {
return fmt.Errorf("unable to parse HOST environment variable: %w", err)
}
db, err := chromem.NewPersistentDB("./db", true)
if err != nil {
return err
}
collection, err := db.GetOrCreateCollection("shopware_1", nil, nil)
if err != nil {
return err
}
me.Path = "auth/callback"
oauthService := oauth.NewService(config.ClientID, config.ClientSecret, me.String())
http.HandleFunc("/auth/authorization", oauthService.PreAuth)
http.HandleFunc("/auth/callback", oauthService.PostAuth)
agentService := agent.NewService(pubKey, collection)
http.HandleFunc("/agent", agentService.ChatCompletion)
fmt.Println("Listening on port", config.Port)
return http.ListenAndServe(":"+config.Port, nil)
},
}
func init() {
rootCmd.AddCommand(serverCmd)
}