Repository: dstotijn/golang-nextjs-portable
Branch: main
Commit: d01a615dcd24
Files: 15
Total size: 6.6 KB
Directory structure:
gitextract_dnyy_ypb/
├── .dockerignore
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── go.mod
├── main.go
└── nextjs/
├── .gitignore
├── next-env.d.ts
├── next.config.js
├── package.json
├── pages/
│ ├── foo/
│ │ ├── bar.tsx
│ │ └── index.tsx
│ └── index.tsx
└── tsconfig.json
================================================
FILE CONTENTS
================================================
================================================
FILE: .dockerignore
================================================
/nextjs/.next
/nextjs/dist
/nextjs/node_modules
================================================
FILE: Dockerfile
================================================
ARG GO_VERSION=1.18
ARG NODE_VERSION=14.16.1
ARG ALPINE_VERSION=3.13.5
FROM node:${NODE_VERSION}-alpine AS node-builder
WORKDIR /app
COPY nextjs/package.json nextjs/yarn.lock ./
RUN yarn install --frozen-lockfile
COPY nextjs/ .
ENV NEXT_TELEMETRY_DISABLED=1
RUN yarn run export
FROM golang:${GO_VERSION}-alpine AS go-builder
WORKDIR /app
COPY go.mod main.go ./
COPY --from=node-builder /app/dist ./nextjs/dist
RUN go build .
FROM alpine:${ALPINE_VERSION}
WORKDIR /app
COPY --from=go-builder /app/golang-nextjs-portable .
ENTRYPOINT ["./golang-nextjs-portable"]
EXPOSE 8080
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2021 David Stotijn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: Makefile
================================================
.PHONY: build-nextjs
build-nextjs:
cd nextjs; \
yarn install; \
NEXT_TELEMETRY_DISABLED=1 yarn run export
.PHONY: build
build: build-nextjs
go build .
================================================
FILE: README.md
================================================
# golang-nextjs-portable
**golang-nextjs-portable** is a small Go program to showcase the `embed` package
for bundling a static HTML export of a Next.js app.
👉 Read the companion
[article](https://v0x.nl/articles/portable-apps-go-nextjs) that walks
through this project.
## Requirements
- Go 1.18
- Yarn
**Note:** While the `embed` package was added in Go 1.16, the `all:` prefix of
the `embed` directive was added in 1.18. We use `all:` in this project because
Next.js static exports contain files and direcories with underscore prefixes.
## Installing
Clone or download the repository:
```sh
$ git clone git@github.com:dstotijn/golang-nextjs-portable.git
```
## Usage
From the repository root directory, generate the static HTML export of the Next.js
app, and build the Go binary:
```sh
$ cd nextjs
$ yarn install
$ yarn run export
$ cd ..
$ go build .
```
Then run the binary:
```sh
$ ./golang-nextjs-portable
2021/04/27 14:55:38 Starting HTTP server at http://localhost:8080 ...
```
## License
[MIT](/LICENSE)
---
© 2021 David Stotijn — [Twitter](https://twitter.com/dstotijn), [Email](mailto:dstotijn@gmail.com), [Homepage](https://v0x.nl)
================================================
FILE: go.mod
================================================
module github.com/dstotijn/golang-nextjs-portable
go 1.18
================================================
FILE: main.go
================================================
package main
import (
"embed"
"io/fs"
"log"
"net/http"
"runtime/pprof"
)
//go:embed all:nextjs/dist
var nextFS embed.FS
func main() {
// Root at the `dist` folder generated by the Next.js app.
distFS, err := fs.Sub(nextFS, "nextjs/dist")
if err != nil {
log.Fatal(err)
}
// The static Next.js app will be served under `/`.
http.Handle("/", http.FileServer(http.FS(distFS)))
// The API will be served under `/api`.
http.HandleFunc("/api", handleAPI)
// Start HTTP server at :8080.
log.Println("Starting HTTP server at http://localhost:8080 ...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handleAPI(w http.ResponseWriter, _ *http.Request) {
// Gather memory allocations profile.
profile := pprof.Lookup("allocs")
// Write profile (human readable, via debug: 1) to HTTP response.
err := profile.WriteTo(w, 1)
if err != nil {
log.Printf("Error: Failed to write allocs profile: %v", err)
}
}
================================================
FILE: nextjs/.gitignore
================================================
/dist
/.next
/node_modules
================================================
FILE: nextjs/next-env.d.ts
================================================
///
This is pages/foo/bar.tsx.
Check out the homepage.
This is pages/foo/index.tsx.
Check out bar.
This is pages/index.tsx.
Check out foo.
Error fetching profile: {error}
)} {!error && !data &&Loading ...
} {!error && data &&{data}}