I am writing a simple flask service that queries solr , get the results and renders it on results page. This is my first attempt at writing services using flask. Any suggestions would be appreciated.
I have following questions:
- I do not want to allow any other actions except get, what is the best way to handle that.
- Any suggestions on how to handle exceptions, currently referring to http://flask.pocoo.org/docs/0.12/errorhandling/.
Following is the structure of my application.
\webapp search.py solr_search.py \static -\css -\js \templates
solr_search.py
from SolrClient import SolrClient class SolrSearchFactory: def __init__(self,host : str, port:int): self.host = host self.port = port def create(self): print(self.host,self.port) return SolrClient("http://"+self.host+":"+self.port+"/solr") class SolrQuery: def __init__(self,solr_search_factory : SolrSearchFactory, index_name): self.solr_factory = solr_search_factory self.index = index_name self.instance = self.solr_factory.create() def query_solr(self,dict_q): return self.instance.query(self.index,dict_q)
search.py
dotenv_path = join(dirname(__file__), '.env') load_dotenv(dotenv_path) #dotenv.load() solr_host = os.environ.get('SOLR_HOST') solr_port = os.environ.get('SOLR_PORT') app = Flask(__name__) @app.route('/search/', methods=['get']) def search_solr(): q = request.args.get('q', None) # use default value repalce 'None' page = request.args.get('page', 0) size = request.args.get('size',10) so, sq = None, None so = SolrSearchFactory(solr_host,solr_port) sq = SolrQuery(so, 'techproducts') res = (sq.query_solr({ 'q':'name:Test', 'facet':True, 'fl':'id' })) return render_template('search.html', results=res.get_json(), query = q)
Thanks