1
0
mirror of https://github.com/stevenferrer/multi-select-facet.git synced 2025-11-23 21:54:45 +02:00

added context cancellation

This commit is contained in:
Steven Ferrer
2021-02-10 16:27:25 +08:00
parent 973d09768b
commit a0f92879ff
2 changed files with 35 additions and 10 deletions

View File

@@ -1,6 +1,10 @@
PODMAN ?= podman PODMAN ?= podman
SOLR ?="multi-select-facet-demo" SOLR ?="multi-select-facet-demo"
.PHONY: build-api
build-api:
go build -v -o api
.PHONY: start-solr .PHONY: start-solr
start-solr: stop-solr start-solr: stop-solr
$(PODMAN) run -d -p 8983:8983 --name $(SOLR) solr:8 solr -c -f $(PODMAN) run -d -p 8983:8983 --name $(SOLR) solr:8 solr -c -f

41
main.go
View File

@@ -6,6 +6,8 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"os/signal"
"time"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
@@ -31,7 +33,8 @@ func main() {
baseURL := "http://localhost:8983" baseURL := "http://localhost:8983"
solrClient := solr.NewJSONClient(baseURL) solrClient := solr.NewJSONClient(baseURL)
ctx := context.Background() ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if *createCollection { if *createCollection {
log.Println("creating collection...") log.Println("creating collection...")
@@ -91,15 +94,35 @@ func main() {
}) })
addr := ":8081" addr := ":8081"
log.Printf("listening on %s\n", addr) srvr := &http.Server{
err := http.ListenAndServe(addr, r) Addr: addr,
if err != nil { Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
// Start the server
go func() {
log.Printf("Server listening on %s\n", addr)
if err := srvr.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
// setup signal capturing
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// wait for SIGINT (pkill -2)
<-c
if err := srvr.Shutdown(ctx); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
func initSolrSchema(ctx context.Context, collection string, solrClient solr.Client) (err error) { func initSolrSchema(ctx context.Context, collection string, solrClient solr.Client) (err error) {
// Auto-suggest field type // Auto-suggest field type
fieldTypes := []solr.FieldType{ fieldTypes := []solr.FieldType{
// approach #1 // approach #1
@@ -179,11 +202,9 @@ func initSolrSchema(ctx context.Context, collection string, solrClient solr.Clie
}, },
} }
for _, fieldType := range fieldTypes { err = solrClient.AddFieldTypes(ctx, collection, fieldTypes...)
err = solrClient.AddFieldTypes(ctx, collection, fieldType) if err != nil {
if err != nil { return errors.Wrap(err, "add field type")
return errors.Wrap(err, "add field type")
}
} }
// define the fields // define the fields