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.
<img src="https://v0x.nl/assets/articles/golang-nextjs-portable-og.png">
## 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
================================================
/// <reference types="next" />
/// <reference types="next/types/global" />
================================================
FILE: nextjs/next.config.js
================================================
module.exports = {
async rewrites() {
// When running Next.js via Node.js (e.g. `dev` mode), proxy API requests
// to the Go server.
return [
{
source: "/api",
destination: "http://localhost:8080/api",
},
];
},
future: {
webpack5: true,
},
trailingSlash: true,
};
================================================
FILE: nextjs/package.json
================================================
{
"name": "golang-nextjs-portable",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"export": "rm -rf .next && next build && next export -o dist"
},
"dependencies": {
"next": "10.1.3",
"react": "17.0.2",
"react-dom": "17.0.2",
"swr": "0.5.5"
},
"devDependencies": {
"@types/react": "17.0.3",
"typescript": "4.2.4"
}
}
================================================
FILE: nextjs/pages/foo/bar.tsx
================================================
import Link from "next/link";
function Bar(): JSX.Element {
return (
<div>
<h1>Bar</h1>
<p>
This is <code>pages/foo/bar.tsx</code>.
</p>
<p>
Check out <Link href="/">the homepage</Link>.
</p>
</div>
);
}
export default Bar;
================================================
FILE: nextjs/pages/foo/index.tsx
================================================
import Link from "next/link";
function Foo(): JSX.Element {
return (
<div>
<h1>Foo</h1>
<p>
This is <code>pages/foo/index.tsx</code>.
</p>
<p>
Check out <Link href="/foo/bar">bar</Link>.
</p>
</div>
);
}
export default Foo;
================================================
FILE: nextjs/pages/index.tsx
================================================
import Link from "next/link";
import useSWR from "swr";
async function fetcher(url: string) {
const resp = await fetch(url);
return resp.text();
}
function Index(): JSX.Element {
const { data, error } = useSWR("/api", fetcher, { refreshInterval: 1000 });
return (
<div>
<h1>Hello, world!</h1>
<p>
This is <code>pages/index.tsx</code>.
</p>
<p>
Check out <Link href="/foo">foo</Link>.
</p>
<h2>Memory allocation stats from Go server</h2>
{error && (
<p>
Error fetching profile: <strong>{error}</strong>
</p>
)}
{!error && !data && <p>Loading ...</p>}
{!error && data && <pre>{data}</pre>}
</div>
);
}
export default Index;
================================================
FILE: nextjs/tsconfig.json
================================================
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
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
SYMBOL INDEX (7 symbols across 5 files)
FILE: main.go
function main (line 14) | func main() {
function handleAPI (line 31) | func handleAPI(w http.ResponseWriter, _ *http.Request) {
FILE: nextjs/next.config.js
method rewrites (line 2) | async rewrites() {
FILE: nextjs/pages/foo/bar.tsx
function Bar (line 3) | function Bar(): JSX.Element {
FILE: nextjs/pages/foo/index.tsx
function Foo (line 3) | function Foo(): JSX.Element {
FILE: nextjs/pages/index.tsx
function fetcher (line 4) | async function fetcher(url: string) {
function Index (line 9) | function Index(): JSX.Element {
Condensed preview — 15 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (8K chars).
[
{
"path": ".dockerignore",
"chars": 47,
"preview": "/nextjs/.next\n/nextjs/dist\n/nextjs/node_modules"
},
{
"path": "Dockerfile",
"chars": 577,
"preview": "ARG GO_VERSION=1.18\nARG NODE_VERSION=14.16.1\nARG ALPINE_VERSION=3.13.5\n\nFROM node:${NODE_VERSION}-alpine AS node-builder"
},
{
"path": "LICENSE",
"chars": 1069,
"preview": "MIT License\n\nCopyright (c) 2021 David Stotijn\n\nPermission is hereby granted, free of charge, to any person obtaining a c"
},
{
"path": "Makefile",
"chars": 155,
"preview": ".PHONY: build-nextjs\nbuild-nextjs:\n\tcd nextjs; \\\n\tyarn install; \\\n\tNEXT_TELEMETRY_DISABLED=1 yarn run export\n\n.PHONY: bu"
},
{
"path": "README.md",
"chars": 1237,
"preview": "# golang-nextjs-portable\n\n**golang-nextjs-portable** is a small Go program to showcase the `embed` package\nfor bundling "
},
{
"path": "go.mod",
"chars": 59,
"preview": "module github.com/dstotijn/golang-nextjs-portable\n\ngo 1.18\n"
},
{
"path": "main.go",
"chars": 930,
"preview": "package main\n\nimport (\n\t\"embed\"\n\t\"io/fs\"\n\t\"log\"\n\t\"net/http\"\n\t\"runtime/pprof\"\n)\n\n//go:embed all:nextjs/dist\nvar nextFS em"
},
{
"path": "nextjs/.gitignore",
"chars": 26,
"preview": "/dist\n/.next\n/node_modules"
},
{
"path": "nextjs/next-env.d.ts",
"chars": 75,
"preview": "/// <reference types=\"next\" />\n/// <reference types=\"next/types/global\" />\n"
},
{
"path": "nextjs/next.config.js",
"chars": 322,
"preview": "module.exports = {\n async rewrites() {\n // When running Next.js via Node.js (e.g. `dev` mode), proxy API requests\n "
},
{
"path": "nextjs/package.json",
"chars": 443,
"preview": "{\n \"name\": \"golang-nextjs-portable\",\n \"version\": \"0.1.0\",\n \"private\": true,\n \"scripts\": {\n \"dev\": \"next dev\",\n "
},
{
"path": "nextjs/pages/foo/bar.tsx",
"chars": 284,
"preview": "import Link from \"next/link\";\n\nfunction Bar(): JSX.Element {\n return (\n <div>\n <h1>Bar</h1>\n <p>\n T"
},
{
"path": "nextjs/pages/foo/index.tsx",
"chars": 284,
"preview": "import Link from \"next/link\";\n\nfunction Foo(): JSX.Element {\n return (\n <div>\n <h1>Foo</h1>\n <p>\n T"
},
{
"path": "nextjs/pages/index.tsx",
"chars": 747,
"preview": "import Link from \"next/link\";\nimport useSWR from \"swr\";\n\nasync function fetcher(url: string) {\n const resp = await fetc"
},
{
"path": "nextjs/tsconfig.json",
"chars": 533,
"preview": "{\n \"compilerOptions\": {\n \"target\": \"es5\",\n \"lib\": [\n \"dom\",\n \"dom.iterable\",\n \"esnext\"\n ],\n "
}
]
About this extraction
This page contains the full source code of the dstotijn/golang-nextjs-portable GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 15 files (6.6 KB), approximately 2.3k tokens, and a symbol index with 7 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.