Re: How to serve minified React with Cherrypy?

Edvin Beqari <[email protected]> Sun, 26 May 2019 06:13:45 -0700 (PDT)
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
------=_Part_1462_1114716379.1558876425207
Content-Type: multipart/alternative; 
	boundary="----=_Part_1463_750607524.1558876425207"

------=_Part_1463_750607524.1558876425207
Content-Type: text/plain; charset="UTF-8"

Thank you Cameron. I went down the rabbit hole of webpack and browsify but 
I just could not get the config right. 

Instead, I I built the client with the default create-react-app 
configuration (chunks the javascript and creates a template) and I simply 
pointed the server to the build directory. 

class Server(object):

@cherrypy.expose
def index(self):
return open('../cococlient/build/index.html')

def cors():
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"

if __name__ == '__main__':
conf = {
'/': {
'tools.staticdir.root': os.path.abspath(os.getcwd())
},
'/catalog': {
'tools.CORS.on': True,
'tools.response_headers.on': True
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': '../cococlient/build/static'
}
}

server = Server()
server.catalog = Catalog()
cherrypy.tools.CORS = cherrypy.Tool('before_handler', cors)

cherrypy.quickstart(server, '/', conf)

-- 
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/8e8edd91-5877-401c-9a52-ab377a8d742f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

------=_Part_1463_750607524.1558876425207
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Thank you Cameron. I went down the rabbit hole of webpack =
and browsify but I just could not get the config right.=C2=A0<div><br></div=
><div>Instead, I I built the client with the default create-react-app confi=
guration (chunks the javascript and creates a template) and I simply pointe=
d the server to the build directory.=C2=A0</div><div><br></div><div><div st=
yle=3D"color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-f=
amily: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; line-=
height: 19px; white-space: pre;"><div><span style=3D"color: #569cd6;">class=
</span> <span style=3D"color: #4ec9b0;">Server</span>(<span style=3D"color:=
 #4ec9b0;">object</span>):</div><br><div>  <span style=3D"color: #dcdcaa;">=
@cherrypy.expose</span></div><div>  <span style=3D"color: #569cd6;">def</sp=
an> <span style=3D"color: #dcdcaa;">index</span>(<span style=3D"color: #9cd=
cfe;">self</span>):</div><div>    <span style=3D"color: #c586c0;">return</s=
pan> <span style=3D"color: #dcdcaa;">open</span>(<span style=3D"color: #ce9=
178;">&#39;../cococlient/build/index.html&#39;</span>)</div><br><div><span =
style=3D"color: #569cd6;">def</span> <span style=3D"color: #dcdcaa;">cors</=
span>():</div><div>  cherrypy.response.headers[<span style=3D"color: #ce917=
8;">&quot;Access-Control-Allow-Origin&quot;</span>] =3D <span style=3D"colo=
r: #ce9178;">&quot;*&quot;</span></div><br><div><span style=3D"color: #c586=
c0;">if</span> <span style=3D"color: #9cdcfe;">__name__</span> =3D=3D <span=
 style=3D"color: #ce9178;">&#39;__main__&#39;</span>:</div><div>    conf =
=3D {</div><div>        <span style=3D"color: #ce9178;">&#39;/&#39;</span>:=
 {</div><div>            <span style=3D"color: #ce9178;">&#39;tools.staticd=
ir.root&#39;</span>: os.path.abspath(os.getcwd())</div><div>        },</div=
><div>        <span style=3D"color: #ce9178;">&#39;/catalog&#39;</span>: {<=
/div><div>            <span style=3D"color: #ce9178;">&#39;tools.CORS.on&#3=
9;</span>: <span style=3D"color: #569cd6;">True</span>,</div><div>         =
   <span style=3D"color: #ce9178;">&#39;tools.response_headers.on&#39;</spa=
n>: <span style=3D"color: #569cd6;">True</span></div><div>        },</div><=
div>        <span style=3D"color: #ce9178;">&#39;/static&#39;</span>: {</di=
v><div>            <span style=3D"color: #ce9178;">&#39;tools.staticdir.on&=
#39;</span>: <span style=3D"color: #569cd6;">True</span>,</div><div>       =
     <span style=3D"color: #ce9178;">&#39;tools.staticdir.dir&#39;</span>: =
<span style=3D"color: #ce9178;">&#39;../cococlient/build/static&#39;</span>=
</div><div>        }</div><div>    }</div><br><div>    server =3D Server()<=
/div><div>    server.catalog =3D Catalog()</div><div>    cherrypy.tools.COR=
S =3D cherrypy.Tool(<span style=3D"color: #ce9178;">&#39;before_handler&#39=
;</span>, cors)</div><div><br></div><div>    cherrypy.quickstart(server, <s=
pan style=3D"color: #ce9178;">&#39;/&#39;</span>, conf)</div></div></div></=
div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;cherrypy-users&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]">cher=
rypy-users+unsubscribe-/[email protected]</a>.<br />
To post to this group, send email to <a href=3D"mailto:cherrypy-users@googl=
egroups.com">cherrypy-users-/[email protected]</a>.<br />
Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use=
rs">https://groups.google.com/group/cherrypy-users</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/d/msgid/cherrypy-users/8e8edd91-5877-401c-9a52-ab377a8d742f%40googlegrou=
ps.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/=
msgid/cherrypy-users/8e8edd91-5877-401c-9a52-ab377a8d742f%40googlegroups.co=
m</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />

------=_Part_1463_750607524.1558876425207--

------=_Part_1462_1114716379.1558876425207--