|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "runtime" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/patrickmn/go-cache" |
| 13 | + log "github.com/sirupsen/logrus" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + TimeDateFormat = "2006-01-02 15:04:05" |
| 18 | + FiberLogFormat = "${ip} - [${time}] ${method} ${url} ${status} ${referer} ${ua}\n" |
| 19 | + WebpMax = 16383 |
| 20 | + AvifMax = 65536 |
| 21 | + RemoteRaw = "remote-raw" |
| 22 | + |
| 23 | + SampleConfig = ` |
| 24 | +{ |
| 25 | + "HOST": "127.0.0.1", |
| 26 | + "PORT": "3333", |
| 27 | + "QUALITY": "80", |
| 28 | + "IMG_PATH": "./pics", |
| 29 | + "EXHAUST_PATH": "./exhaust", |
| 30 | + "ALLOWED_TYPES": ["jpg","png","jpeg","bmp"], |
| 31 | + "ENABLE_AVIF": false, |
| 32 | + "ENABLE_EXTRA_PARAMS": false |
| 33 | +}` |
| 34 | + |
| 35 | + SampleSystemd = ` |
| 36 | +[Unit] |
| 37 | +Description=WebP Server Go |
| 38 | +Documentation=https://github.com/webp-sh/webp_server_go |
| 39 | +After=nginx.target |
| 40 | +
|
| 41 | +[Service] |
| 42 | +Type=simple |
| 43 | +StandardError=journal |
| 44 | +WorkingDirectory=/opt/webps |
| 45 | +ExecStart=/opt/webps/webp-server --config /opt/webps/config.json |
| 46 | +Restart=always |
| 47 | +RestartSec=3s |
| 48 | +
|
| 49 | +[Install] |
| 50 | +WantedBy=multi-user.target` |
| 51 | +) |
| 52 | + |
| 53 | +var ( |
| 54 | + configPath string |
| 55 | + Jobs int |
| 56 | + DumpSystemd bool |
| 57 | + DumpConfig bool |
| 58 | + ShowVersion bool |
| 59 | + ProxyMode bool |
| 60 | + Prefetch bool |
| 61 | + Config jsonFile |
| 62 | + Version = "0.9.0" |
| 63 | + WriteLock = cache.New(5*time.Minute, 10*time.Minute) |
| 64 | +) |
| 65 | + |
| 66 | +type jsonFile struct { |
| 67 | + Host string `json:"HOST"` |
| 68 | + Port string `json:"PORT"` |
| 69 | + ImgPath string `json:"IMG_PATH"` |
| 70 | + Quality int `json:"QUALITY,string"` |
| 71 | + AllowedTypes []string `json:"ALLOWED_TYPES"` |
| 72 | + ExhaustPath string `json:"EXHAUST_PATH"` |
| 73 | + EnableAVIF bool `json:"ENABLE_AVIF"` |
| 74 | + EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"` |
| 75 | +} |
| 76 | + |
| 77 | +func init() { |
| 78 | + flag.StringVar(&configPath, "config", "config.json", "/path/to/config.json. (Default: ./config.json)") |
| 79 | + flag.BoolVar(&Prefetch, "prefetch", false, "Prefetch and convert image to webp") |
| 80 | + flag.IntVar(&Jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.") |
| 81 | + flag.BoolVar(&DumpConfig, "dump-config", false, "Print sample config.json") |
| 82 | + flag.BoolVar(&DumpSystemd, "dump-systemd", false, "Print sample systemd service file.") |
| 83 | + flag.BoolVar(&ShowVersion, "V", false, "Show version information.") |
| 84 | + flag.Parse() |
| 85 | + Config = loadConfig() |
| 86 | + switchProxyMode() |
| 87 | +} |
| 88 | + |
| 89 | +func loadConfig() (config jsonFile) { |
| 90 | + jsonObject, err := os.Open(configPath) |
| 91 | + if err != nil { |
| 92 | + log.Fatal(err) |
| 93 | + } |
| 94 | + decoder := json.NewDecoder(jsonObject) |
| 95 | + _ = decoder.Decode(&config) |
| 96 | + _ = jsonObject.Close() |
| 97 | + return config |
| 98 | +} |
| 99 | + |
| 100 | +type ExtraParams struct { |
| 101 | + Width int // in px |
| 102 | + Height int // in px |
| 103 | +} |
| 104 | + |
| 105 | +// String : convert ExtraParams to string, used to generate cache path |
| 106 | +func (e *ExtraParams) String() string { |
| 107 | + return fmt.Sprintf("_width=%d&height=%d", e.Width, e.Height) |
| 108 | +} |
| 109 | + |
| 110 | +func switchProxyMode() { |
| 111 | + matched, _ := regexp.MatchString(`^https?://`, Config.ImgPath) |
| 112 | + if matched { |
| 113 | + ProxyMode = true |
| 114 | + } |
| 115 | +} |
0 commit comments