示例#1
0
func main() {
	flagWork := flag.String("work", "work", "")
	flagAddr := flag.String("addr", ":4020", "")
	flag.Parse()

	root, err := rootPath()
	if err != nil {
		panic(err)
	}

	r := pork.NewRouter(
		func(status int, r *http.Request) {
			log.Printf("%d - %s", status, r.URL.Path)
		}, nil, nil)

	r.RespondWith("/", pork.Content(pork.NewConfig(pork.None),
		http.Dir(filepath.Join(root, "src/pub"))))
	r.RespondWith("/data/", pork.Content(pork.NewConfig(pork.None),
		http.Dir(*flagWork)))

	log.Printf("Server running on address %s\n", *flagAddr)
	if err := http.ListenAndServe(*flagAddr, r); err != nil {
		panic(err)
	}
}
示例#2
0
文件: main.go 项目: kellegous/404
func setup(r pork.Router, ctx *context.Context) error {
	root, err := findRoot()
	if err != nil {
		return err
	}

	c := pork.Content(pork.NewConfig(pork.None),
		http.Dir(root))

	// serves up static content
	r.RespondWithFunc("/", func(w pork.ResponseWriter, r *http.Request) {
		_, err := auth.SessionFromRequest(ctx, r)
		if err != nil {
			http.Redirect(w, r, "/auth/a", http.StatusTemporaryRedirect)
			return
		}

		c.ServePork(w, r)
	})

	// some debugging handlers
	r.RespondWithFunc("/info", func(w pork.ResponseWriter, r *http.Request) {
		sess, user, err := auth.UserFromRequest(ctx, r)
		if err != nil {
			panic(err)
		}

		fmt.Fprintln(w, sess, user)
	})

	return nil
}
示例#3
0
文件: pork.go 项目: kellegous/pork
func mainBuild(args []string) {
	flags := flag.NewFlagSet("", flag.ExitOnError)
	flagOut := flags.String("out", "", "")
	flagOpt := flags.String("opt", "None", "")
	flags.Parse(args)

	lvl, err := parseOptimization(*flagOpt)
	if err != nil {
		fmt.Fprintf(os.Stderr, "invalid optimization level: %s\n", *flagOpt)
	}

	var dirs []http.Dir
	if flags.NArg() == 0 {
		dirs = append(dirs, http.Dir("."))
	} else {
		for _, arg := range flags.Args() {
			dirs = append(dirs, http.Dir(arg))
		}
	}

	for _, dir := range dirs {
		out := dir
		if *flagOut != "" {
			out = http.Dir(*flagOut)
		}

		if _, err := pork.Content(pork.NewConfig(lvl), dir).Productionize(out); err != nil {
			log.Panic(err)
		}
	}
}
示例#4
0
文件: serve.go 项目: kellegous/grund
func main() {
	flagAddr := flag.String("addr", ":2018", "")
	flagData := flag.String("data", "data", "")
	flagProd := flag.Bool("prod", false, "")

	flag.Parse()

	if _, err := os.Stat(*flagData); err != nil {
		if err := os.MkdirAll(*flagData, os.ModePerm); err != nil {
			panic(err)
		}
	}

	root, err := rootPath()
	if err != nil {
		panic(err)
	}

	pub := filepath.Join(root, "pub")
	c := pork.Content(pork.NewConfig(pork.None), http.Dir(pub))

	if *flagProd {
		t, err := ioutil.TempDir(os.TempDir(), "grund-")
		if err != nil {
			panic(err)
		}

		if _, err := c.Productionize(http.Dir(t)); err != nil {
			panic(err)
		}

		return
	}

	r := pork.NewRouter(
		func(status int, r *http.Request) {
			log.Printf("%d - %s", status, r.URL.Path)
		}, nil, nil)

	r.Handle("/", &handler{
		c: c,
		t: filepath.Join(root, "index.tpl.html"),
		d: pub,
	})

	setup(*flagData, r)
	if err := http.ListenAndServe(*flagAddr, r); err != nil {
		panic(err)
	}
}
示例#5
0
func main() {
	flagAddr := flag.String("addr", ":8080", "")
	flag.Parse()

	root, err := findRoot()
	if err != nil {
		panic(err)
	}

	fmt.Printf("root = %s\n", root)

	r := pork.NewRouter(nil, nil, nil)

	r.RespondWith("/", pork.Content(pork.NewConfig(pork.None),
		http.Dir(filepath.Join(root, "src/pub"))))

	if err := http.ListenAndServe(*flagAddr, r); err != nil {
		panic(err)
	}
}
示例#6
0
文件: pork-cli.go 项目: progers/pork
func main() {
	addr := flag.String("addr", ":6080", "The address to use")
	flag.Parse()

	var dirs []http.Dir
	if flag.NArg() == 0 {
		dirs = []http.Dir{http.Dir(".")}
	} else {
		dirs = make([]http.Dir, flag.NArg())
		for i, arg := range flag.Args() {
			dirs[i] = http.Dir(arg)
		}
	}

	// setup a simple router
	r := pork.NewRouter(func(status int, r *http.Request) {
		log.Printf("%d %s %s %s", status, r.RemoteAddr, r.Method, r.URL.String())
	}, nil, nil)
	r.Handle("/", pork.Content(pork.None, dirs...))
	http.ListenAndServe(*addr, r)
}
示例#7
0
文件: pork.go 项目: donjaime/pork
func main() {
	flagAddr := flag.String("addr", ":8082", "address to bind")

	flag.Parse()

	var dirs []http.Dir
	if flag.NArg() == 0 {
		dirs = append(dirs, http.Dir("."))
	} else {
		for _, arg := range flag.Args() {
			dirs = append(dirs, http.Dir(arg))
		}
	}

	r := pork.NewRouter(func(status int, r *http.Request) {
		log.Printf("[%d] %s", status, r.RequestURI)
	}, nil, nil)
	r.Handle("/", pork.Content(pork.NewConfig(pork.None), dirs...))

	if err := http.ListenAndServe(*flagAddr, r); err != nil {
		log.Panic(err)
	}
}