|
1 | | -# API Vercel Template |
| 1 | +# API Vercel Template Guide |
2 | 2 |
|
3 | | -An API template optimized for Vercel serverless deployment. |
| 3 | +## Overview |
4 | 4 |
|
5 | | -## Features |
| 5 | +jzero deeply integrates with Vercel ecosystem, bringing Go developers a frontend-level deployment experience! Automatically generate Vercel-compliant serverless functions from `.api` definitions, no need to deploy to your own servers. |
6 | 6 |
|
7 | | -- **RESTful API Structure**: Clean and organized API design |
8 | | -- **Vercel Serverless Deployment**: Optimized for Vercel's serverless functions |
9 | | -- **Built-in Middleware**: Common middleware included out of the box |
10 | | -- **Example Endpoints**: Ready-to-use example API endpoints |
| 7 | +### Core Value |
11 | 8 |
|
12 | | -## Getting Started |
| 9 | +- ✅ **Zero-Config Deployment**: Deploy directly from Git repository, no extra configuration needed, no servers required, no CI/CD pipeline |
| 10 | +- ✅ **Global Edge Network**: Deploy Go functions to edge nodes nearest to users using Vercel's global infrastructure, achieving millisecond-level response |
| 11 | +- ✅ **Free Domain & HTTPS**: Automatically get `.vercel.app` production-grade domain with built-in CDN acceleration and SSL certificates |
| 12 | +- ✅ **Preview Environments**: Automatically generate independent preview URLs for each PR |
13 | 13 |
|
14 | | -```bash |
15 | | -jzero new simpleapi-vercel --branch api-vercel |
16 | | -cd simpleapi-vercel |
17 | | -``` |
| 14 | +## Quick Start |
18 | 15 |
|
19 | | -## Project Structure |
| 16 | +### Install jzero |
20 | 17 |
|
21 | | -``` |
22 | | -. |
23 | | -├── api/ # API endpoints |
24 | | -│ ├── handlers/ # Request handlers |
25 | | -│ ├── logic/ # Business logic |
26 | | -│ └── middleware/ # Middleware functions |
27 | | -├── internal/ # Internal packages |
28 | | -├── go.mod # Go module file |
29 | | -└── vercel.json # Vercel configuration |
| 18 | +```bash |
| 19 | +# Install jzero |
| 20 | +go install github.com/jzero-io/jzero@latest |
| 21 | + |
| 22 | +# Install related tools |
| 23 | +jzero check |
| 24 | +go install github.com/jzero-io/gorename@latest |
30 | 25 | ``` |
31 | 26 |
|
32 | | -## Usage Examples |
| 27 | +### Create Vercel Project |
33 | 28 |
|
34 | | -### Create a New Endpoint |
| 29 | +```bash |
| 30 | +# Create new Vercel serverless project from remote template |
| 31 | +jzero new jzero-api-vercel-example --remote https://github.com/jzero-io/templates --branch api-vercel |
35 | 32 |
|
36 | | -```go |
37 | | -// api/hello/hello.go |
38 | | -package hello |
| 33 | +# Add new api |
| 34 | +jzero add api test |
39 | 35 |
|
40 | | -import ( |
41 | | - "net/http" |
42 | | - |
43 | | - "github.com/zeromicro/go-zero/rest/httpx" |
44 | | -) |
| 36 | +# Generate code |
| 37 | +jzero gen |
| 38 | +``` |
| 39 | + |
| 40 | +## Project Structure |
45 | 41 |
|
46 | | -func Handler(w http.ResponseWriter, r *http.Request) { |
47 | | - httpx.OkJson(w, map[string]string{ |
48 | | - "message": "Hello from Vercel!", |
49 | | - }) |
50 | | -} |
51 | 42 | ``` |
| 43 | +jzero-api-vercel-example/ |
| 44 | +├── vercel/ |
| 45 | +│ └── client.go # Vercel Go runtime entry |
| 46 | +├── desc/ # API definitions |
| 47 | +│ └── api/ # .api files |
| 48 | +├── server/ # Server side |
| 49 | +│ ├── handler/ # HTTP handlers |
| 50 | +│ ├── logic/ # Business logic |
| 51 | +│ └── types/ # Type definitions |
| 52 | +├── vercel.json # Vercel platform configuration |
| 53 | +├── main.go # Local runtime entry |
| 54 | +└── go.mod # Go module file |
| 55 | +``` |
| 56 | + |
| 57 | +## Deploy to Vercel |
52 | 58 |
|
53 | | -### Deploy to Vercel |
| 59 | +### Git Push to Deploy |
| 60 | + |
| 61 | +jzero-generated projects are fully compatible with Vercel's Git workflow for automatic deployment: |
54 | 62 |
|
55 | 63 | ```bash |
56 | | -# Install Vercel CLI |
57 | | -npm i -g vercel |
| 64 | +cd jzero-api-vercel-example |
| 65 | + |
| 66 | +# Initialize git |
| 67 | +git init |
| 68 | +git add . |
| 69 | +git commit -m "Initial commit" |
58 | 70 |
|
59 | | -# Deploy |
60 | | -vercel |
| 71 | +# Create repository on GitHub and push |
| 72 | +git remote add origin https://github.com/your-username/jzero-api-vercel-example.git |
| 73 | +git push -u origin main |
61 | 74 | ``` |
62 | 75 |
|
63 | | -## Configuration |
| 76 | +### Vercel Platform Auto-Detection |
64 | 77 |
|
65 | | -### vercel.json |
| 78 | +1. Visit [Vercel Dashboard](https://vercel.com/dashboard) |
| 79 | +2. Click "Add New Project" |
| 80 | +3. Import your GitHub repository |
| 81 | +4. `vercel.json` configuration lets Vercel automatically recognize it as a Go project |
| 82 | +5. Click "Deploy" |
66 | 83 |
|
67 | | -```json |
68 | | -{ |
69 | | - "functions": { |
70 | | - "api/**/*.go": { |
71 | | - "runtime": "go1.x" |
72 | | - } |
73 | | - } |
74 | | -} |
75 | | -``` |
| 84 | +**🎉 Deployment complete! Your Go API is now connected to Vercel's global network** |
76 | 85 |
|
77 | | -## Learn More |
| 86 | +## Related Resources |
78 | 87 |
|
79 | | -- [Vercel Documentation](https://vercel.com/docs) |
80 | | -- [Go-Zero Documentation](https://go-zero.dev/) |
| 88 | +- [jzero GitHub](https://github.com/jzero-io/jzero) |
| 89 | +- [jzero Official Site](https://jzero.io) |
| 90 | +- [Vercel Template Repository](https://github.com/jzero-io/templates/tree/api-vercel) |
| 91 | +- [Example Code Repository](https://github.com/jaronnie/jzero-api-vercel-example) |
0 commit comments