Skip to content

Commit 23bbed8

Browse files
BennyThinkn0vad3v
andauthored
Refactor review (#220)
* runnable * convert is working * some refactoring * update go.mod * fix some TODOs * add TODO * update go mod * rebase onto master * fix #234 2: 5.9s - 7.6MB 4: 26s - 6.9MB * fix malloc tests * fix malloc tests * remote TODO * add X-Real-IP #236 * Better localRawImagePath * remove some wrong comments * Bump version to 0.9.0 --------- Co-authored-by: n0vad3v <[email protected]>
1 parent a8090ff commit 23bbed8

22 files changed

+705
-1387
lines changed

.github/workflows/integration-test.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626

2727
- name: Start the container
2828
run: |
29-
cp tests/glib_malloc/docker-compose.yml ./
29+
cp malloc_tests/glib_malloc/docker-compose.yml ./
3030
docker-compose up -d
3131
3232
- name: Send Requests to Server
@@ -55,7 +55,7 @@ jobs:
5555

5656
- name: Start the container
5757
run: |
58-
cp tests/jemalloc/docker-compose.yml ./
58+
cp malloc_tests/jemalloc/docker-compose.yml ./
5959
docker-compose up -d
6060
6161
- name: Send Requests to Server
@@ -65,4 +65,4 @@ jobs:
6565
6666
- name: Get container RAM stats
6767
run: |
68-
docker stats --no-stream
68+
docker stats --no-stream

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ builds/
2323
/.idea/webp_server_go.iml
2424
remote-raw/
2525
coverage.txt
26+
.DS_Store
27+
/webp_server_go

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ test: static-check
3535
go test -v -coverprofile=coverage.txt -covermode=atomic
3636

3737
clean:
38-
rm -rf builds
39-
rm -rf prefetch
38+
rm -rf builds prefetch remote-raw exhaust tools coverage.txt
39+
4040

4141
docker:
42-
DOCKER_BUILDKIT=1 docker build -t webpsh/webps .
42+
DOCKER_BUILDKIT=1 docker build -t webpsh/webps .

config.go

-71
This file was deleted.

config/config.go

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)