|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "regexp" |
| 7 | + |
| 8 | + // Packages |
| 9 | + router "github.com/mutablelogic/go-server/pkg/httprouter" |
| 10 | + indexer "github.com/mutablelogic/go-sqlite/pkg/indexer" |
| 11 | + |
| 12 | + // Namespace imports |
| 13 | + . "github.com/mutablelogic/go-server" |
| 14 | +) |
| 15 | + |
| 16 | +/////////////////////////////////////////////////////////////////////////////// |
| 17 | +// TYPES |
| 18 | + |
| 19 | +type PingResponse struct { |
| 20 | + Indexes []IndexResponse `json:"indexes"` |
| 21 | +} |
| 22 | + |
| 23 | +type IndexResponse struct { |
| 24 | + Name string `json:"name"` |
| 25 | + Path string `json:"path,omitempty"` |
| 26 | + Count int64 `json:"count,omitempty"` |
| 27 | + Modtime interface{} `json:"reindexed,omitempty"` |
| 28 | + Status string `json:"status,omitempty"` |
| 29 | +} |
| 30 | + |
| 31 | +/////////////////////////////////////////////////////////////////////////////// |
| 32 | +// ROUTES |
| 33 | + |
| 34 | +var ( |
| 35 | + reRoutePing = regexp.MustCompile(`^/?$`) |
| 36 | +) |
| 37 | + |
| 38 | +/////////////////////////////////////////////////////////////////////////////// |
| 39 | +// CONSTANTS |
| 40 | + |
| 41 | +const ( |
| 42 | + maxResultLimit = 1000 |
| 43 | +) |
| 44 | + |
| 45 | +/////////////////////////////////////////////////////////////////////////////// |
| 46 | +// LIFECYCLE |
| 47 | + |
| 48 | +func (p *plugin) AddHandlers(ctx context.Context, provider Provider) error { |
| 49 | + // Add handler for ping |
| 50 | + if err := provider.AddHandlerFuncEx(ctx, reRoutePing, p.ServePing); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + // Return success |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +/////////////////////////////////////////////////////////////////////////////// |
| 59 | +// HANDLERS |
| 60 | + |
| 61 | +func (p *plugin) ServePing(w http.ResponseWriter, req *http.Request) { |
| 62 | + // Get a connection |
| 63 | + conn := p.pool.Get() |
| 64 | + if conn == nil { |
| 65 | + router.ServeError(w, http.StatusBadGateway, "No connection") |
| 66 | + return |
| 67 | + } |
| 68 | + defer p.pool.Put(conn) |
| 69 | + |
| 70 | + // Retrieve indexes with count of documents in each |
| 71 | + index, err := indexer.ListIndexWithCount(req.Context(), conn, p.store.Schema()) |
| 72 | + if err != nil { |
| 73 | + router.ServeError(w, http.StatusBadGateway, err.Error()) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + // Add known indexes to the response - these may not yet have any rows in the |
| 78 | + // database |
| 79 | + for _, idx := range p.index { |
| 80 | + name := idx.Name() |
| 81 | + if _, exists := index[name]; !exists { |
| 82 | + index[name] = 0 |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Populate response |
| 87 | + response := PingResponse{ |
| 88 | + Indexes: make([]IndexResponse, 0, len(index)), |
| 89 | + } |
| 90 | + for name, count := range index { |
| 91 | + response.Indexes = append(response.Indexes, IndexResponse{ |
| 92 | + Name: name, |
| 93 | + Count: count, |
| 94 | + Path: p.pathForIndex(name), |
| 95 | + Modtime: p.modtimeForIndex(name), |
| 96 | + }) |
| 97 | + } |
| 98 | + // Now add all indexes into the response, adding their modtime |
| 99 | + |
| 100 | + // Serve response |
| 101 | + router.ServeJSON(w, response, http.StatusOK, 2) |
| 102 | +} |
| 103 | + |
| 104 | +/////////////////////////////////////////////////////////////////////////////// |
| 105 | +// PRIVATE METHODS |
| 106 | + |
| 107 | +func (p *plugin) pathForIndex(name string) string { |
| 108 | + if idx, exists := p.index[name]; exists { |
| 109 | + return idx.Path() |
| 110 | + } else { |
| 111 | + return "" |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func (p *plugin) modtimeForIndex(name string) interface{} { |
| 116 | + if t, exists := p.modtime[name]; exists && t.IsZero() == false { |
| 117 | + return t |
| 118 | + } else { |
| 119 | + return nil |
| 120 | + } |
| 121 | +} |
0 commit comments