-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_server.go
57 lines (44 loc) · 1.34 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
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"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)
}
cfg, err := config.New()
if err != nil {
return fmt.Errorf("error fetching config: %w", err)
}
me, err := url.Parse(cfg.FQDN)
if err != nil {
return fmt.Errorf("unable to parse HOST environment variable: %w", err)
}
collection, err := config.GetCollection(cfg)
if err != nil {
return fmt.Errorf("failed to get collection: %w", err)
}
me.Path = "auth/callback"
oauthService := oauth.NewService(cfg.ClientID, cfg.ClientSecret, me.String())
http.HandleFunc("/auth/authorization", oauthService.PreAuth)
http.HandleFunc("/auth/callback", oauthService.PostAuth)
agentService := agent.NewService(pubKey, collection, os.Getenv("DEBUG") == "true")
http.HandleFunc("/agent", agentService.ChatCompletion)
fmt.Println("Listening on port 8000")
return http.ListenAndServe(":8000", nil)
},
}
func init() {
rootCmd.AddCommand(serverCmd)
}