Skip to content

Commit 3319429

Browse files
committed
add to github
0 parents  commit 3319429

12 files changed

+877
-0
lines changed

.dockerignore

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Git
2+
.git
3+
.gitignore
4+
.github
5+
6+
# IDE
7+
.idea
8+
.vscode
9+
10+
# Build
11+
./api-proxy
12+
13+
# Docker
14+
docker-compose.yml
15+
16+
# Config
17+
config.json
18+
19+
# Env
20+
.env
21+
22+
# README
23+
README.md
24+
25+
# LICENSE
26+
LICENSE
27+
28+
# .github
29+
.github
30+
31+
# .gitignore
32+
.gitignore
33+
34+
# .dockerignore
35+
.dockerignore

.env.example

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
COMPOSE_PROJECT_NAME=api-proxy
2+
UID=1000
3+
GID=1000
4+
5+
DC_HTTP_PORT=3003

.github/workflows/build.yml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Build and Push Docker image to GHCR
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build-and-push:
10+
runs-on: ubuntu-latest
11+
# runs-on: [self-hosted, personal,ubuntu-latest]
12+
permissions:
13+
packages: write
14+
contents: read
15+
16+
steps:
17+
# Step 1: Checkout the repository
18+
- name: Checkout repository
19+
uses: actions/checkout@v3
20+
21+
# Step 2: Log in to GitHub Container Registry (GHCR)
22+
- name: Log in to GitHub Container Registry
23+
uses: docker/login-action@v2
24+
with:
25+
registry: ghcr.io
26+
username: ${{ github.actor }}
27+
password: ${{ secrets.GITHUB_TOKEN }}
28+
29+
# Step 3: Define dynamic image name and tag based on repo name
30+
- name: Set dynamic image name and tag
31+
id: vars
32+
run: |
33+
REPO_NAME=$(basename "${{ github.repository }}") # Extract the repository name
34+
IMAGE_NAME="ghcr.io/${{ github.repository_owner }}/$REPO_NAME"
35+
GIT_SHA="${{ github.sha }}"
36+
TAG="main-${GIT_SHA:0:7}" # Example: Use first 7 characters of commit SHA
37+
echo "IMAGE_NAME=$IMAGE_NAME" >> $GITHUB_ENV
38+
echo "IMAGE_TAG=$TAG" >> $GITHUB_ENV
39+
40+
# Step 4: Build the Docker image with both tags
41+
- name: Build Docker image
42+
run: |
43+
docker build -t $IMAGE_NAME:$IMAGE_TAG -t $IMAGE_NAME:latest .
44+
45+
# Step 5: Push both Docker image tags to GHCR
46+
- name: Push Docker image to GHCR
47+
run: |
48+
docker push $IMAGE_NAME:$IMAGE_TAG
49+
docker push $IMAGE_NAME:latest

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.env
2+
.idea/
3+
.vscode/
4+
5+
# Env
6+
.env
7+
config.json

Dockerfile

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
FROM golang:1.22.5-alpine AS builder
2+
3+
# Install required system packages and update certificates
4+
RUN apk update && \
5+
apk upgrade && \
6+
apk add --no-cache ca-certificates && \
7+
update-ca-certificates
8+
9+
# Add Maintainer Info to the Image
10+
LABEL maintainer="Mehrdad Amini <[email protected]>"
11+
LABEL description="API Proxy Service"
12+
13+
# Set the Current Working Directory inside the container
14+
WORKDIR /build/api-proxy
15+
16+
# Copy go mod files
17+
COPY go.mod go.sum ./
18+
19+
# Download all dependencies
20+
RUN go mod download
21+
22+
# Copy the source code
23+
COPY main.go .
24+
25+
# Build the binary with optimizations
26+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o api-proxy .
27+
28+
# Start a new stage for final image
29+
#FROM scratch
30+
FROM gcr.io/distroless/static-debian11
31+
32+
WORKDIR /app
33+
34+
# Copy binary and configuration
35+
COPY --from=builder /build/api-proxy/api-proxy .
36+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
37+
COPY config.json.example ./config.json
38+
39+
# Expose port
40+
EXPOSE 3000
41+
42+
# Command to run
43+
CMD ["./api-proxy"]

