changes bootstrap mechanism
This commit is contained in:
@@ -6,14 +6,13 @@ WORKDIR /
|
||||
COPY go.mod ./
|
||||
COPY go.sum ./
|
||||
COPY migrations ./migrations
|
||||
RUN tar -xvf migrations/initial_data/cities500.tar.gz -C migrations/initial_data
|
||||
|
||||
RUN go mod download
|
||||
|
||||
COPY *.go ./
|
||||
|
||||
RUN go build -o /pocketbase
|
||||
RUN /pocketbase migrate
|
||||
RUN /pocketbase seed
|
||||
|
||||
ENV MEILI_URL=http://localhost:7700
|
||||
ENV MEILI_MASTER_KEY=
|
||||
|
||||
@@ -3,7 +3,9 @@ module pocketbase
|
||||
go 1.22.0
|
||||
|
||||
require (
|
||||
github.com/labstack/echo/v5 v5.0.0-20230722203903-ec5b858dab61
|
||||
github.com/meilisearch/meilisearch-go v0.26.2
|
||||
github.com/pocketbase/dbx v1.10.1
|
||||
github.com/pocketbase/pocketbase v0.21.3
|
||||
)
|
||||
|
||||
@@ -50,13 +52,11 @@ require (
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
|
||||
github.com/klauspost/compress v1.15.6 // indirect
|
||||
github.com/labstack/echo/v5 v5.0.0-20230722203903-ec5b858dab61 // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.19 // indirect
|
||||
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
|
||||
github.com/pocketbase/dbx v1.10.1 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/spf13/cast v1.6.0 // indirect
|
||||
github.com/spf13/cobra v1.8.0 // indirect
|
||||
|
||||
125
db/main.go
125
db/main.go
@@ -1,11 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/meilisearch/meilisearch-go"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
@@ -13,41 +14,10 @@ import (
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"github.com/pocketbase/pocketbase/plugins/migratecmd"
|
||||
"github.com/pocketbase/pocketbase/tools/filesystem"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
_ "pocketbase/migrations"
|
||||
)
|
||||
|
||||
func indexRecord(r *models.Record, client *meilisearch.Client) error {
|
||||
documents := []map[string]interface{}{
|
||||
{
|
||||
"id": r.Id,
|
||||
"author": r.GetString("author"),
|
||||
"name": r.GetString("name"),
|
||||
"description": r.GetString("description"),
|
||||
"location": r.GetString("location"),
|
||||
"distance": r.GetFloat("distance"),
|
||||
"elevation_gain": r.GetFloat("elevation_gain"),
|
||||
"duration": r.GetFloat("duration"),
|
||||
"difficulty": r.Get("difficulty"),
|
||||
"category": r.Get("category"),
|
||||
"completed": len(r.GetStringSlice("summit_logs")) > 0,
|
||||
"created": r.GetDateTime("created"),
|
||||
"public": r.GetBool("public"),
|
||||
"_geo": map[string]float64{
|
||||
"lat": r.GetFloat("lat"),
|
||||
"lng": r.GetFloat("lon"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := client.Index("trails").AddDocuments(documents); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := pocketbase.New()
|
||||
|
||||
@@ -56,26 +26,6 @@ func main() {
|
||||
Automigrate: true,
|
||||
})
|
||||
|
||||
app.RootCmd.AddCommand(&cobra.Command{
|
||||
Use: "seed",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
collection, _ := app.Dao().FindCollectionByNameOrId("categories")
|
||||
|
||||
categories := []string{"Hiking", "Walking", "Climbing", "Skiing", "Canoeing", "Biking"}
|
||||
for _, element := range categories {
|
||||
record := models.NewRecord(collection)
|
||||
form := forms.NewRecordUpsert(app, record)
|
||||
form.LoadData(map[string]any{
|
||||
"name": element,
|
||||
})
|
||||
f, _ := filesystem.NewFileFromPath("migrations/initial_data/" + strings.ToLower(element) + ".jpg")
|
||||
form.AddFiles("img", f)
|
||||
form.Submit()
|
||||
}
|
||||
|
||||
},
|
||||
})
|
||||
|
||||
client := meilisearch.NewClient(meilisearch.ClientConfig{
|
||||
Host: os.Getenv("MEILI_URL"),
|
||||
APIKey: os.Getenv("MEILI_MASTER_KEY"),
|
||||
@@ -84,35 +34,14 @@ func main() {
|
||||
app.OnRecordAfterCreateRequest("users").Add(func(e *core.RecordCreateEvent) error {
|
||||
userId := e.Record.GetId()
|
||||
|
||||
apiKeyUid := ""
|
||||
apiKey := ""
|
||||
|
||||
if keys, err := client.GetKeys(nil); err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
for _, k := range keys.Results {
|
||||
if k.Name == "Default Search API Key" {
|
||||
apiKeyUid = k.UID
|
||||
apiKey = k.Key
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(apiKey) == 0 || len(apiKeyUid) == 0 {
|
||||
return errors.New("unable to locate meilisearch API key")
|
||||
}
|
||||
|
||||
searchRules := map[string]interface{}{
|
||||
"cities500": map[string]string{},
|
||||
"trails": map[string]string{
|
||||
"filter": "public = true OR author = " + userId,
|
||||
},
|
||||
}
|
||||
options := &meilisearch.TenantTokenOptions{
|
||||
APIKey: apiKey,
|
||||
}
|
||||
|
||||
if token, err := client.GenerateTenantToken(apiKeyUid, searchRules, options); err != nil {
|
||||
if token, err := generateMeilisearchToken(searchRules, client); err != nil {
|
||||
return err
|
||||
} else {
|
||||
e.Record.Set("token", token)
|
||||
@@ -146,6 +75,54 @@ func main() {
|
||||
return nil
|
||||
})
|
||||
|
||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
e.Router.GET("/public/search/token", func(c echo.Context) error {
|
||||
searchRules := map[string]interface{}{
|
||||
"cities500": map[string]string{},
|
||||
"trails": map[string]string{
|
||||
"filter": "public = true",
|
||||
},
|
||||
}
|
||||
|
||||
if token, err := generateMeilisearchToken(searchRules, client); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return c.JSON(http.StatusOK, map[string]string{"token": token})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
// bootstrap pocketbase
|
||||
query := app.Dao().RecordQuery("categories")
|
||||
records := []*models.Record{}
|
||||
|
||||
if err := query.All(&records); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(records) == 0 {
|
||||
collection, _ := app.Dao().FindCollectionByNameOrId("categories")
|
||||
|
||||
categories := []string{"Hiking", "Walking", "Climbing", "Skiing", "Canoeing", "Biking"}
|
||||
for _, element := range categories {
|
||||
record := models.NewRecord(collection)
|
||||
form := forms.NewRecordUpsert(app, record)
|
||||
form.LoadData(map[string]any{
|
||||
"name": element,
|
||||
})
|
||||
f, _ := filesystem.NewFileFromPath("migrations/initial_data/" + strings.ToLower(element) + ".jpg")
|
||||
form.AddFiles("img", f)
|
||||
form.Submit()
|
||||
}
|
||||
}
|
||||
|
||||
// bootstrap meilisearch
|
||||
if err := bootstrapMeilisearch(client); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := app.Start(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
154
db/meilisearch.go
Normal file
154
db/meilisearch.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/meilisearch/meilisearch-go"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
)
|
||||
|
||||
func hasIndex(index string, client *meilisearch.Client) (resp bool, err error) {
|
||||
|
||||
if _, err := client.GetIndex(index); err != nil {
|
||||
if err.(*meilisearch.Error).StatusCode == 404 {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func bootstrapMeilisearch(client *meilisearch.Client) error {
|
||||
indexExists, err := hasIndex("cities500", client)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !indexExists {
|
||||
|
||||
jsonFile, err := os.Open("migrations/initial_data/cities500.json")
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer jsonFile.Close()
|
||||
|
||||
byteValue, err := io.ReadAll(jsonFile)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var cities []map[string]interface{}
|
||||
json.Unmarshal(byteValue, &cities)
|
||||
|
||||
_, err = client.CreateIndex(&meilisearch.IndexConfig{
|
||||
Uid: "cities500",
|
||||
PrimaryKey: "id",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
settings := meilisearch.Settings{
|
||||
SortableAttributes: []string{
|
||||
"_geo",
|
||||
},
|
||||
FilterableAttributes: []string{
|
||||
"_geo",
|
||||
},
|
||||
}
|
||||
_, err = client.Index("cities500").UpdateSettings(&settings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = client.Index("cities500").AddDocuments(cities)
|
||||
if err != nil {
|
||||
return (err)
|
||||
}
|
||||
}
|
||||
|
||||
indexExists, err = hasIndex("trails", client)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !indexExists {
|
||||
_, err = client.CreateIndex(&meilisearch.IndexConfig{
|
||||
Uid: "trails",
|
||||
PrimaryKey: "id",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
settings := meilisearch.Settings{
|
||||
SortableAttributes: []string{
|
||||
"name", "distance", "elevation_gain", "difficulty", "created",
|
||||
},
|
||||
FilterableAttributes: []string{
|
||||
"category", "difficulty", "distance", "elevation_gain", "completed", "_geo", "public", "author",
|
||||
},
|
||||
}
|
||||
_, err = client.Index("trails").UpdateSettings(&settings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func indexRecord(r *models.Record, client *meilisearch.Client) error {
|
||||
documents := []map[string]interface{}{
|
||||
{
|
||||
"id": r.Id,
|
||||
"author": r.GetString("author"),
|
||||
"name": r.GetString("name"),
|
||||
"description": r.GetString("description"),
|
||||
"location": r.GetString("location"),
|
||||
"distance": r.GetFloat("distance"),
|
||||
"elevation_gain": r.GetFloat("elevation_gain"),
|
||||
"duration": r.GetFloat("duration"),
|
||||
"difficulty": r.Get("difficulty"),
|
||||
"category": r.Get("category"),
|
||||
"completed": len(r.GetStringSlice("summit_logs")) > 0,
|
||||
"created": r.GetDateTime("created"),
|
||||
"public": r.GetBool("public"),
|
||||
"_geo": map[string]float64{
|
||||
"lat": r.GetFloat("lat"),
|
||||
"lng": r.GetFloat("lon"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := client.Index("trails").AddDocuments(documents); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateMeilisearchToken(rules map[string]interface{}, client *meilisearch.Client) (resp string, err error) {
|
||||
apiKeyUid := ""
|
||||
apiKey := ""
|
||||
|
||||
if keys, err := client.GetKeys(nil); err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
for _, k := range keys.Results {
|
||||
if k.Name == "Default Search API Key" {
|
||||
apiKeyUid = k.UID
|
||||
apiKey = k.Key
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(apiKey) == 0 || len(apiKeyUid) == 0 {
|
||||
return "", errors.New("unable to locate meilisearch API key")
|
||||
}
|
||||
|
||||
options := &meilisearch.TenantTokenOptions{
|
||||
APIKey: apiKey,
|
||||
}
|
||||
|
||||
return client.GenerateTenantToken(apiKeyUid, rules, options)
|
||||
}
|
||||
Reference in New Issue
Block a user