-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
56 lines (46 loc) · 1.21 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gptscript-ai/datasets/pkg/tools"
)
func main() {
if os.Getenv("PORT") == "" {
fmt.Println("PORT is not set")
os.Exit(1)
}
mux := http.NewServeMux()
mux.HandleFunc("/addElements", tools.AddElements)
mux.HandleFunc("/getAllElements", tools.GetAllElements)
mux.HandleFunc("/listElements", tools.ListElements)
mux.HandleFunc("/getElement", tools.GetElement)
mux.HandleFunc("/listDatasets", tools.ListDatasets)
mux.HandleFunc("/outputFilter", tools.OutputFilter)
mux.HandleFunc("/", health)
srv := &http.Server{
Addr: "127.0.0.1:" + os.Getenv("PORT"),
Handler: mux,
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
fmt.Printf("error: %v\n", err)
}
}()
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
fmt.Printf("server forced to shut down: %v\n", err)
}
}
func health(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("ok"))
}