import requests
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http.response import HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic import TemplateView
from graphene_django.views import GraphQLView


class PrivateGraphQLView(LoginRequiredMixin, GraphQLView):
    pass


@ensure_csrf_cookie
def home(request):
    if settings.SERVE_VIA_WEBPACK:
        try:
            res = requests.get('http://localhost:8080/{}'.format(request.get_full_path()))
            headers = res.headers
            content_type = headers.get('content-type', 'text/html')
            return HttpResponse(res.text, content_type=content_type)
        except Exception as e:
            print('Can not connect to dev server at http://localhost:8080:', e)

    return render(request, 'index.html', {})
