示例#1
0
文件: echo.go 项目: hawx/echo
func main() {
	flag.Parse()

	route.HandleFunc("/delay/:ms/*path", func(w http.ResponseWriter, r *http.Request) {
		ms, err := strconv.ParseInt(route.Vars(r)["ms"], 10, 64)
		if err != nil {
			log.Println(err)
			return
		}

		time.Sleep(time.Duration(ms) * time.Millisecond)
		fmt.Fprintf(w, createResponseBody(r))
	})

	route.HandleFunc("/code/:code/*path", func(w http.ResponseWriter, r *http.Request) {
		code, err := strconv.ParseInt(route.Vars(r)["code"], 10, 0)
		if err != nil {
			log.Println(err)
			return
		}

		w.WriteHeader(int(code))
		fmt.Fprint(w, createResponseBody(r))
	})

	route.HandleFunc("/*path", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, createResponseBody(r))
	})

	serve.Serve(*port, *socket, route.Default)
}
示例#2
0
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))
}
示例#3
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))
}
示例#4
0
func main() {
	var (
		settingsPath = flag.String("settings", "./settings.toml", "")
		port         = flag.String("port", "8081", "")
		socket       = flag.String("socket", "", "")
	)
	flag.Usage = func() { fmt.Println(help) }
	flag.Parse()

	if flag.NArg() == 0 {
		fmt.Println(help)
		return
	}

	opmlPath := flag.Arg(0)

	var conf *Conf
	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)

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

	http.Handle("/", mux.Method{
		"GET": uberich.Protect(
			handlers.List(opmlPath, conf.URL, conf.PathPrefix),
			handlers.Login(conf.URL, conf.PathPrefix),
		),
	})
	http.Handle("/subscribe", shield(mux.Method{
		"GET": handlers.Subscribe(opmlPath, conf.PathPrefix),
	}))
	http.Handle("/unsubscribe", shield(mux.Method{
		"GET": handlers.Unsubscribe(opmlPath, conf.PathPrefix),
	}))
	http.Handle("/sign-in", uberich.SignIn(conf.PathPrefix))
	http.Handle("/sign-out", uberich.SignOut(conf.PathPrefix))

	serve.Serve(*port, *socket, Log(http.DefaultServeMux))
}
示例#5
0
文件: phemera.go 项目: hawx/phemera
func main() {
	var (
		settingsPath = flag.String("settings", "./settings.toml", "")
		port         = flag.String("port", "8080", "")
		socket       = flag.String("socket", "", "")
	)
	flag.Parse()

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

	db := database.Open(conf.DbPath, conf.Horizon)
	defer db.Close()

	es := eventsource.New(nil, nil)
	defer es.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())
	}

	http.Handle("/", uberich.Protect(List(conf, db, true), List(conf, db, false)))
	http.Handle("/add", shield(Add(conf, db, es)))
	http.Handle("/preview", shield(Preview))
	http.Handle("/feed", Feed(conf, db))
	http.Handle("/connect", es)

	http.Handle("/sign-in", uberich.SignIn("/"))
	http.Handle("/sign-out", uberich.SignOut("/"))

	http.Handle("/assets/", http.StripPrefix("/assets/", assets.Server(map[string]string{
		"jquery.caret.js":        assets.Caret,
		"jquery.autosize.min.js": assets.AutoSize,
		"styles.css":             assets.Styles,
		"list.js":                assets.List,
	})))

	serve.Serve(*port, *socket, Log(http.DefaultServeMux))
}
示例#6
0
文件: riviera.go 项目: hawx/riviera
func main() {
	flag.Parse()

	if *help || flag.NArg() == 0 {
		printHelp()
		return
	}

	var wg sync.WaitGroup
	waitFor := func(name string, f func() error) {
		log.Println(name, "waiting to close")
		wg.Add(1)
		if err := f(); err != nil {
			log.Println("waitFor:", err)
		}
		wg.Done()
		log.Println(name, "closed")
	}

	opmlPath := flag.Arg(0)

	duration, err := time.ParseDuration(*cutOff)
	if err != nil {
		log.Println(err)
		return
	}

	cacheTimeout, err := time.ParseDuration(*refresh)
	if err != nil {
		log.Println(err)
		return
	}

	store, err := loadDatastore()
	if err != nil {
		log.Println(err)
		return
	}
	defer waitFor("datastore", store.Close)

	outline, err := opml.Load(opmlPath)
	if err != nil {
		log.Println(err)
		return
	}

	feeds := river.New(store, river.Options{
		Mapping:   river.DefaultMapping,
		CutOff:    duration,
		Refresh:   cacheTimeout,
		LogLength: 500,
	})
	defer waitFor("feeds", feeds.Close)

	subs := subscriptions.FromOpml(outline)
	for _, sub := range subs.List() {
		feeds.Add(sub.Uri)
	}

	watcher, err := watchFile(opmlPath, func() {
		log.Printf("reading %s\n", opmlPath)
		outline, err := opml.Load(opmlPath)
		if err != nil {
			log.Printf("could not read %s: %s\n", opmlPath, err)
			return
		}

		added, removed := subscriptions.Diff(subs, subscriptions.FromOpml(outline))
		for _, uri := range added {
			feeds.Add(uri)
			subs.Add(uri)
		}
		for _, uri := range removed {
			feeds.Remove(uri)
			subs.Remove(uri)
		}
	})
	if err != nil {
		log.Printf("could not start watching %s: %v\n", opmlPath, err)
	}
	defer waitFor("watcher", watcher.Close)

	http.Handle("/river/", http.StripPrefix("/river", newRiverHandler(feeds)))

	serve.Serve(*port, *socket, http.DefaultServeMux)
	wg.Wait()
}
示例#7
0
文件: ihkh.go 项目: hawx/ihkh
func main() {
	const usage = `Usage: ihkh [OPTIONS]

  A minimalist Flickr viewer.

 OPTIONS
   --user-id ID      # Your Flickr userid, like XXXXXXXX@XXX
   --api-key KEY     # Your Flickr API key
   --page-size NUM   # Number of photos per page (default: 10)

   --port PORT       # Port to serve on (default: 8080)
   --socket PATH     # Socket to serve at, instead

`

	var (
		port     = flag.String("port", "8080", "")
		socket   = flag.String("socket", "", "")
		userId   = flag.String("user-id", "", "")
		apiKey   = flag.String("api-key", "", "")
		pageSize = flag.Int("page-size", 10, "")
	)

	flag.Usage = func() { fmt.Fprint(os.Stderr, usage) }
	flag.Parse()

	client := flickr.New(*apiKey)

	user, err := client.UserInfo(*userId)
	if err != nil {
		log.Println(err)
		return
	}

	userInfo := views.UserInfo{
		Id:         *userId,
		PhotosUrl:  user.PhotosUrl,
		ProfileUrl: user.ProfileUrl,
		UserName:   user.Username,
		RealName:   user.Realname,
	}

	index := &handler{
		client:   client,
		userInfo: userInfo,
		pageSize: *pageSize,
		showView: views.Photostream,
		get:      getIndex,
	}

	route.HandleFunc("/:page", index.Show())

	sets := &handler{
		client:   client,
		userInfo: userInfo,
		pageSize: *pageSize,
		listView: views.Sets,
		showView: views.Photostream,
		getAll:   getAllSets,
		get:      getSet,
	}

	route.HandleFunc("/sets", sets.List())
	route.HandleFunc("/sets/:param", sets.Show())
	route.HandleFunc("/sets/:param/:page", sets.Show())

	tags := &handler{
		client:   client,
		userInfo: userInfo,
		pageSize: *pageSize,
		listView: views.Tags,
		showView: views.Photostream,
		getAll:   getAllTags,
		get:      getTag,
	}

	route.HandleFunc("/tags", tags.List())
	route.HandleFunc("/tags/:param", tags.Show())
	route.HandleFunc("/tags/:param/:page", tags.Show())

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