-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (47 loc) · 1.43 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
57
58
59
60
61
62
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/adapters/humachi"
"github.com/go-chi/chi/v5"
_ "github.com/danielgtaylor/huma/v2/formats/cbor"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
const version = "0.0.1"
func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&Tool{})
// Create a new router & API
router := chi.NewMux()
api := humachi.New(router, huma.DefaultConfig("Laridae", version))
huma.Get(api, "/list/tools", func(ctx context.Context, input *struct{}) (*ListToolOutput, error) {
resp := ListTools(*db)
out := ListToolOutput{Body: resp}
return &out, nil
})
huma.Post(api, "/ingest/tools", func(ctx context.Context, input *IngestToolInput) (*IngestToolOutput, error) {
req, err := json.Marshal(input)
if err != nil {
log.Println(err)
}
log.Println("request body: ", string(req))
log.Printf("Starting Ingest for URL" + input.Body.URL)
resp, err := IngestTool(*db, input.Body)
out := IngestToolOutput{Body: resp}
return &out, err
})
huma.Get(api, "/search/tools/{term}", func(ctx context.Context, input *SearchToolsInput) (*ListToolOutput, error) {
resp := SearchTools(*db, *input)
out := ListToolOutput{Body: resp}
return &out, nil
})
// Start the server!
http.ListenAndServe("127.0.0.1:8888", router)
}