69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
|
/*
|
||
|
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 (
|
||
|
"fmt"
|
||
|
"github.com/dalu/chromedom/server"
|
||
|
"github.com/spf13/cobra"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
// serveCmd represents the serve command
|
||
|
var serveCmd = &cobra.Command{
|
||
|
Use: "serve",
|
||
|
Short: "",
|
||
|
Long: ``,
|
||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||
|
binary := cmd.Flag("binary").Value.String()
|
||
|
s := http.DefaultServeMux
|
||
|
h := server.NewHandler(binary)
|
||
|
s.Handle("/", h)
|
||
|
return http.ListenAndServe(":9292", s)
|
||
|
// return RunUnix("/tmp/chromedom.sock", s)
|
||
|
},
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
defer listener.Close()
|
||
|
os.Chmod(file, 0777)
|
||
|
err = http.Serve(listener, engine)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
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")
|
||
|
}
|