README.md

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# API Proxy Service
2+
3+
A lightweight API proxy service written in Go that supports multiple endpoints, rate limiting, and proxy rotation.
4+
5+
## Features
6+
7+
- Multiple site/endpoint support
8+
- API key rotation
9+
- Rate limiting
10+
- SOCKS5 and HTTP proxy support
11+
- Detailed logging system
12+
- Docker support
13+
- CORS enabled
14+
- Customizable base path
15+
16+
## Quick Start
17+
18+
### Using Docker Compose
19+
20+
1. Clone the repository
21+
2. Configure your settings:
22+
- Copy `.env.example` to `.env`
23+
- Copy `config.json.example` to `config.json`
24+
3. Run the service:
25+
```bash
26+
docker-compose up -d
27+
```
28+
29+
### Manual Setup
30+
31+
1. Install Go 1.22 or later
32+
2. Configure your settings in `config.json`
33+
3. Run the service:
34+
```bash
35+
go run main.go
36+
```
37+
38+
## Configuration
39+
40+
### Environment Variables
41+
42+
- `PORT`: Server port (default: 3003)
43+
- `LOG_LEVEL`: Logging level (debug, info, warn, error)
44+
- `COMPOSE_PROJECT_NAME`: Docker compose project name
45+
- `UID`: User ID for Docker
46+
- `GID`: Group ID for Docker
47+
- `DC_HTTP_PORT`: Docker container HTTP port
48+
49+
### Config.json Structure
50+
51+
```json
52+
{
53+
"GLOBAL_SETTINGS": {
54+
"DIRECT_ACCESS": false,
55+
"BASE_PATH": "/proxy",
56+
"PROXIES": [
57+
"socks5://user:pass@host:port",
58+
"http://user:pass@host:port"
59+
]
60+
},
61+
"SITES": {
62+
"site-name": {
63+
"domain": "https://api.example.com",
64+
"PROXY_TYPE": "header|query|path|direct",
65+
"KEY": "X-Api-Key",
66+
"VALUES": [
67+
{"key1": 3},
68+
{"key2": 3}
69+
]
70+
}
71+
}
72+
}
73+
```
74+
75+
#### Configuration Fields
76+
77+
- `BASE_PATH`: Base URL path for all proxy endpoints (default: "/proxy")
78+
- `PROXY_TYPE`: How the proxy should handle requests
79+
- Supported types: `header`, `query`, `path`, or `direct`
80+
- For `direct` type, `VALUES` should contain full target URLs
81+
- `KEY`: API key header name or query parameter name
82+
- `VALUES`: API keys with their rate limits
83+
84+
## Usage
85+
86+
### Making Requests
87+
88+
The proxy service accepts requests in the following format:
89+
90+
```
91+
http://localhost:{PORT}{BASE_PATH}/{site-name}/{endpoint}
92+
```
93+
94+
Examples:
95+
```bash
96+
# Default configuration
97+
curl http://localhost:3003/proxy/myip4/
98+
99+
# Custom base path configuration
100+
curl http://localhost:3003/api/myip4/
101+
```
102+
103+
### Logs
104+
105+
Logs are written to both console and `proxy.log` file. View Docker container logs using:
106+
107+
```bash
108+
docker-compose logs api-proxy
109+
```
110+
111+
## Development
112+
113+
### Prerequisites
114+
115+
- Go 1.22+
116+
- Docker and Docker Compose (for containerized deployment)
117+
118+
### Building
119+
120+
```bash
121+
# Build locally
122+
go build -o api-proxy
123+
124+
# Build Docker image
125+
docker-compose build
126+
```
127+
128+
## License
129+
130+
MIT License

0 commit comments

Comments
 (0)