github.phpd.cn/thought-machine/please@v12.2.0+incompatible/tools/cache/http_server_main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 "time" 7 8 "gopkg.in/op/go-logging.v1" 9 10 "cli" 11 "tools/cache/server" 12 ) 13 14 var log = logging.MustGetLogger("http_cache_server") 15 16 var opts struct { 17 Usage string `usage:"http_cache_server is a server for Please's remote HTTP cache.\n\nSee https://please.build/cache.html for more information."` 18 Verbosity int `short:"v" long:"verbosity" description:"Verbosity of output (higher number = more output, default 2 -> notice, warnings and errors only)" default:"2"` 19 Port int `short:"p" long:"port" description:"Port to serve on" default:"8080"` 20 Dir string `short:"d" long:"dir" description:"Directory to write into" default:"plz-http-cache"` 21 LogFile string `long:"log_file" description:"File to log to (in addition to stdout)"` 22 23 CleanFlags struct { 24 LowWaterMark cli.ByteSize `short:"l" long:"low_water_mark" description:"Size of cache to clean down to" default:"18G"` 25 HighWaterMark cli.ByteSize `short:"i" long:"high_water_mark" description:"Max size of cache to clean at" default:"20G"` 26 CleanFrequency cli.Duration `short:"f" long:"clean_frequency" description:"Frequency to clean cache at" default:"10m"` 27 MaxArtifactAge cli.Duration `short:"m" long:"max_artifact_age" description:"Clean any artifact that's not been read in this long" default:"720h"` 28 } `group:"Options controlling when to clean the cache"` 29 } 30 31 func main() { 32 cli.ParseFlagsOrDie("Please HTTP cache server", "5.5.0", &opts) 33 cli.InitLogging(opts.Verbosity) 34 if opts.LogFile != "" { 35 cli.InitFileLogging(opts.LogFile, opts.Verbosity) 36 } 37 log.Notice("Initialising cache server...") 38 cache := server.NewCache(opts.Dir, time.Duration(opts.CleanFlags.CleanFrequency), 39 time.Duration(opts.CleanFlags.MaxArtifactAge), 40 uint64(opts.CleanFlags.LowWaterMark), uint64(opts.CleanFlags.HighWaterMark)) 41 log.Notice("Starting up http cache server on port %d...", opts.Port) 42 router := server.BuildRouter(cache) 43 http.Handle("/", router) 44 http.ListenAndServe(fmt.Sprintf(":%d", opts.Port), router) 45 }