packwise-skills 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursorrules +23 -0
- package/CLAUDE.md +25 -0
- package/README.md +295 -0
- package/audit.md +224 -0
- package/bin/packwise.js +155 -0
- package/package.json +31 -0
- package/skill.md +719 -0
- package/sub-skills/ai/local-llm.md +183 -0
- package/sub-skills/ai/python-ml.md +164 -0
- package/sub-skills/backend/go-server.md +184 -0
- package/sub-skills/backend/java-spring.md +241 -0
- package/sub-skills/backend/node-server.md +164 -0
- package/sub-skills/backend/php-laravel.md +175 -0
- package/sub-skills/backend/python-server.md +164 -0
- package/sub-skills/backend/rust-backend.md +118 -0
- package/sub-skills/cli/python-cli.md +236 -0
- package/sub-skills/cli/sdk-library.md +497 -0
- package/sub-skills/cloud/ci-cd-pipelines.md +350 -0
- package/sub-skills/cloud/docker.md +191 -0
- package/sub-skills/cloud/kubernetes.md +277 -0
- package/sub-skills/cloud/payment-integration.md +307 -0
- package/sub-skills/cross-platform/multiplatform.md +252 -0
- package/sub-skills/desktop/electron.md +783 -0
- package/sub-skills/desktop/game-dev.md +443 -0
- package/sub-skills/desktop/native-app.md +123 -0
- package/sub-skills/desktop/scenarios.md +443 -0
- package/sub-skills/desktop/smart-platforms.md +324 -0
- package/sub-skills/desktop/tauri.md +428 -0
- package/sub-skills/desktop/vr-ar.md +252 -0
- package/sub-skills/desktop/web-to-desktop.md +153 -0
- package/sub-skills/embedded/car-infotainment.md +129 -0
- package/sub-skills/embedded/esp32.md +184 -0
- package/sub-skills/embedded/ros.md +150 -0
- package/sub-skills/embedded/stm32.md +160 -0
- package/sub-skills/mobile/android.md +322 -0
- package/sub-skills/mobile/capacitor.md +232 -0
- package/sub-skills/mobile/flutter-mobile.md +138 -0
- package/sub-skills/mobile/harmonyos.md +150 -0
- package/sub-skills/mobile/ios.md +245 -0
- package/sub-skills/mobile/react-native.md +443 -0
- package/sub-skills/mobile/wearables.md +230 -0
- package/sub-skills/plugins/browser-extension.md +308 -0
- package/sub-skills/plugins/jetbrains-plugin.md +226 -0
- package/sub-skills/plugins/vscode-extension.md +204 -0
- package/sub-skills/security/security-tools.md +174 -0
- package/sub-skills/web/monorepo.md +274 -0
- package/sub-skills/web/pwa.md +220 -0
- package/sub-skills/web/serverless-edge.md +295 -0
- package/sub-skills/web/spa.md +266 -0
- package/sub-skills/web/ssr.md +228 -0
- package/sub-skills/web/wasm.md +243 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# Local LLM Application Build Sub-Skill
|
|
2
|
+
|
|
3
|
+
Package and deploy local large language model applications (offline AI, private deployment, edge inference).
|
|
4
|
+
|
|
5
|
+
**Current versions**: Ollama 0.4+ / llama.cpp b4000+ / vLLM 0.6+ (2025-2026)
|
|
6
|
+
|
|
7
|
+
## When to Use
|
|
8
|
+
|
|
9
|
+
- Offline AI assistant (no internet required)
|
|
10
|
+
- Privacy-sensitive AI applications (enterprise internal)
|
|
11
|
+
- Edge AI deployment (Jetson, Raspberry Pi, local servers)
|
|
12
|
+
- Cost optimization (avoid API fees)
|
|
13
|
+
- Custom fine-tuned model serving
|
|
14
|
+
|
|
15
|
+
## Tech Stack Comparison
|
|
16
|
+
|
|
17
|
+
| Framework | Language | GPU Support | Best For | Setup Complexity |
|
|
18
|
+
|-----------|---------|-------------|---------|-----------------|
|
|
19
|
+
| Ollama | Go | CUDA/Metal/ROCm | Simplest local LLM runtime | Lowest |
|
|
20
|
+
| llama.cpp | C++ | CUDA/Metal/Vulkan/ROCm | CPU inference, maximum control | Medium |
|
|
21
|
+
| vLLM | Python | CUDA only | High-throughput GPU serving | Medium |
|
|
22
|
+
| LM Studio | Desktop app | CUDA/Metal | GUI-based model management | Lowest |
|
|
23
|
+
| text-generation-inference | Rust/Python | CUDA | Production GPU serving (Hugging Face) | High |
|
|
24
|
+
| LocalAI | Go | CUDA/Metal | OpenAI-compatible local API | Low |
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Ollama (Recommended for Getting Started)
|
|
29
|
+
|
|
30
|
+
### Install & Run
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# Install
|
|
34
|
+
curl -fsSL https://ollama.ai/install.sh | sh # Linux
|
|
35
|
+
brew install ollama # macOS
|
|
36
|
+
# Windows: download from ollama.ai
|
|
37
|
+
|
|
38
|
+
# Run model
|
|
39
|
+
ollama run llama3.1 # 8B (default)
|
|
40
|
+
ollama run llama3.1:70b # 70B (needs ~40GB VRAM)
|
|
41
|
+
ollama run codellama # Code-specific model
|
|
42
|
+
ollama run mistral # Mistral 7B
|
|
43
|
+
ollama run phi3 # Microsoft Phi-3
|
|
44
|
+
|
|
45
|
+
# API call (OpenAI-compatible)
|
|
46
|
+
curl http://localhost:11434/v1/chat/completions -d '{
|
|
47
|
+
"model": "llama3.1",
|
|
48
|
+
"messages": [{"role": "user", "content": "Hello!"}]
|
|
49
|
+
}'
|
|
50
|
+
|
|
51
|
+
# List installed models
|
|
52
|
+
ollama list
|
|
53
|
+
|
|
54
|
+
# Pull model without running
|
|
55
|
+
ollama pull llama3.1:70b
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Docker Deployment
|
|
59
|
+
|
|
60
|
+
```yaml
|
|
61
|
+
# docker-compose.yml
|
|
62
|
+
services:
|
|
63
|
+
ollama:
|
|
64
|
+
image: ollama/ollama
|
|
65
|
+
ports: ["11434:11434"]
|
|
66
|
+
volumes: ["ollama:/root/.ollama"]
|
|
67
|
+
deploy:
|
|
68
|
+
resources:
|
|
69
|
+
reservations:
|
|
70
|
+
devices:
|
|
71
|
+
- driver: nvidia
|
|
72
|
+
count: all
|
|
73
|
+
capabilities: [gpu]
|
|
74
|
+
open-webui:
|
|
75
|
+
image: ghcr.io/open-webui/open-webui
|
|
76
|
+
ports: ["3000:8080"]
|
|
77
|
+
environment:
|
|
78
|
+
OLLAMA_BASE_URL: http://ollama:11434
|
|
79
|
+
depends_on: [ollama]
|
|
80
|
+
volumes:
|
|
81
|
+
ollama:
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## llama.cpp (Maximum Control)
|
|
87
|
+
|
|
88
|
+
### Build & Run
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
# Clone and build
|
|
92
|
+
git clone https://github.com/ggerganov/llama.cpp
|
|
93
|
+
cd llama.cpp
|
|
94
|
+
make -j$(nproc) # CPU only
|
|
95
|
+
make -j$(nproc) CUDA=1 # NVIDIA GPU
|
|
96
|
+
make -j$(nproc) METAL=1 # Apple Silicon
|
|
97
|
+
|
|
98
|
+
# Run
|
|
99
|
+
./llama-server -m models/llama-3.1-8b-q4_k_m.gguf \
|
|
100
|
+
--host 0.0.0.0 --port 8080 \
|
|
101
|
+
-ngl 99 \ # Offload all layers to GPU
|
|
102
|
+
-c 4096 # Context size
|
|
103
|
+
|
|
104
|
+
# Quantize model
|
|
105
|
+
./llama-quantize input.gguf output-q4_k_m.gguf Q4_K_M
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### GGUF Quantization Levels
|
|
109
|
+
|
|
110
|
+
| Quant | Size (8B) | Quality | Speed | Use Case |
|
|
111
|
+
|-------|----------|---------|-------|----------|
|
|
112
|
+
| Q2_K | ~3 GB | Low | Fastest | Maximum compression |
|
|
113
|
+
| Q4_K_M | ~5 GB | Good | Fast | **Recommended default** |
|
|
114
|
+
| Q5_K_M | ~6 GB | Better | Good | Quality-sensitive |
|
|
115
|
+
| Q6_K | ~7 GB | Great | Slower | Near-lossless |
|
|
116
|
+
| Q8_0 | ~8 GB | Excellent | Slower | Minimal quality loss |
|
|
117
|
+
| F16 | ~16 GB | Lossless | Slowest | Research/evaluation |
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## vLLM (High-Throughput GPU Serving)
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
pip install vllm
|
|
125
|
+
|
|
126
|
+
# Serve model (OpenAI-compatible API)
|
|
127
|
+
vllm serve meta-llama/Meta-Llama-3.1-8B-Instruct \
|
|
128
|
+
--host 0.0.0.0 --port 8000 \
|
|
129
|
+
--tensor-parallel-size 1 \ # Number of GPUs
|
|
130
|
+
--max-model-len 4096 \
|
|
131
|
+
--quantization awq # AWQ quantization (optional)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
# Use as Python library
|
|
136
|
+
from vllm import LLM, SamplingParams
|
|
137
|
+
|
|
138
|
+
llm = LLM(model="meta-llama/Meta-Llama-3.1-8B-Instruct")
|
|
139
|
+
params = SamplingParams(temperature=0.7, max_tokens=512)
|
|
140
|
+
outputs = llm.generate(["Hello, how are you?"], params)
|
|
141
|
+
print(outputs[0].outputs[0].text)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Memory Requirements (Approximate)
|
|
147
|
+
|
|
148
|
+
| Model | Q4_K_M (Recommended) | FP16 (Full) | Min GPU VRAM |
|
|
149
|
+
|-------|---------------------|-------------|-------------|
|
|
150
|
+
| 1B | ~1 GB | ~2 GB | 2 GB |
|
|
151
|
+
| 3B | ~2 GB | ~6 GB | 4 GB |
|
|
152
|
+
| 7B/8B | ~5 GB | ~14 GB | 8 GB |
|
|
153
|
+
| 13B | ~8 GB | ~26 GB | 16 GB |
|
|
154
|
+
| 34B | ~20 GB | ~68 GB | 40 GB |
|
|
155
|
+
| 70B | ~40 GB | ~140 GB | 2×40 GB |
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Hardware Recommendations
|
|
160
|
+
|
|
161
|
+
| Use Case | Minimum | Recommended |
|
|
162
|
+
|----------|---------|------------|
|
|
163
|
+
| Chat (7B) | 16GB RAM (CPU) | 8GB VRAM GPU |
|
|
164
|
+
| Chat (13B+) | 32GB RAM or 16GB VRAM | 24GB VRAM (RTX 4090) |
|
|
165
|
+
| Code (7B) | 16GB RAM | 12GB VRAM |
|
|
166
|
+
| Production serving | 24GB VRAM | A100 40GB / H100 |
|
|
167
|
+
| Edge (Raspberry Pi) | 8GB RAM (very slow) | Jetson Orin 16GB |
|
|
168
|
+
| Apple Silicon Mac | 16GB unified | 32GB+ unified (M2/M3 Pro/Max) |
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## Common Pitfalls
|
|
173
|
+
|
|
174
|
+
| Issue | Fix |
|
|
175
|
+
|-------|-----|
|
|
176
|
+
| Slow model download | Use Hugging Face mirror (`HF_ENDPOINT`); use `ollama pull` for Ollama |
|
|
177
|
+
| GPU not detected | Check `nvidia-smi`; install NVIDIA Container Toolkit for Docker |
|
|
178
|
+
| Out of memory | Use smaller quantization (Q4_K_M); reduce context length; use CPU offload |
|
|
179
|
+
| Slow response | Use GPU; reduce model size; use `--flash-attention` |
|
|
180
|
+
| CORS errors when calling API | Ollama: set `OLLAMA_ORIGINS=*`; llama.cpp: add `--cors *` |
|
|
181
|
+
| Model hallucination | Use system prompt; lower temperature; use RAG for factual accuracy |
|
|
182
|
+
| Docker GPU not working | Install `nvidia-container-toolkit`; restart Docker daemon |
|
|
183
|
+
| Apple Silicon not using GPU | Use Metal-enabled build; Ollama uses Metal by default on macOS |
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# Python ML Model Packaging Sub-Skill
|
|
2
|
+
|
|
3
|
+
Package, optimize, and serve machine learning models for production.
|
|
4
|
+
|
|
5
|
+
**Current version**: Python 3.12+ / PyTorch 2.x / TensorFlow 2.17+ / ONNX Runtime 1.19+ (2025-2026)
|
|
6
|
+
|
|
7
|
+
## When to Use
|
|
8
|
+
|
|
9
|
+
- Trained ML model deployed as API service
|
|
10
|
+
- Image recognition / NLP / recommendation system
|
|
11
|
+
- Data analysis pipeline
|
|
12
|
+
- Research model publication and reproducibility
|
|
13
|
+
- Edge ML deployment (mobile/embedded)
|
|
14
|
+
|
|
15
|
+
## Deployment Options
|
|
16
|
+
|
|
17
|
+
### FastAPI + Uvicorn (Recommended for APIs)
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from fastapi import FastAPI
|
|
21
|
+
from transformers import pipeline
|
|
22
|
+
import torch
|
|
23
|
+
|
|
24
|
+
app = FastAPI()
|
|
25
|
+
|
|
26
|
+
# Load model once at startup
|
|
27
|
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
28
|
+
classifier = pipeline("text-classification", model="my-model", device=device)
|
|
29
|
+
|
|
30
|
+
@app.post("/predict")
|
|
31
|
+
async def predict(text: str):
|
|
32
|
+
return classifier(text)
|
|
33
|
+
|
|
34
|
+
@app.get("/health")
|
|
35
|
+
async def health():
|
|
36
|
+
return {"status": "ok", "device": device}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Flask + Gunicorn
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from flask import Flask, request, jsonify
|
|
43
|
+
import pickle
|
|
44
|
+
|
|
45
|
+
app = Flask(__name__)
|
|
46
|
+
model = pickle.load(open("model.pkl", "rb"))
|
|
47
|
+
|
|
48
|
+
@app.route("/predict", methods=["POST"])
|
|
49
|
+
def predict():
|
|
50
|
+
data = request.json
|
|
51
|
+
result = model.predict([data["features"]])
|
|
52
|
+
return jsonify({"prediction": result.tolist()})
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### ONNX Runtime (High-Performance Inference)
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import onnxruntime as ort
|
|
59
|
+
import numpy as np
|
|
60
|
+
|
|
61
|
+
# CPU inference
|
|
62
|
+
session = ort.InferenceSession("model.onnx", providers=["CPUExecutionProvider"])
|
|
63
|
+
|
|
64
|
+
# GPU inference (CUDA)
|
|
65
|
+
session = ort.InferenceSession("model.onnx", providers=["CUDAExecutionProvider"])
|
|
66
|
+
|
|
67
|
+
# TensorRT (fastest, NVIDIA GPU only)
|
|
68
|
+
session = ort.InferenceSession("model.onnx", providers=["TensorrtExecutionProvider", "CUDAExecutionProvider"])
|
|
69
|
+
|
|
70
|
+
result = session.run(None, {"input": input_data.astype(np.float32)})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Model Export
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
# PyTorch → ONNX
|
|
77
|
+
import torch
|
|
78
|
+
model = torch.load("model.pt")
|
|
79
|
+
model.eval()
|
|
80
|
+
dummy_input = torch.randn(1, 3, 224, 224)
|
|
81
|
+
torch.onnx.export(model, dummy_input, "model.onnx", opset_version=17)
|
|
82
|
+
|
|
83
|
+
# TensorFlow → SavedModel
|
|
84
|
+
model.save("saved_model/")
|
|
85
|
+
|
|
86
|
+
# TensorFlow → TFLite (mobile)
|
|
87
|
+
converter = tf.lite.TFLiteConverter.from_saved_model("saved_model/")
|
|
88
|
+
converter.optimizations = [tf.lite.Optimize.DEFAULT]
|
|
89
|
+
tflite_model = converter.convert()
|
|
90
|
+
with open("model.tflite", "wb") as f:
|
|
91
|
+
f.write(tflite_model)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Model Formats
|
|
95
|
+
|
|
96
|
+
| Format | Framework | Size | Inference Speed | Best For |
|
|
97
|
+
|--------|----------|------|----------------|----------|
|
|
98
|
+
| PyTorch (.pt) | PyTorch | Large | Standard | Research, training |
|
|
99
|
+
| ONNX (.onnx) | Cross-framework | Medium | Fast | Production APIs |
|
|
100
|
+
| SavedModel | TensorFlow | Large | Standard | TF Serving |
|
|
101
|
+
| TFLite (.tflite) | TensorFlow Lite | Small | Fast (mobile) | Mobile/edge |
|
|
102
|
+
| TensorRT | NVIDIA | Medium | Fastest (GPU) | NVIDIA GPU servers |
|
|
103
|
+
| GGUF | llama.cpp | Small | Fast (CPU) | Local LLM inference |
|
|
104
|
+
| CoreML (.mlmodel) | Apple | Medium | Fast (Apple) | iOS/macOS on-device |
|
|
105
|
+
| OpenVINO | Intel | Medium | Fast (Intel CPU) | Intel hardware |
|
|
106
|
+
|
|
107
|
+
## Docker (GPU)
|
|
108
|
+
|
|
109
|
+
```dockerfile
|
|
110
|
+
FROM nvidia/cuda:12.4-runtime-ubuntu22.04
|
|
111
|
+
RUN apt-get update && apt-get install -y python3 python3-pip && rm -rf /var/lib/apt/lists/*
|
|
112
|
+
WORKDIR /app
|
|
113
|
+
COPY requirements.txt .
|
|
114
|
+
RUN pip3 install --no-cache-dir -r requirements.txt
|
|
115
|
+
COPY . .
|
|
116
|
+
RUN groupadd -r appuser && useradd -r -g appuser appuser
|
|
117
|
+
USER appuser
|
|
118
|
+
EXPOSE 8000
|
|
119
|
+
HEALTHCHECK --interval=30s --timeout=3s CMD curl -f http://localhost:8000/health || exit 1
|
|
120
|
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```dockerfile
|
|
124
|
+
# CPU-only (smaller)
|
|
125
|
+
FROM python:3.13-slim
|
|
126
|
+
WORKDIR /app
|
|
127
|
+
COPY requirements.txt .
|
|
128
|
+
RUN pip install --no-cache-dir -r requirements.txt
|
|
129
|
+
COPY . .
|
|
130
|
+
RUN groupadd -r appuser && useradd -r -g appuser appuser
|
|
131
|
+
USER appuser
|
|
132
|
+
EXPOSE 8000
|
|
133
|
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0"]
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Model Optimization
|
|
137
|
+
|
|
138
|
+
| Technique | Description | Typical Savings |
|
|
139
|
+
|-----------|-------------|----------------|
|
|
140
|
+
| Quantization (INT8) | Reduce precision from FP32 to INT8 | 4x smaller, 2-3x faster |
|
|
141
|
+
| Quantization (INT4) | Further reduction | 8x smaller (LLMs) |
|
|
142
|
+
| Pruning | Remove redundant weights | 2-10x smaller |
|
|
143
|
+
| Knowledge Distillation | Train smaller model from larger | Custom |
|
|
144
|
+
| ONNX Optimization | Graph optimization | 10-30% faster |
|
|
145
|
+
| TensorRT | NVIDIA GPU optimization | 2-5x faster |
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
# ONNX quantization
|
|
149
|
+
from onnxruntime.quantization import quantize_dynamic, QuantType
|
|
150
|
+
quantize_dynamic("model.onnx", "model_quant.onnx", weight_type=QuantType.QUInt8)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Common Pitfalls
|
|
154
|
+
|
|
155
|
+
| Issue | Fix |
|
|
156
|
+
|-------|-----|
|
|
157
|
+
| Model too large for deployment | Quantize (INT8/INT4); use ONNX; prune |
|
|
158
|
+
| GPU out of memory | Reduce batch size; use gradient checkpointing; quantize |
|
|
159
|
+
| Dependency conflicts | Use Docker isolation; pin exact versions |
|
|
160
|
+
| Slow inference | Convert to ONNX or TensorRT; use GPU; batch requests |
|
|
161
|
+
| CUDA version mismatch | Match PyTorch CUDA version with system CUDA; use Docker |
|
|
162
|
+
| Model loading time too long | Load once at startup; use model server (TorchServe, TF Serving) |
|
|
163
|
+
| Inconsistent results between environments | Fix random seeds; pin all dependency versions |
|
|
164
|
+
| ONNX export fails | Check opset version; use `torch.onnx.export` with `opset_version=17` |
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# Go Backend Build Sub-Skill
|
|
2
|
+
|
|
3
|
+
Build Go backend services (Gin/Echo/Fiber/Axum-like stdlib).
|
|
4
|
+
|
|
5
|
+
**Current version**: Go 1.24+ / 1.25 / 1.26 (2025-2026)
|
|
6
|
+
|
|
7
|
+
## When to Use
|
|
8
|
+
|
|
9
|
+
- High-performance REST/gRPC API services
|
|
10
|
+
- CLI tools with embedded server
|
|
11
|
+
- Microservices requiring low memory footprint
|
|
12
|
+
- Network services (proxy, gateway, load balancer)
|
|
13
|
+
- Concurrent/parallel processing services
|
|
14
|
+
|
|
15
|
+
## Framework Quick Start
|
|
16
|
+
|
|
17
|
+
### Gin (Most Popular)
|
|
18
|
+
|
|
19
|
+
```go
|
|
20
|
+
package main
|
|
21
|
+
|
|
22
|
+
import "github.com/gin-gonic/gin"
|
|
23
|
+
|
|
24
|
+
func main() {
|
|
25
|
+
r := gin.Default()
|
|
26
|
+
r.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) })
|
|
27
|
+
r.GET("/api/users", getUsers)
|
|
28
|
+
r.Run(":8080")
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Echo (Lightweight)
|
|
33
|
+
|
|
34
|
+
```go
|
|
35
|
+
package main
|
|
36
|
+
|
|
37
|
+
import "github.com/labstack/echo/v4"
|
|
38
|
+
|
|
39
|
+
func main() {
|
|
40
|
+
e := echo.New()
|
|
41
|
+
e.GET("/health", func(c echo.Context) error { return c.JSON(200, map[string]string{"status": "ok"}) })
|
|
42
|
+
e.Logger.Fatal(e.Start(":8080"))
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Fiber (Express-like, fastest)
|
|
47
|
+
|
|
48
|
+
```go
|
|
49
|
+
package main
|
|
50
|
+
|
|
51
|
+
import "github.com/gofiber/fiber/v3"
|
|
52
|
+
|
|
53
|
+
func main() {
|
|
54
|
+
app := fiber.New()
|
|
55
|
+
app.Get("/health", func(c fiber.Ctx) error { return c.JSON(map[string]string{"status": "ok"}) })
|
|
56
|
+
app.Listen(":8080")
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### net/http (Standard Library, Zero Dependencies)
|
|
61
|
+
|
|
62
|
+
```go
|
|
63
|
+
package main
|
|
64
|
+
|
|
65
|
+
import (
|
|
66
|
+
"encoding/json"
|
|
67
|
+
"net/http"
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
func main() {
|
|
71
|
+
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
72
|
+
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
|
|
73
|
+
})
|
|
74
|
+
http.ListenAndServe(":8080", nil)
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Framework Comparison
|
|
79
|
+
|
|
80
|
+
| Framework | Performance | Middleware | Ecosystem | Best For |
|
|
81
|
+
|-----------|------------|-----------|-----------|----------|
|
|
82
|
+
| Gin | High | Rich | Largest | General-purpose APIs |
|
|
83
|
+
| Echo | High | Rich | Large | Clean API design |
|
|
84
|
+
| Fiber | Highest | Rich | Growing | Express-style, max performance |
|
|
85
|
+
| net/http | High | Manual | Stdlib | Zero-dependency, simple APIs |
|
|
86
|
+
| Chi | High | Composable | Moderate | RESTful APIs, middleware chains |
|
|
87
|
+
|
|
88
|
+
## Build
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
# Standard build
|
|
92
|
+
go build -o myapp .
|
|
93
|
+
|
|
94
|
+
# Optimized release build
|
|
95
|
+
CGO_ENABLED=0 go build -ldflags="-s -w" -o myapp .
|
|
96
|
+
|
|
97
|
+
# With version embedding
|
|
98
|
+
go build -ldflags="-s -w \
|
|
99
|
+
-X main.version=$(git describe --tags --always) \
|
|
100
|
+
-X main.buildDate=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
|
|
101
|
+
-o myapp .
|
|
102
|
+
|
|
103
|
+
# Cross-compile
|
|
104
|
+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o myapp-linux .
|
|
105
|
+
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o myapp.exe .
|
|
106
|
+
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o myapp-mac .
|
|
107
|
+
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o myapp-mac-intel .
|
|
108
|
+
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o myapp-linux-arm64 .
|
|
109
|
+
|
|
110
|
+
# Build all platforms at once
|
|
111
|
+
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o dist/myapp-linux-amd64 .
|
|
112
|
+
GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o dist/myapp-linux-arm64 .
|
|
113
|
+
GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o dist/myapp-darwin-arm64 .
|
|
114
|
+
GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o dist/myapp-darwin-amd64 .
|
|
115
|
+
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o dist/myapp-windows-amd64.exe .
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Docker
|
|
119
|
+
|
|
120
|
+
```dockerfile
|
|
121
|
+
FROM golang:1.23-alpine AS builder
|
|
122
|
+
WORKDIR /app
|
|
123
|
+
COPY go.* ./
|
|
124
|
+
RUN go mod download
|
|
125
|
+
COPY . .
|
|
126
|
+
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o myapp .
|
|
127
|
+
|
|
128
|
+
FROM alpine:latest
|
|
129
|
+
RUN apk add --no-cache ca-certificates tzdata && \
|
|
130
|
+
addgroup -S appgroup && adduser -S appuser -G appgroup
|
|
131
|
+
COPY --from=builder /app/myapp /myapp
|
|
132
|
+
USER appuser
|
|
133
|
+
EXPOSE 8080
|
|
134
|
+
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:8080/health || exit 1
|
|
135
|
+
CMD ["/myapp"]
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Embed Static Files
|
|
139
|
+
|
|
140
|
+
```go
|
|
141
|
+
package main
|
|
142
|
+
|
|
143
|
+
import "embed"
|
|
144
|
+
|
|
145
|
+
//go:embed static/*
|
|
146
|
+
var staticFiles embed.FS
|
|
147
|
+
|
|
148
|
+
func main() {
|
|
149
|
+
http.Handle("/", http.FileServer(http.FS(staticFiles)))
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## gRPC Build
|
|
154
|
+
|
|
155
|
+
```protobuf
|
|
156
|
+
// proto/service.proto
|
|
157
|
+
syntax = "proto3";
|
|
158
|
+
package myservice;
|
|
159
|
+
service MyService {
|
|
160
|
+
rpc GetUser(GetUserRequest) returns (User);
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
# Generate Go code
|
|
166
|
+
protoc --go_out=. --go-grpc_out=. proto/service.proto
|
|
167
|
+
|
|
168
|
+
# Build server and client
|
|
169
|
+
go build -o server ./cmd/server
|
|
170
|
+
go build -o client ./cmd/client
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Common Pitfalls
|
|
174
|
+
|
|
175
|
+
| Issue | Fix |
|
|
176
|
+
|-------|-----|
|
|
177
|
+
| CGO dependency fails cross-compile | Use `CGO_ENABLED=0`; or use `cross` Docker image |
|
|
178
|
+
| Timezone issues in container | Install `tzdata` package in Alpine image |
|
|
179
|
+
| Static files not included | Use `//go:embed` directive (Go 1.16+) |
|
|
180
|
+
| Binary too large | Use `ldflags="-s -w"`; strip debug symbols |
|
|
181
|
+
| `go.sum` mismatch in CI | Run `go mod tidy`; commit both `go.mod` and `go.sum` |
|
|
182
|
+
| Import cycle | Restructure packages; use interfaces to break cycles |
|
|
183
|
+
| Race condition | Run with `-race` flag during testing |
|
|
184
|
+
| Memory leak in long-running | Use `pprof` for profiling; check goroutine leaks |
|