Giter Site home page Giter Site logo

gocleo's People

Contributors

jamra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

gocleo's Issues

You shouldn't run a standalone webserver

After having played a while with your lib, it turns out that I like it but I think running a standalone web server is not a good idea. Because the most common use case is using your lib as part of a web applicaion. So you have to launch two web servers and patch your lib in order to allow cross domain AJAX requests (see issue #3), e.g. :

func main() {
  go cleo.InitAndRun("liste.txt", "7777", nil)  //go routine needed here to allow main to carry on
  http.HandleFunc("/home", homeHandler) //Without patching, my AJAX requests fail
  err := http.ListenAndServe(":9999", nil)
  if err != nil {
    panic(err)
  }
}

I think that you shouldn't start a standalone web server but offer additionnal HTTP handlers in the manner of expvar package (it adds its handlers by side effect) :

import _ "expvar"

The difference will be that the developper will have the responsability to run its own web server. Once started, the application importing gocleo will have gocleo's handlers.

In the lib, you should add an init function (called when gocleo package is imported) so as to register your handler(s):

func init() {
    http.HandleFunc("/cleo", searchHandler)
}

You should limit the dependencies of your lib (I love Gorilla toolkit but it's a bit underexploited here). And handle parameters (whether they are coming from a POST or a GET request) the classic way :

func Search(w http.ResponseWriter, r *http.Request) {
        query := r.FormValue("query")
        ...

Instead of :

func Search(w http.ResponseWriter, r *http.Request) {
        vars := mux.Vars(r)
        query := vars["query"]
        ...

Then, my application becomes :

import (
 "net/http"
 "github.com/jamra/gocleo"    //No side effect by import, because i still need to init the indexes
)

func main() {
  cleo.BuildIndexes("liste.txt", nil)   //No need to create a goroutine now (I renamed your func)
  http.HandleFunc("/home", homeHandler) //my AJAX requests work now
  err := http.ListenAndServe(":9999", nil)
  if err != nil {
    panic(err)
  }
}

I can now use the following URLs (with only one webserver) :
http://localhost:9999/home
http://localhost:9999/cleo?query=wonderful

By doing so, you don't need to solve the issue #3 anymore because the two web pages belong now to the same domain.

Remove debug messages

Your code is fast but you spoil it by printing debug messages to the console. Please remove these two lines (they are good for you when you debug, but useless in production):

fmt.Println("Query:", query)
...
fmt.Printf("The call took %v to run.\n", t1.Sub(t0))

Allow cross domain AJAX Requests

If you want to distribute your lib as a standalone web server (A), then you have to take into account that other developpers using it might have their own web server (B) in their go app. Please patch you lib to allow web pages served by B to request server A.

func Search(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    query := vars["query"]

    searchResult := CleoSearch(m.iIndex, m.fIndex, query)
    sort.Sort(ByScore{searchResult})
    myJson, _ := json.Marshal(searchResult)

    // allow cross domain AJAX requests
        w.Header().Set("Access-Control-Allow-Origin", "*")
    fmt.Fprintf(w, string(myJson))
}

Improve error handling

At key moments of the code, you should panic or log.Fatal, for example:

file, _ := os.Open(corpusPath)

You shouldn't ignore the error because the entire lib will not work from this point.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.