Wood Chen

random-api-go: Go Rewrite

0 comments89 views447 words

This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文

Latest updates at: https://q58.club/t/topic/127?u=wood

Repository

https://github.com/woodchen-ink/random-api-go

Demo and API URL

https://random-api.czl.net


Background

The original nodejs and worker versions: https://q58.club/t/topic/20

I rewrote this in go to address performance and concurrency issues. After the rewrite, each request typically takes 5 microseconds to process, with memory usage as low as 4M.

image|294x131 image|147x290


Features

  • Dynamic loading and caching of CSV file contents
  • Random image and video serving
  • Customizable URL path configuration
  • Docker support for easy deployment and scaling
  • Detailed logging

Deployment

docker compose:

https://github.com/woodchen-ink/random-api-go/blob/main/docker-compose.yml

Notes:

  1. The public directory holds files such as index.html, index.md, and css. When the container is first created, these files are automatically copied from the project. Modify them as needed.
  2. The data directory holds server.log and stats.json, which contain logs and statistics, respectively.
  3. BASE_URL is the base URL for requesting the csv files listed in url.json. It automatically handles https and supports paths.

Detailed Steps

  1. Create a csv file containing image URLs, one per line, such as ecy.csv, and make it accessible at https://file.example.com/random/pic/ecy.csv ; The CSV file should contain one URL per line. For example:

    https://example.com/image1.jpg
    https://example.com/image2.jpg
    https://example.com/image3.jpg
    
  2. Create your url.json and make it accessible at https://file.example.com/random/url.json, for example:

    {
      "pic": {
        "ecy": "pic/ecy.csv"
      }
    }
    

    If you use a different prefix instead of pic, you need to add it at the following location in the code, following the example.

https://github.com/woodchen-ink/random-api-go/blob/cbeadf2e48892c37278f7a5d00ed047cb9f04968/main.go#L61

  1. Deploy with docker using this docker-compose.yml:

    services:
      random-api-go:
        container_name: random-api-go
        image: woodchen/random-api-go:latest
        ports:
          - "5003:5003"
        volumes:
          - ./public:/root/public
          - ./data:/root/data
        environment:
          - TZ=Asia/Shanghai
          - BASE_URL=https://file.example.com/random
        restart: unless-stopped
    
  2. Configure nginx as a reverse proxy for port 5003. Example configuration:

    location ^~ / {
        proxy_pass http://127.0.0.1:5003; 
        proxy_set_header Host $host; 
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_set_header REMOTE-HOST $remote_addr; 
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection "upgrade"; 
        proxy_set_header X-Forwarded-Proto $scheme; 
        proxy_http_version 1.1; 
        add_header X-Cache $upstream_cache_status; 
        add_header Cache-Control no-cache; 
        proxy_ssl_server_name off; 
        add_header Strict-Transport-Security "max-age=31536000"; 
    }
    
  3. Visiting 域名/pic/ecy returns a 302 redirect to an image URL.

  4. Visiting 域名 displays the home page. Customize its content in public/index.html and public/index.md.

Notes

  1. The current version no longer periodically clears the csv cache and fetches the files again, as this is unnecessary and slows things down. Restart the container after modifying the files to fetch them again;
  2. If you run into problems, check the container logs. They are detailed and written in Chinese;
  3. If you do not want to host static files separately, place url.json and pic/ecy.csv in the public directory and leave BASE_URL unset. In this case, requests use the domain serving the current project, meaning they will request https://random.com/url.json and https://random.com/pic/ecy.csv;
  4. Leave a comment if you have any other questions.

Example url.json with multiple paths:

{
  "pic": {
    "all": "url/pic/all.csv",
    "fj": "url/pic/fj.csv",
    "czlwb": "url/pic/czl-website-background.csv",
    "truegirl": "url/pic/truegirl.csv",
    "truegirl1": "url/pic/truegirl1.csv",
    "truegirl2": "url/pic/truegirl2.csv",
    "girl-gif": "url/pic/girl-gif.csv",
    "ecy": "url/pic/ecy.csv",
    "ecy1": "url/pic/ecy1.csv",
    "ecy2": "url/pic/ecy2.csv",
    "ai": "url/pic/ai.csv",
    "loading": "url/pic/loading.csv"
  },
  "video": {
    "all": "url/video/all.csv"
  }
}

Last updated 2025-02-15

Related posts

Comments 0