forked from webp-sh/webp_server_go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebp-server.go
More file actions
119 lines (103 loc) · 3.05 KB
/
Copy pathwebp-server.go
File metadata and controls
119 lines (103 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"regexp"
"runtime"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
log "github.com/sirupsen/logrus"
)
func loadConfig(path string) Config {
jsonObject, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
decoder := json.NewDecoder(jsonObject)
_ = decoder.Decode(&config)
_ = jsonObject.Close()
return config
}
func deferInit() {
flag.StringVar(&configPath, "config", "config.json", "/path/to/config.json. (Default: ./config.json)")
flag.BoolVar(&prefetch, "prefetch", false, "Prefetch and convert image to webp")
flag.IntVar(&jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.")
flag.BoolVar(&dumpConfig, "dump-config", false, "Print sample config.json")
flag.BoolVar(&dumpSystemd, "dump-systemd", false, "Print sample systemd service file.")
flag.BoolVar(&verboseMode, "v", false, "Verbose, print out debug info.")
flag.BoolVar(&showVersion, "V", false, "Show version information.")
flag.Parse()
// Logrus
log.SetOutput(os.Stdout)
log.SetReportCaller(true)
Formatter := &log.TextFormatter{
EnvironmentOverrideColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
return fmt.Sprintf("[%d:%s()]", f.Line, f.Function), ""
},
}
log.SetFormatter(Formatter)
if verboseMode {
log.SetLevel(log.DebugLevel)
log.Debug("Debug mode is enable!")
} else {
log.SetLevel(log.InfoLevel)
}
}
func switchProxyMode() {
// Check for remote address
matched, _ := regexp.MatchString(`^https?://`, config.ImgPath)
proxyMode = false
if matched {
proxyMode = true
} else {
_, err := os.Stat(config.ImgPath)
if err != nil {
log.Fatalf("Your image path %s is incorrect.Please check and confirm.", config.ImgPath)
}
}
}
func main() {
// Our banner
banner := fmt.Sprintf(`
▌ ▌ ▌ ▛▀▖ ▞▀▖ ▞▀▖
▌▖▌▞▀▖▛▀▖▙▄▘ ▚▄ ▞▀▖▙▀▖▌ ▌▞▀▖▙▀▖ ▌▄▖▞▀▖
▙▚▌▛▀ ▌ ▌▌ ▖ ▌▛▀ ▌ ▐▐ ▛▀ ▌ ▌ ▌▌ ▌
▘ ▘▝▀▘▀▀ ▘ ▝▀ ▝▀▘▘ ▘ ▝▀▘▘ ▝▀ ▝▀
Webp Server Go - v%s
Develop by WebP Server team. https://github.com/webp-sh`, version)
deferInit()
// process cli params
if dumpConfig {
fmt.Println(sampleConfig)
os.Exit(0)
}
if dumpSystemd {
fmt.Println(sampleSystemd)
os.Exit(0)
}
if showVersion {
fmt.Printf("\n %c[1;32m%s%c[0m\n\n", 0x1B, banner+"", 0x1B)
os.Exit(0)
}
go autoUpdate()
config = loadConfig(configPath)
switchProxyMode()
if prefetch {
go prefetchImages(config.ImgPath, config.ExhaustPath)
}
app := fiber.New(fiber.Config{
ServerHeader: "Webp-Server-Go",
DisableStartupMessage: true,
})
app.Use(logger.New())
listenAddress := config.Host + ":" + config.Port
app.Get("/*", convert)
fmt.Printf("\n %c[1;32m%s%c[0m\n\n", 0x1B, banner, 0x1B)
fmt.Println("Webp-Server-Go is Running on http://" + listenAddress)
_ = app.Listen(listenAddress)
}