chromedom/cmd/serve.go

69 lines
1.8 KiB
Go
Raw Permalink Normal View History

2019-10-29 21:37:31 +01:00
/*
Copyright © 2019 Darko Luketic <info@icod.de>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"net/http"
2019-11-01 14:25:11 +01:00
"time"
2019-11-01 16:24:22 +01:00
"github.com/dalu/chromedom/server"
"github.com/spf13/cobra"
)
var (
binary string
duration time.Duration
maxmem int
2019-10-29 21:37:31 +01:00
)
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
s := http.DefaultServeMux
2019-11-01 16:24:22 +01:00
h := server.NewHandler(binary, duration, maxmem)
2019-10-29 21:37:31 +01:00
s.Handle("/", h)
return http.ListenAndServe(":9292", s)
2019-11-01 16:24:22 +01:00
// return runUnix("/tmp/chromedom.sock", s)
2019-10-29 21:37:31 +01:00
},
}
2019-11-01 14:25:11 +01:00
/*
2019-11-01 16:24:22 +01:00
func runUnix(file string, engine *http.ServeMux) (err error) {
2019-10-29 21:37:31 +01:00
fmt.Printf("Listening and serving HTTP on unix:/%s", file)
os.Remove(file)
listener, err := net.Listen("unix", file)
if err != nil {
return
}
defer listener.Close()
os.Chmod(file, 0777)
err = http.Serve(listener, engine)
return
}
2019-11-01 14:25:11 +01:00
*/
2019-10-29 21:37:31 +01:00
func init() {
rootCmd.AddCommand(serveCmd)
2019-11-01 16:24:22 +01:00
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")
2019-11-01 17:32:59 +01:00
serveCmd.Flags().IntVarP(&maxmem, "maxmem", "m", 512, "maximum memory in MB the cache may use")
2019-10-29 21:37:31 +01:00
}