-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (52 loc) · 1.74 KB
/
Copy pathmain.go
File metadata and controls
63 lines (52 loc) · 1.74 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
package main
import (
"flag"
_ "image/jpeg"
_ "image/png"
"log"
"net/http"
"path/filepath"
"time"
"github.com/mseshachalam/colors/app"
cache "github.com/patrickmn/go-cache"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
// make these configurable
const (
defaultMaxRequestBodySize int64 = 1 // in mb
defaultMaxProminentColors int = 5
defaultPort string = ":8080"
defaultDiskCacheDir string = "./cache"
)
func main() {
// receive flags
portPtr := flag.String("port", defaultPort, "port number for the server to run on")
maxReqBodySizePtr := flag.Int64("max-req-body-size", defaultMaxRequestBodySize, "maximum request body size in mb")
maxProminentColorsPtr := flag.Int("max-prominent-colors", defaultMaxProminentColors, "maximum promiment colors that can be used to limit the user's choice")
diskCacheDirPtr := flag.String("disk-cache-dir", defaultDiskCacheDir, "disk cache directory")
flag.Parse()
var c = cache.New(5*time.Minute, 10*time.Minute)
absPath, err := filepath.Abs(*diskCacheDirPtr)
if err != nil {
log.Fatalln(err)
} else {
log.Printf("using %s as cache dir while fetching images from urls\n", absPath)
}
app := &app.App{
MaxBodySizeInBytes: *maxReqBodySizePtr << 20, // in bytes
MaxProminentColors: *maxProminentColorsPtr,
Cache: c,
DiskCacheDir: absPath,
}
r := mux.NewRouter()
r.HandleFunc("/", app.ProminentColorsFinderHandler).Methods(http.MethodPost).Headers("Content-Type", "application/json") // add rate limiting
srv := &http.Server{
Handler: handlers.CORS()(r),
Addr: *portPtr,
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 2 * time.Second,
ReadTimeout: 2 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}