append route to web
This commit is contained in:
parent
79604f553f
commit
964e537479
7 changed files with 118 additions and 77 deletions
|
@ -7,7 +7,7 @@ RUN_USER = lunny
|
||||||
RUN_MODE = dev
|
RUN_MODE = dev
|
||||||
|
|
||||||
[repository]
|
[repository]
|
||||||
ROOT = /Users/%(RUN_USER)s/git/gogs-repositories
|
ROOT = /home/work/%(RUN_USER)s/git/gogs-repositories
|
||||||
LANG_IGNS = Google Go|C|C++|Python|Ruby|C Sharp
|
LANG_IGNS = Google Go|C|C++|Python|Ruby|C Sharp
|
||||||
LICENSES = Apache v2 License|GPL v2|MIT License|Affero GPL|Artistic License 2.0|BSD (3-Clause) License
|
LICENSES = Apache v2 License|GPL v2|MIT License|Affero GPL|Artistic License 2.0|BSD (3-Clause) License
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ LICENSES = Apache v2 License|GPL v2|MIT License|Affero GPL|Artistic License 2.0|
|
||||||
DOMAIN = localhost
|
DOMAIN = localhost
|
||||||
ROOT_URL = http://%(DOMAIN)s:%(HTTP_PORT)s/
|
ROOT_URL = http://%(DOMAIN)s:%(HTTP_PORT)s/
|
||||||
HTTP_ADDR =
|
HTTP_ADDR =
|
||||||
HTTP_PORT = 3000
|
HTTP_PORT = 8002
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
; Either "mysql", "postgres" or "sqlite3"(binary release only), it's your choice
|
; Either "mysql", "postgres" or "sqlite3"(binary release only), it's your choice
|
||||||
|
@ -23,7 +23,7 @@ DB_TYPE = mysql
|
||||||
HOST =
|
HOST =
|
||||||
NAME = gogs
|
NAME = gogs
|
||||||
USER = root
|
USER = root
|
||||||
PASSWD =
|
PASSWD = toor
|
||||||
; For "postgres" only, either "disable", "require" or "verify-full"
|
; For "postgres" only, either "disable", "require" or "verify-full"
|
||||||
SSL_MODE = disable
|
SSL_MODE = disable
|
||||||
; For "sqlite3" only
|
; For "sqlite3" only
|
||||||
|
|
|
@ -72,7 +72,7 @@ func (user *User) HomeLink() string {
|
||||||
|
|
||||||
// AvatarLink returns the user gravatar link.
|
// AvatarLink returns the user gravatar link.
|
||||||
func (user *User) AvatarLink() string {
|
func (user *User) AvatarLink() string {
|
||||||
return "http://1.gravatar.com/avatar/" + user.Avatar
|
return "/avatar/" + user.Avatar
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGitSig generates and returns the signature of given user.
|
// NewGitSig generates and returns the signature of given user.
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// for www.gravatar.com image cache
|
||||||
package avatar
|
package avatar
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -23,10 +28,16 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gravatar = "http://www.gravatar.com/avatar"
|
gravatar = "http://www.gravatar.com/avatar"
|
||||||
defaultImagePath = "./default.jpg"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func debug(a ...interface{}) {
|
||||||
|
if true {
|
||||||
|
log.Println(a...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// hash email to md5 string
|
// hash email to md5 string
|
||||||
|
// keep this func in order to make this package indenpent
|
||||||
func HashEmail(email string) string {
|
func HashEmail(email string) string {
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
h.Write([]byte(strings.ToLower(email)))
|
h.Write([]byte(strings.ToLower(email)))
|
||||||
|
@ -35,6 +46,7 @@ func HashEmail(email string) string {
|
||||||
|
|
||||||
type Avatar struct {
|
type Avatar struct {
|
||||||
Hash string
|
Hash string
|
||||||
|
AlterImage string // image path
|
||||||
cacheDir string // image save dir
|
cacheDir string // image save dir
|
||||||
reqParams string
|
reqParams string
|
||||||
imagePath string
|
imagePath string
|
||||||
|
@ -54,7 +66,7 @@ func New(hash string, cacheDir string) *Avatar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Avatar) InCache() bool {
|
func (this *Avatar) HasCache() bool {
|
||||||
fileInfo, err := os.Stat(this.imagePath)
|
fileInfo, err := os.Stat(this.imagePath)
|
||||||
return err == nil && fileInfo.Mode().IsRegular()
|
return err == nil && fileInfo.Mode().IsRegular()
|
||||||
}
|
}
|
||||||
|
@ -68,11 +80,8 @@ func (this *Avatar) Modtime() (modtime time.Time, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Avatar) Expired() bool {
|
func (this *Avatar) Expired() bool {
|
||||||
if !this.InCache() {
|
modtime, err := this.Modtime()
|
||||||
return true
|
return err != nil || time.Since(modtime) > this.expireDuration
|
||||||
}
|
|
||||||
fileInfo, err := os.Stat(this.imagePath)
|
|
||||||
return err != nil || time.Since(fileInfo.ModTime()) > this.expireDuration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// default image format: jpeg
|
// default image format: jpeg
|
||||||
|
@ -92,8 +101,11 @@ func (this *Avatar) Encode(wr io.Writer, size int) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
imgPath := this.imagePath
|
imgPath := this.imagePath
|
||||||
if !this.InCache() {
|
if !this.HasCache() {
|
||||||
imgPath = defaultImagePath
|
if this.AlterImage == "" {
|
||||||
|
return errors.New("request image failed, and no alt image offered")
|
||||||
|
}
|
||||||
|
imgPath = this.AlterImage
|
||||||
}
|
}
|
||||||
img, err = decodeImageFile(imgPath)
|
img, err = decodeImageFile(imgPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -120,13 +132,12 @@ func (this *Avatar) UpdateTimeout(timeout time.Duration) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
type avatarHandler struct {
|
||||||
log.SetFlags(log.Lshortfile | log.LstdFlags)
|
cacheDir string
|
||||||
|
altImage string
|
||||||
}
|
}
|
||||||
|
|
||||||
// http.Handle("/avatar/", avatar.HttpHandler("./cache"))
|
func (this *avatarHandler) mustInt(r *http.Request, defaultValue int, keys ...string) int {
|
||||||
func HttpHandler(cacheDir string) func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
MustInt := func(r *http.Request, defaultValue int, keys ...string) int {
|
|
||||||
var v int
|
var v int
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
if _, err := fmt.Sscanf(r.FormValue(k), "%d", &v); err == nil {
|
if _, err := fmt.Sscanf(r.FormValue(k), "%d", &v); err == nil {
|
||||||
|
@ -136,17 +147,19 @@ func HttpHandler(cacheDir string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return defaultValue
|
return defaultValue
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
func (this *avatarHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
urlPath := r.URL.Path
|
urlPath := r.URL.Path
|
||||||
hash := urlPath[strings.LastIndex(urlPath, "/")+1:]
|
hash := urlPath[strings.LastIndex(urlPath, "/")+1:]
|
||||||
hash = HashEmail(hash)
|
//hash = HashEmail(hash)
|
||||||
size := MustInt(r, 80, "s", "size") // size = 80*80
|
size := this.mustInt(r, 80, "s", "size") // size = 80*80
|
||||||
|
|
||||||
avatar := New(hash, cacheDir)
|
avatar := New(hash, this.cacheDir)
|
||||||
|
avatar.AlterImage = this.altImage
|
||||||
if avatar.Expired() {
|
if avatar.Expired() {
|
||||||
err := avatar.UpdateTimeout(time.Millisecond * 500)
|
err := avatar.UpdateTimeout(time.Millisecond * 500)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
debug(err)
|
||||||
|
//log.Trace("avatar update error: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if modtime, err := avatar.Modtime(); err == nil {
|
if modtime, err := avatar.Modtime(); err == nil {
|
||||||
|
@ -164,17 +177,21 @@ func HttpHandler(cacheDir string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "image/jpeg")
|
w.Header().Set("Content-Type", "image/jpeg")
|
||||||
err := avatar.Encode(w, size)
|
err := avatar.Encode(w, size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
//log.Warn("avatar encode error: %v", err) // will panic when err != nil
|
||||||
|
debug(err)
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// http.Handle("/avatar/", avatar.HttpHandler("./cache"))
|
||||||
|
func HttpHandler(cacheDir string, defaultImgPath string) http.Handler {
|
||||||
|
return &avatarHandler{
|
||||||
|
cacheDir: cacheDir,
|
||||||
|
altImage: defaultImgPath,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
// thunder downloader
|
||||||
http.HandleFunc("/", HttpHandler("./"))
|
|
||||||
log.Fatal(http.ListenAndServe(":8001", nil))
|
|
||||||
}
|
|
||||||
|
|
||||||
var thunder = &Thunder{QueueSize: 10}
|
var thunder = &Thunder{QueueSize: 10}
|
||||||
|
|
||||||
type Thunder struct {
|
type Thunder struct {
|
||||||
|
@ -234,7 +251,7 @@ func (this *thunderTask) Fetch() {
|
||||||
var client = &http.Client{}
|
var client = &http.Client{}
|
||||||
|
|
||||||
func (this *thunderTask) fetch() error {
|
func (this *thunderTask) fetch() error {
|
||||||
log.Println("thunder, fetch", this.Url)
|
//log.Println("thunder, fetch", this.Url)
|
||||||
req, _ := http.NewRequest("GET", this.Url, nil)
|
req, _ := http.NewRequest("GET", this.Url, nil)
|
||||||
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
|
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
|
||||||
req.Header.Set("Accept-Encoding", "gzip,deflate,sdch")
|
req.Header.Set("Accept-Encoding", "gzip,deflate,sdch")
|
||||||
|
|
|
@ -1,29 +1,41 @@
|
||||||
package avatar
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
package avatar_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gogits/gogs/modules/avatar"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const TMPDIR = "test-avatar"
|
||||||
|
|
||||||
func TestFetch(t *testing.T) {
|
func TestFetch(t *testing.T) {
|
||||||
hash := HashEmail("ssx205@gmail.com")
|
os.Mkdir(TMPDIR, 0755)
|
||||||
avatar := New(hash, "./")
|
defer os.RemoveAll(TMPDIR)
|
||||||
//avatar.Update()
|
|
||||||
avatar.UpdateTimeout(time.Millisecond * 200)
|
hash := avatar.HashEmail("ssx205@gmail.com")
|
||||||
time.Sleep(5 * time.Second)
|
a := avatar.New(hash, TMPDIR)
|
||||||
|
a.UpdateTimeout(time.Millisecond * 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFetchMany(t *testing.T) {
|
func TestFetchMany(t *testing.T) {
|
||||||
|
os.Mkdir(TMPDIR, 0755)
|
||||||
|
defer os.RemoveAll(TMPDIR)
|
||||||
|
|
||||||
log.Println("start")
|
log.Println("start")
|
||||||
var n = 50
|
var n = 5
|
||||||
ch := make(chan bool, n)
|
ch := make(chan bool, n)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
go func(i int) {
|
go func(i int) {
|
||||||
hash := HashEmail(strconv.Itoa(i) + "ssx205@gmail.com")
|
hash := avatar.HashEmail(strconv.Itoa(i) + "ssx205@gmail.com")
|
||||||
avatar := New(hash, "./")
|
a := avatar.New(hash, TMPDIR)
|
||||||
avatar.Update()
|
a.Update()
|
||||||
log.Println("finish", hash)
|
log.Println("finish", hash)
|
||||||
ch <- true
|
ch <- true
|
||||||
}(i)
|
}(i)
|
||||||
|
@ -33,3 +45,12 @@ func TestFetchMany(t *testing.T) {
|
||||||
}
|
}
|
||||||
log.Println("end")
|
log.Println("end")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cat
|
||||||
|
// wget http://www.artsjournal.com/artfulmanager/wp/wp-content/uploads/2013/12/200x200xmirror_cat.jpg.pagespeed.ic.GOZSv6v1_H.jpg -O default.jpg
|
||||||
|
/*
|
||||||
|
func TestHttp(t *testing.T) {
|
||||||
|
http.Handle("/", avatar.HttpHandler("./", "default.jpg"))
|
||||||
|
http.ListenAndServe(":8001", nil)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -98,7 +98,7 @@ func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string
|
||||||
|
|
||||||
// AvatarLink returns avatar link by given e-mail.
|
// AvatarLink returns avatar link by given e-mail.
|
||||||
func AvatarLink(email string) string {
|
func AvatarLink(email string) string {
|
||||||
return "http://1.gravatar.com/avatar/" + EncodeMd5(email)
|
return "/avatar/" + EncodeMd5(email)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seconds-based time units
|
// Seconds-based time units
|
||||||
|
|
BIN
public/img/avatar/default.jpg
Normal file
BIN
public/img/avatar/default.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
5
web.go
5
web.go
|
@ -18,6 +18,7 @@ import (
|
||||||
|
|
||||||
"github.com/gogits/gogs/models"
|
"github.com/gogits/gogs/models"
|
||||||
"github.com/gogits/gogs/modules/auth"
|
"github.com/gogits/gogs/modules/auth"
|
||||||
|
"github.com/gogits/gogs/modules/avatar"
|
||||||
"github.com/gogits/gogs/modules/base"
|
"github.com/gogits/gogs/modules/base"
|
||||||
"github.com/gogits/gogs/modules/log"
|
"github.com/gogits/gogs/modules/log"
|
||||||
"github.com/gogits/gogs/modules/mailer"
|
"github.com/gogits/gogs/modules/mailer"
|
||||||
|
@ -114,6 +115,9 @@ func runWeb(*cli.Context) {
|
||||||
|
|
||||||
m.Get("/help", routers.Help)
|
m.Get("/help", routers.Help)
|
||||||
|
|
||||||
|
avatarHandler := avatar.HttpHandler("public/img/avatar", "public/img/avatar/default.jpg")
|
||||||
|
m.Get("/avatar/:hash", avatarHandler.ServeHTTP)
|
||||||
|
|
||||||
adminReq := middleware.AdminRequire()
|
adminReq := middleware.AdminRequire()
|
||||||
m.Get("/admin", reqSignIn, adminReq, admin.Dashboard)
|
m.Get("/admin", reqSignIn, adminReq, admin.Dashboard)
|
||||||
m.Get("/admin/users", reqSignIn, adminReq, admin.Users)
|
m.Get("/admin/users", reqSignIn, adminReq, admin.Users)
|
||||||
|
@ -136,7 +140,6 @@ func runWeb(*cli.Context) {
|
||||||
ignSignIn, middleware.RepoAssignment(true), repo.Single)
|
ignSignIn, middleware.RepoAssignment(true), repo.Single)
|
||||||
m.Get("/:username/:reponame/commit/:commitid/**", ignSignIn, middleware.RepoAssignment(true), repo.Single)
|
m.Get("/:username/:reponame/commit/:commitid/**", ignSignIn, middleware.RepoAssignment(true), repo.Single)
|
||||||
m.Get("/:username/:reponame/commit/:commitid", ignSignIn, middleware.RepoAssignment(true), repo.Single)
|
m.Get("/:username/:reponame/commit/:commitid", ignSignIn, middleware.RepoAssignment(true), repo.Single)
|
||||||
|
|
||||||
m.Get("/:username/:reponame", ignSignIn, middleware.RepoAssignment(true), repo.Single)
|
m.Get("/:username/:reponame", ignSignIn, middleware.RepoAssignment(true), repo.Single)
|
||||||
|
|
||||||
m.Any("/:username/:reponame/**", ignSignIn, repo.Http)
|
m.Any("/:username/:reponame/**", ignSignIn, repo.Http)
|
||||||
|
|
Loading…
Reference in a new issue