Esempio n. 1
0
File: echo.go Progetto: 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)
}
Esempio n. 2
0
File: ggg.go Progetto: 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))
}
Esempio n. 3
0
File: ihkh.go Progetto: 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)
}