diff --git a/.gitignore b/.gitignore index 2308e69..c32b417 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ # vendor/ .env -/config \ No newline at end of file +/config +/soke-vise/ \ No newline at end of file diff --git a/controller/discord.go b/controller/discord.go new file mode 100644 index 0000000..170f709 --- /dev/null +++ b/controller/discord.go @@ -0,0 +1 @@ +// MAKE A DISCORD SOURCE. \ No newline at end of file diff --git a/controller/elastic.go b/controller/elastic.go index 7eeb4bd..3d9b27e 100644 --- a/controller/elastic.go +++ b/controller/elastic.go @@ -1,6 +1,9 @@ package elastic import ( + "bytes" + "context" + "encoding/json" "fmt" "log" "sokemotor/models" @@ -9,6 +12,7 @@ import ( "time" "github.com/elastic/go-elasticsearch/v7" + "github.com/elastic/go-elasticsearch/v7/esapi" ) var client *elasticsearch.Client @@ -55,23 +59,55 @@ func Client() *elasticsearch.Client { func createIndex(mapping string) error { - res, err := client.Indices.Exists([]string{models.IndexName}) + exists, err := client.Indices.Exists([]string{models.IndexName}) if err != nil { return err } + var res *esapi.Response // Index not found, so Creating one. - if res.IsError() { + if exists.IsError() { res, err = client.Indices.Create("myindex", client.Indices.Create.WithBody(strings.NewReader(mapping))) log.Println("INDEX CREATED") } + // Index Creation Error. if err != nil { return err + } else if res.IsError() { return fmt.Errorf("error: %s", res) } return nil } + +// Inserts the given htmlDocument into the ES Index. +func InsertIntoIndex(data models.HtmlDocument) error { + + dataBytes, err := json.Marshal(data) + + if err != nil { + return err + } + + req := esapi.IndexRequest{ + Index: "test", + Body: bytes.NewReader(dataBytes), + Refresh: "true", + } + + res, err := req.Do(context.Background(), client) + defer res.Body.Close() + + if err != nil { + return err + } + + if res.IsError() { + return fmt.Errorf("Error Inserting into Index") + } + + return nil +} diff --git a/main.go b/main.go index f369707..23efbfa 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ func main() { } func searchHandler(c *gin.Context) { + } func documentHandler(c *gin.Context) { @@ -35,13 +36,21 @@ func processHTML(c *gin.Context) { err := c.Bind(&v) if err != nil { - log.Printf("Error %v", err) - } else { - log.Printf("Recevied: %v", v.Url) + log.Printf("indexHTML Error: %v", err) + return + } + + err = elastic.InsertIntoIndex(v) + + if err != nil { + log.Printf("indexHTML Error: %v", err) + return } + // Acknowledge that document is created. c.JSON(http.StatusCreated, gin.H{ "url": v.Url, "Created": "true", }) + } diff --git a/models/searchResponse.go b/models/searchResponse.go new file mode 100644 index 0000000..519f378 --- /dev/null +++ b/models/searchResponse.go @@ -0,0 +1,8 @@ +package models + +type SearchResponse struct { + Url string `json:"url"` + Title string `json:"title"` + BoxContent string `json:"content"` + LastAccessed string `json:"lastaccessed"` +}