Example #1
0
File: echo.go Project: 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)
}
Example #2
0
func (h booksHandler) Update() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := route.Vars(r)["id"]
		book, ok := h.db.Find(id)

		if !ok {
			w.WriteHeader(404)
			return
		}

		var req models.Book
		json.NewDecoder(r.Body).Decode(&req)

		if req.Title != "" {
			book.Title = req.Title
		}
		if req.Author != "" {
			book.Author = req.Author
		}

		h.db.Save(book)
		h.es.Update(book)

		w.Header().Add("Content-Type", "application/json")
		json.NewEncoder(w).Encode(book)
	})
}
Example #3
0
func (h editionsHandler) Get() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := route.Vars(r)["id"]
		edition, book, ok := h.db.FindEdition(id)

		if !ok {
			w.WriteHeader(404)
			return
		}

		file, err := os.Open(path.Join(h.bookPath, edition.Path()))
		if err != nil {
			log.Println(err)
			w.WriteHeader(500)
			return
		}

		stat, err := file.Stat()
		if err != nil {
			log.Println(err)
			w.WriteHeader(500)
			return
		}

		w.Header().Set("Content-Type", edition.ContentType)
		w.Header().Set("Content-Disposition", `attachment; filename=`+book.Slug(edition))
		w.Header().Set("Content-Length", strconv.Itoa(int(stat.Size())))
		io.Copy(w, file)
	})
}
Example #4
0
File: edit.go Project: hawx/ggg
func (h editHandler) Post() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		repoName := route.Vars(r)["name"]
		repo := h.db.Get(repoName)

		repo.Web = r.FormValue("web")
		repo.Description = r.FormValue("description")
		repo.Branch = r.FormValue("branch")
		repo.IsPrivate = r.FormValue("private") == "private"
		h.db.Save(repo)
		http.Redirect(w, r, "/", 302)
	})
}
Example #5
0
File: edit.go Project: hawx/ggg
func (h editHandler) Get() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		repoName := route.Vars(r)["name"]
		repo := h.db.Get(repoName)

		w.Header().Add("Content-Type", "text/html")
		views.Edit.Execute(w, struct {
			Title string
			*repos.Repo
			LoggedIn bool
		}{h.title, repo, true})
	})
}
Example #6
0
File: delete.go Project: hawx/ggg
func Delete(db repos.Db) http.Handler {
	return mux.Method{
		"GET": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			repoName := route.Vars(r)["name"]

			repo := db.Get(repoName)
			db.Delete(repo)
			os.RemoveAll(repo.Path)

			http.Redirect(w, r, "/", 302)
		}),
	}
}
Example #7
0
func (h booksHandler) Get() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := route.Vars(r)["id"]
		book, ok := h.db.Find(id)

		if !ok {
			w.WriteHeader(404)
			return
		}

		w.Header().Add("Content-Type", "application/json")
		json.NewEncoder(w).Encode(book)
	})
}
Example #8
0
File: ggg.go Project: 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))
}
Example #9
0
File: repo.go Project: hawx/ggg
func (h repoHandler) Git() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		name := route.Vars(r)["name"]
		repoName := name[:len(name)-4]
		repo := h.db.Get(repoName)

		if repo.Name == "" || repo.IsPrivate {
			http.NotFound(w, r)
			return
		}

		http.StripPrefix("/"+name+"/",
			http.FileServer(http.Dir(repo.Path))).ServeHTTP(w, r)
	})
}
Example #10
0
func (h booksHandler) Delete() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := route.Vars(r)["id"]
		book, ok := h.db.Find(id)

		if !ok {
			w.WriteHeader(404)
			return
		}

		h.db.Remove(book)
		h.es.Delete(book)

		w.WriteHeader(204)
	})
}
Example #11
0
File: repo.go Project: hawx/ggg
func (h repoHandler) Html(title, url string, protect Protect) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		name := route.Vars(r)["name"]
		repo := h.db.Get(name)

		if repo.Name == "" {
			http.NotFound(w, r)
			return
		}

		innerHandler := h.htmlPage(title, repo, url)

		if repo.IsPrivate {
			protect(innerHandler(true), http.NotFoundHandler()).ServeHTTP(w, r)
			return
		}

		protect(innerHandler(true), innerHandler(false)).ServeHTTP(w, r)
	})
}
Example #12
0
File: ihkh.go Project: hawx/ihkh
func (h *handler) Show() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		vars := route.Vars(r)
		param := vars["param"]

		page, err := strconv.Atoi(vars["page"])
		if err != nil || page < 1 {
			page = 1
		}

		ctx, err := h.get(h.client, h.userInfo, param, page, h.pageSize)
		if err != nil {
			log.Println(err)
			w.WriteHeader(500)
			return
		}

		h.showView(w, ctx)
	}
}