Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
3a9999ce6d | |||
f1aa34fc56 | |||
69dd863b6c | |||
9f85a8901e |
14
README.md
14
README.md
@ -45,11 +45,8 @@ In `/etc/nginx/dumpdom.conf`, paste this:
|
|||||||
|
|
||||||
```
|
```
|
||||||
map $http_user_agent $dumpdom_ua {
|
map $http_user_agent $dumpdom_ua {
|
||||||
|
|
||||||
default 0;
|
default 0;
|
||||||
|
|
||||||
"~Prerender" 0;
|
|
||||||
|
|
||||||
"~*googlebot" 1;
|
"~*googlebot" 1;
|
||||||
"~*bingbot" 1;
|
"~*bingbot" 1;
|
||||||
"~*yandex" 1;
|
"~*yandex" 1;
|
||||||
@ -133,10 +130,10 @@ server {
|
|||||||
if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") {
|
if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") {
|
||||||
set $dumpdom 0;
|
set $dumpdom 0;
|
||||||
}
|
}
|
||||||
if ($prerender = 1) {
|
if ($dumpdom = 1) {
|
||||||
proxy_pass http://127.0.0.1:9292;
|
proxy_pass http://127.0.0.1:9292;
|
||||||
}
|
}
|
||||||
if ($prerender = 0) {
|
if ($dumpdom = 0) {
|
||||||
rewrite .* /index.html break;
|
rewrite .* /index.html break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -230,16 +227,13 @@ server {
|
|||||||
if ($args ~ "_escaped_fragment_") {
|
if ($args ~ "_escaped_fragment_") {
|
||||||
set $dumpdom 1;
|
set $dumpdom 1;
|
||||||
}
|
}
|
||||||
if ($http_user_agent ~ "Prerender") {
|
|
||||||
set $dumpdom 0;
|
|
||||||
}
|
|
||||||
if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") {
|
if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") {
|
||||||
set $dumpdom 0;
|
set $dumpdom 0;
|
||||||
}
|
}
|
||||||
if ($prerender = 1) {
|
if ($dumpdom = 1) {
|
||||||
proxy_pass http://127.0.0.1:9292;
|
proxy_pass http://127.0.0.1:9292;
|
||||||
}
|
}
|
||||||
if ($prerender = 0) {
|
if ($dumpdom = 0) {
|
||||||
rewrite .* /index.html break;
|
rewrite .* /index.html break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
50
cmd/serve.go
50
cmd/serve.go
@ -16,10 +16,22 @@ limitations under the License.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/dalu/chromedom/server"
|
"github.com/dalu/chromedom/server"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"net/http"
|
)
|
||||||
"time"
|
|
||||||
|
var (
|
||||||
|
binary string
|
||||||
|
duration time.Duration
|
||||||
|
maxmem int
|
||||||
|
addr string
|
||||||
)
|
)
|
||||||
|
|
||||||
// serveCmd represents the serve command
|
// serveCmd represents the serve command
|
||||||
@ -28,18 +40,19 @@ var serveCmd = &cobra.Command{
|
|||||||
Short: "",
|
Short: "",
|
||||||
Long: ``,
|
Long: ``,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
binary := cmd.Flag("binary").Value.String()
|
|
||||||
s := http.DefaultServeMux
|
s := http.DefaultServeMux
|
||||||
h := server.NewHandler(binary, 30*time.Minute)
|
h := server.NewHandler(binary, duration, maxmem)
|
||||||
s.Handle("/", h)
|
s.Handle("/", h)
|
||||||
return http.ListenAndServe(":9292", s)
|
if strings.HasPrefix(addr, "unix:") {
|
||||||
// return RunUnix("/tmp/chromedom.sock", s)
|
return runUnix(strings.TrimPrefix(addr, "unix:"), s)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Listening and serving HTTP on %s", addr)
|
||||||
|
return http.ListenAndServe(addr, 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)
|
fmt.Printf("Listening and serving HTTP on unix:/%s", file)
|
||||||
os.Remove(file)
|
os.Remove(file)
|
||||||
listener, err := net.Listen("unix", file)
|
listener, err := net.Listen("unix", file)
|
||||||
@ -47,23 +60,18 @@ func RunUnix(file string, engine *http.ServeMux) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
os.Chmod(file, 0777)
|
if err := os.Chmod(file, 0777); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
err = http.Serve(listener, engine)
|
err = http.Serve(listener, engine)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(serveCmd)
|
rootCmd.AddCommand(serveCmd)
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
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")
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
serveCmd.Flags().IntVarP(&maxmem, "maxmem", "m", 512, "maximum memory in MB the cache may use")
|
||||||
// and all subcommands, e.g.:
|
serveCmd.Flags().StringVarP(&addr, "addr", "a", "localhost:9292", "ip:port pair to listen on, or prefixed with unix: the unix socket path to listen on")
|
||||||
// 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")
|
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ type Handler struct {
|
|||||||
cache *bigcache.BigCache
|
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 := new(Handler)
|
||||||
h.binary = binary
|
h.binary = binary
|
||||||
h.uid = syscall.Getuid()
|
h.uid = syscall.Getuid()
|
||||||
@ -64,7 +64,7 @@ func NewHandler(binary string, eviction time.Duration) *Handler {
|
|||||||
}
|
}
|
||||||
h.logger = logrus.StandardLogger()
|
h.logger = logrus.StandardLogger()
|
||||||
cacheConf := bigcache.DefaultConfig(eviction)
|
cacheConf := bigcache.DefaultConfig(eviction)
|
||||||
cacheConf.HardMaxCacheSize = 500 * 1024 * 1024 // 500MB
|
cacheConf.HardMaxCacheSize = maxmem
|
||||||
cache, e := bigcache.NewBigCache(cacheConf)
|
cache, e := bigcache.NewBigCache(cacheConf)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
h.logger.Fatal(e.Error())
|
h.logger.Fatal(e.Error())
|
||||||
|
Reference in New Issue
Block a user