-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (47 loc) · 1.34 KB
/
main.go
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
package main
import (
"flag"
"net"
"net/http"
"os"
"path/filepath"
"github.com/Ink-33/ProfileTool/config"
"github.com/Ink-33/ProfileTool/internal/image"
"github.com/Ink-33/ProfileTool/service"
"github.com/Ink-33/ProfileTool/utils"
log "github.com/Ink-33/logger"
)
func init() {
log.SetProductName("ProfileTool")
}
var (
flagConfig string
)
func main() {
wd, _ := os.Getwd()
flag.StringVar(&flagConfig, "c", filepath.Join(wd, "config.json"), "Configuration filename")
flag.Parse()
log.Info("Reading config: %v", flagConfig)
conf, err := config.Parse(flagConfig)
if err != nil {
log.Error("Invalid config: %v", err.Error())
}
imgs, err := image.Init(conf)
if err != nil {
log.Fatal("Initing images failed: %v", err.Error())
}
log.Info("Loaded %v images.", imgs.Length())
mux := &http.ServeMux{}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Info("%v %v %v %v", utils.ReadUserIP(r), r.Method, r.URL.Path, http.StatusNotFound)
w.WriteHeader(http.StatusNotFound)
})
mux.HandleFunc(conf.Endpoint.Image, service.Image(imgs))
mux.HandleFunc(conf.Endpoint.Switch, service.Switch(imgs))
listener, err := net.Listen("tcp", conf.Listen)
if err != nil {
log.Fatal("Cannot start server: %v", err.Error())
}
log.Info("Listening on: %v", listener.Addr().String())
log.Fatal("%v", http.Serve(listener, mux))
}