コード例 #1
0
ファイル: alexandria.go プロジェクト: hawx/alexandria
func main() {
	flag.Parse()

	var conf struct {
		Secret    string
		DbPath    string `toml:"database"`
		BooksPath string `toml:"library"`
		Uberich   struct {
			AppName    string
			AppURL     string
			UberichURL string
			Secret     string
		}
	}
	if _, err := toml.DecodeFile(*settingsPath, &conf); err != nil {
		log.Fatal("toml:", err)
	}

	store := uberich.NewStore(conf.Secret)
	uberich := uberich.NewClient(conf.Uberich.AppName, conf.Uberich.AppURL, conf.Uberich.UberichURL, conf.Uberich.Secret, store)

	db := data.Open(conf.DbPath)
	defer db.Close()

	es := events.New()
	defer es.Close()

	shield := func(h http.Handler) http.Handler {
		return uberich.Protect(h, http.NotFoundHandler())
	}

	route.Handle("/", mux.Method{"GET": uberich.Protect(handlers.List(true), handlers.List(false))})
	route.Handle("/books", shield(handlers.AllBooks(db, es)))
	route.Handle("/books/:id", shield(handlers.Books(db, es)))
	route.Handle("/editions/:id", shield(handlers.Editions(db, conf.BooksPath)))
	route.Handle("/upload", shield(handlers.Upload(db, es, conf.BooksPath)))

	route.Handle("/sign-in", uberich.SignIn("/"))
	route.Handle("/sign-out", uberich.SignOut("/"))
	route.Handle("/events", es)
	route.Handle("/assets/*filepath", http.StripPrefix("/assets/", assets.Server(map[string]string{
		"main.js":        assets.MainJs,
		"mustache.js":    assets.MustacheJs,
		"tablesorter.js": assets.TablesorterJs,
		"tablefilter.js": assets.TablefilterJs,
		"styles.css":     assets.StylesCss,
	})))

	serve.Serve(*port, *socket, filters.Log(route.Default))
}
コード例 #2
0
ファイル: ggg.go プロジェクト: hawx/ggg
func main() {
	var (
		settingsPath = flag.String("settings", "./settings.toml", "Path to 'settings.toml'")
		port         = flag.String("port", "8080", "Port to run on")
		socket       = flag.String("socket", "", "")
	)
	flag.Parse()

	var conf *Conf
	if _, err := toml.DecodeFile(*settingsPath, &conf); err != nil {
		log.Fatal("toml: ", err)
	}

	db := repos.Open(conf.DbPath, conf.GitDir)
	defer db.Close()

	store := uberich.NewStore(conf.Secret)
	uberich := uberich.NewClient(conf.Uberich.AppName, conf.Uberich.AppURL, conf.Uberich.UberichURL, conf.Uberich.Secret, store)

	shield := func(h http.Handler) http.Handler {
		return uberich.Protect(h, http.NotFoundHandler())
	}

	list := handlers.List(db, conf.Title, conf.URL)
	repo := handlers.Repo(db, conf.Title, conf.URL, uberich.Protect)

	route.Handle("/", mux.Method{"GET": uberich.Protect(list.All, list.Public)})

	route.HandleFunc("/:name/*path", func(w http.ResponseWriter, r *http.Request) {
		vars := route.Vars(r)

		if strings.HasSuffix(vars["name"], ".git") {
			repo.Git.ServeHTTP(w, r)
			return
		}

		repo.Html.ServeHTTP(w, r)
	})
	route.Handle("/:name/edit", shield(handlers.Edit(db, conf.Title)))
	route.Handle("/:name/delete", shield(handlers.Delete(db)))

	route.Handle("/-/create", shield(handlers.Create(db, conf.Title)))
	route.Handle("/-/sign-in", uberich.SignIn("/"))
	route.Handle("/-/sign-out", uberich.SignOut("/"))

	route.Handle("/assets/styles.css", mux.Method{"GET": assets.Styles})

	serve.Serve(*port, *socket, filters.Log(route.Default))
}