Skip to content

Commit 862e392

Browse files
committed
Functional Go and React development and deployment
1 parent 840c3d7 commit 862e392

26 files changed

+16903
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
react-app/build

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
run: pkged.go
2+
go run main.go pkged.go -mode embed
3+
4+
pkged.go: react-app/build
5+
pkger -o ./ -include /react-app/build
6+
7+
8+
react-app/build:
9+
(cd react-app && npm run build)
10+
11+
clean:
12+
rm -rf react-app/build pkged.go

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/bmurray/go-react
22

33
go 1.15
4+
5+
require github.com/markbates/pkger v0.17.1

go.sum

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
5+
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
6+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
7+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
8+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
9+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
10+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
11+
github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno=
12+
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
13+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
14+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
16+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
17+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
18+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
19+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
20+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
22+
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
23+
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"log"
7+
"net/http"
8+
"net/http/httputil"
9+
"net/url"
10+
"time"
11+
12+
"github.com/markbates/pkger"
13+
)
14+
15+
func main() {
16+
proxy := flag.String("proxy", "http://localhost:3000/", "Address to proxy requests to")
17+
// Dir follows general filesystem pathing rules
18+
dir := flag.String("dir", "./react-app/build/", "Directory where the static built app resides")
19+
// Embed needs to be absolute, based on the arguments of pkger. See Makefile
20+
embed := flag.String("embed", "/react-app/build/", "Directory where the static built embedded app resides")
21+
mode := flag.String("mode", "proxy", "Mode to serve REACT site: proxy, dir, embed")
22+
listen := flag.String("listen", ":8080", "Listen on address")
23+
flag.Parse()
24+
25+
// Basic ServeMux and API that just sends the time
26+
mux := http.NewServeMux()
27+
mux.HandleFunc("/api", basicAPI)
28+
29+
// The React serve magic
30+
switch *mode {
31+
case "proxy":
32+
// Proxy mode is most useful for development
33+
// Preserves live-reload
34+
u, err := url.Parse(*proxy)
35+
if err != nil {
36+
log.Fatalf("Cannot parse proxy address: %s", err)
37+
}
38+
mux.Handle("/", httputil.NewSingleHostReverseProxy(u))
39+
case "dir":
40+
// Dir mode is useful if you build your react app but don't want to embed it in the binary
41+
mux.Handle("/", http.FileServer(http.Dir(*dir)))
42+
case "embed":
43+
// Embed mode serves files that are embedded in the binary. Very useful for distribution
44+
mux.Handle("/", http.FileServer(EmbedDir(*embed)))
45+
default:
46+
// Any other mode would assume you have a reverse proxy, like nginx, that filters traffic
47+
log.Println("No react mode; this only works if you have a frontend reverse proxy")
48+
}
49+
s := &http.Server{
50+
Addr: *listen,
51+
Handler: mux,
52+
ReadTimeout: 10 * time.Second,
53+
WriteTimeout: 10 * time.Second,
54+
}
55+
log.Println(s.ListenAndServe())
56+
}
57+
58+
// EmbedDir provides a convenience method to default requests back to /index.html, allowing react-router to work correctly
59+
type EmbedDir string
60+
61+
func (d EmbedDir) Open(name string) (http.File, error) {
62+
// pkger.Dir can be used directly, but does not failback to the index file, breaking react-router
63+
pk := pkger.Dir(d)
64+
if f, err := pk.Open(name); err == nil {
65+
return f, err
66+
} else {
67+
return pk.Open("/index.html")
68+
}
69+
}
70+
71+
func basicAPI(w http.ResponseWriter, req *http.Request) {
72+
w.Header().Add("Content-Type", "application/json")
73+
j := json.NewEncoder(w)
74+
if err := j.Encode(struct {
75+
Time string `json:"time"`
76+
}{
77+
time.Now().String(),
78+
}); err != nil {
79+
log.Println("Cannot encode time", err)
80+
}
81+
}

pkged.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react-app/.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

react-app/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Getting Started with Create React App
2+
3+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4+
5+
## Available Scripts
6+
7+
In the project directory, you can run:
8+
9+
### `npm start`
10+
11+
Runs the app in the development mode.\
12+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13+
14+
The page will reload if you make edits.\
15+
You will also see any lint errors in the console.
16+
17+
### `npm test`
18+
19+
Launches the test runner in the interactive watch mode.\
20+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21+
22+
### `npm run build`
23+
24+
Builds the app for production to the `build` folder.\
25+
It correctly bundles React in production mode and optimizes the build for the best performance.
26+
27+
The build is minified and the filenames include the hashes.\
28+
Your app is ready to be deployed!
29+
30+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31+
32+
### `npm run eject`
33+
34+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35+
36+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37+
38+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39+
40+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41+
42+
## Learn More
43+
44+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45+
46+
To learn React, check out the [React documentation](https://reactjs.org/).

0 commit comments

Comments
 (0)