Go server serving a react-based website using server-side rendering powered by V8.
This repo is forked from the awesome go-starter-kit.
I forked that project and am modifying to get a better understanding of how it works. And use some of the tools I'm more familiar with. There are many changes, but some of the major ones include:
- rework the JS rendering engine to use either v8 or duktape On windows, only duktape is allowed and used by default.
- simplify the server code to be an easier-to-understand example
- use modd instead of on for running the server.
- use go-rice instead of go-bindata for embedding the data.
- use govendor rather than srlt
Make sure you have:
- golang
- node.js with npm, only to build the application bundle at compile time. For windows, it's very important that you have a recent npm, otherwise the npm install will fail because the directory paths are too long.
- GNU make
$ git clone git@github.com:augustoroman/go-react-v8-ssr.git $GOPATH/src/github.com/<username>/<project>
$ cd $GOPATH/src/github.com/<username>/<project>
$ go get -u github.com/kardianos/govendor
$ go get -u github.com/GeertJohan/go.rice/rice
$ go get -u github.com/cortesi/modd
$ govendor sync
$ npm install
See instructions at https://github.com/augustoroman/v8
$ vendor/github.com/augustoroman/v8/symlink.sh <V8-DIR>
Start dev server:
$ modd
that's it. Open http://localhost:5001/(if you use default port) at your browser. Now you ready to start coding your awesome project.
Install dependencies and type NODE_ENV=production make build
. This rule produces the production webpack build and that is embedded into the go server, then builds the server. You can find the result at ./bin/server
.
$ tree server
server
├── main.go <-- main function declared here
├── react-v8.go
├── bindata.go <-- this file is gitignored, it will appear at compile time
└── data
├── static
| └── build <-- this dir is populated by webpack automatically
└── templates
└── react.html
The ./server/
is flat golang package.
It's simple React application
$ tree client
client
├── actions.js
├── components
│ ├── app
│ │ ├── favicon.ico
│ │ ├── index.js
│ │ └── styles.css
│ ├── homepage
│ │ ├── index.js
│ │ └── styles.css
│ ├── not-found
│ │ ├── index.js
│ │ └── styles.css
│ └── usage
│ ├── index.js
│ └── styles.css
├── css
│ ├── funcs.js
│ ├── global.css
│ ├── index.js
│ └── vars.js
├── index.js <-- main function declared here
├── reducers.js
├── router
│ ├── index.js
│ ├── routes.js
│ └── toString.js
└── store.js
The client app will be compiled into server/data/static/build/
. Then it will be embedded into go package via go-bindata. After that the package will be compiled into binary.
Convention: javascript app should declare main function right in the global namespace. It will used to render the app at the server side.
MIT