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
SOLR ?="multi-select-facet-demo"
.PHONY: build-api
build-api:
go build -v -o api
.PHONY: start-solr
start-solr: stop-solr
$(PODMAN) run -d -p 8983:8983 --name $(SOLR) solr:8 solr -c -f

37
main.go
View File

@@ -6,6 +6,8 @@ import (
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
@@ -31,7 +33,8 @@ func main() {
baseURL := "http://localhost:8983"
solrClient := solr.NewJSONClient(baseURL)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if *createCollection {
log.Println("creating collection...")
@@ -91,15 +94,35 @@ func main() {
})
addr := ":8081"
log.Printf("listening on %s\n", addr)
err := http.ListenAndServe(addr, r)
if err != nil {
srvr := &http.Server{
Addr: addr,
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)
}
}
func initSolrSchema(ctx context.Context, collection string, solrClient solr.Client) (err error) {
// Auto-suggest field type
fieldTypes := []solr.FieldType{
// approach #1
@@ -179,12 +202,10 @@ func initSolrSchema(ctx context.Context, collection string, solrClient solr.Clie
},
}
for _, fieldType := range fieldTypes {
err = solrClient.AddFieldTypes(ctx, collection, fieldType)
err = solrClient.AddFieldTypes(ctx, collection, fieldTypes...)
if err != nil {
return errors.Wrap(err, "add field type")
}
}
// define the fields
fields := []solr.Field{