Skip to content

Commit

Permalink
More refactor.
Browse files Browse the repository at this point in the history
Signed-off-by: Daksh Chauhan <dak-x@outlook.com>
  • Loading branch information
dak-x committed Aug 3, 2021
1 parent d4d2aef commit ca625b6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
35 changes: 25 additions & 10 deletions elastic/elastic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package elastic

import (
"fmt"
"log"
"sokemotor/models"
"sokemotor/utils"
"strings"
"time"

"github.com/elastic/go-elasticsearch/v7"
Expand All @@ -19,14 +23,11 @@ func init() {

client, err := elasticsearch.NewClient(esCfg)

if err != nil {
log.Fatalf("Cannot create Client: %v", err)
}
utils.HandleFatalError("Error creating client", err)

esInfo, err := client.Info()

var numRetries int = 0

for err != nil {
// Wait for the instane to get up and running.
time.Sleep(3 * time.Second)
Expand All @@ -37,15 +38,29 @@ func init() {
esInfo, err = client.Info()
}

if err != nil {
log.Fatalf("Elastic is not running %s", err)
}
utils.HandleFatalError("Coundn't connect to elastic instance", err)

log.Printf("%v", esInfo)

// Default Mapping the string.
err = createIndex(models.HtmlDocumentMapping)

log.Printf("ELASTIC IS UP: %v\n", esInfo)
log.Printf("STARTING SOKEMOTOR SERVICE \n")
utils.HandleFatalError("Index creation error:", err)

}

func GetEsClint() *elasticsearch.Client {
func GetEsClient() *elasticsearch.Client {
return client
}

func createIndex(mapping string) error {
res, err := client.Indices.Create("index01", client.Indices.Create.WithBody(strings.NewReader(mapping)))

if err != nil {
return err
}
if res.IsError() {
return fmt.Errorf("error: %s", res)
}
return nil
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ go 1.16
require github.com/gin-gonic/gin v1.7.2

require github.com/elastic/go-elasticsearch/v7 v7.13.1

11 changes: 4 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package main
import (
"log"
"net/http"

"sokemotor/elastic"
"sokemotor/models"

"github.com/gin-gonic/gin"
)

type htmlDocument struct {
Url string `json:"url"`
Dom string `json:"dom"`
}

func main() {

_ = elastic.GetEsClint()
_ = elastic.GetEsClient()

app := gin.Default()
app.GET("/search/:query", searchHandler)
Expand All @@ -33,7 +30,7 @@ func documentHandler(c *gin.Context) {

func processHtml(c *gin.Context) {

var v htmlDocument
var v models.HtmlDocument

err := c.Bind(&v)

Expand Down
15 changes: 15 additions & 0 deletions models/htmlDoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package models

type HtmlDocument struct {
Url string `json:"url"`
Dom string `json:"dom"`
}

// For elastic-search indexing
const HtmlDocumentMapping string = `"mappings": {
"properties": {
"url" : {"type" : ""},
"htmltext" : {"type" : "text"},
"time_of_registry" : {"type" : "date"},
}
}`
9 changes: 9 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package utils

import "log"

func HandleFatalError(logMessage string, err error) {
if err != nil {
log.Fatalf("%s %s", logMessage, err)
}
}

0 comments on commit ca625b6

Please sign in to comment.