exposed configuration

This commit is contained in:
Darko Luketic 2019-11-01 16:24:22 +01:00
parent 6eaff883d4
commit 9f85a8901e
2 changed files with 17 additions and 18 deletions

View File

@ -16,10 +16,17 @@ limitations under the License.
package cmd
import (
"github.com/dalu/chromedom/server"
"github.com/spf13/cobra"
"net/http"
"time"
"github.com/dalu/chromedom/server"
"github.com/spf13/cobra"
)
var (
binary string
duration time.Duration
maxmem int
)
// serveCmd represents the serve command
@ -28,18 +35,17 @@ var serveCmd = &cobra.Command{
Short: "",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
binary := cmd.Flag("binary").Value.String()
s := http.DefaultServeMux
h := server.NewHandler(binary, 30*time.Minute)
h := server.NewHandler(binary, duration, maxmem)
s.Handle("/", h)
return http.ListenAndServe(":9292", s)
// return RunUnix("/tmp/chromedom.sock", s)
// return runUnix("/tmp/chromedom.sock", s)
},
}
/*
func RunUnix(file string, engine *http.ServeMux) (err error) {
func runUnix(file string, engine *http.ServeMux) (err error) {
fmt.Printf("Listening and serving HTTP on unix:/%s", file)
os.Remove(file)
listener, err := net.Listen("unix", file)
@ -56,14 +62,7 @@ func RunUnix(file string, engine *http.ServeMux) (err error) {
func init() {
rootCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
serveCmd.Flags().String("binary", "/bin/chromium", "chrome, chromium or headless binary path")
serveCmd.Flags().StringVarP(&binary, "binary", "b", "/usr/bin/google-chrome", "chrome, chromium or headless binary path")
serveCmd.Flags().DurationVarP(&duration, "duration", "d", 60*time.Minute, "how long the values live in the cache")
serveCmd.Flags().IntVarP(&maxmem, "maxmem", "m", 500*1024*1024, "maximum memory the cache may use in bytes")
}

View File

@ -35,7 +35,7 @@ type Handler struct {
cache *bigcache.BigCache
}
func NewHandler(binary string, eviction time.Duration) *Handler {
func NewHandler(binary string, eviction time.Duration, maxmem int) *Handler {
h := new(Handler)
h.binary = binary
h.uid = syscall.Getuid()
@ -64,7 +64,7 @@ func NewHandler(binary string, eviction time.Duration) *Handler {
}
h.logger = logrus.StandardLogger()
cacheConf := bigcache.DefaultConfig(eviction)
cacheConf.HardMaxCacheSize = 500 * 1024 * 1024 // 500MB
cacheConf.HardMaxCacheSize = maxmem
cache, e := bigcache.NewBigCache(cacheConf)
if e != nil {
h.logger.Fatal(e.Error())