From e5d96ef7b7f36f338debc091ae211e388695344a Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Wed, 22 Aug 2012 19:48:02 +0200 Subject: [PATCH] Move constants to a config file --- admin.go | 10 +++------- config.go | 18 ++++++++++++++++++ search.go | 8 ++------ template.go | 24 ++++++++++-------------- trantor.go | 12 +----------- upload.go | 14 ++++---------- 6 files changed, 38 insertions(+), 48 deletions(-) create mode 100644 config.go diff --git a/admin.go b/admin.go index 2d96441..90b4a81 100644 --- a/admin.go +++ b/admin.go @@ -9,10 +9,6 @@ import ( "strconv" ) -const ( - PATH = "books/" -) - func deleteHandler(coll *mgo.Collection, url string) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { sess := GetSession(r) @@ -151,14 +147,14 @@ func storeHandler(newColl, coll *mgo.Collection) func(http.ResponseWriter, *http } book := books[0] - path := PATH + book.Title[:1] + "/" + book.Title + ".epub" + path := BOOKS_PATH + book.Title[:1] + "/" + book.Title + ".epub" _, err = os.Stat(path) for i := 0; err == nil; i++ { - path := PATH + book.Title[:1] + "/" + book.Title + "_" + strconv.Itoa(i) + ".epub" + path := BOOKS_PATH + book.Title[:1] + "/" + book.Title + "_" + strconv.Itoa(i) + ".epub" _, err = os.Stat(path) } - os.Mkdir(PATH+book.Title[:1], os.ModePerm) + os.Mkdir(BOOKS_PATH+book.Title[:1], os.ModePerm) cmd := exec.Command("mv", book.Path, path) cmd.Run() book.Path = path diff --git a/config.go b/config.go new file mode 100644 index 0000000..83553fc --- /dev/null +++ b/config.go @@ -0,0 +1,18 @@ +package main + +const ( + DB_IP = "127.0.0.1" + DB_NAME = "trantor" + BOOKS_COLL = "books" + NEW_BOOKS_COLL = "new" + USERS_COLL = "users" + PASS_SALT = "ImperialLibSalt" + TAGS_DISPLAY = 50 + SEARCH_ITEMS_PAGE = 10 + TEMPLATE_PATH = "templates/" + BOOKS_PATH = "books/" + COVER_PATH = "cover/" + NEW_PATH = "new/" + RESIZE_CMD = "/usr/bin/convert -resize 300 -quality 60 " + RESIZE_THUMB_CMD = "/usr/bin/convert -resize 60 -quality 60 " +) diff --git a/search.go b/search.go index f58e230..af5d606 100644 --- a/search.go +++ b/search.go @@ -8,10 +8,6 @@ import ( "strings" ) -const ( - ITEMS_PAGE = 10 -) - func buildQuery(q string) bson.M { var reg []bson.RegEx query := bson.M{} @@ -54,7 +50,7 @@ func searchHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request page = 0 } } - res, num, _ := GetBook(coll, buildQuery(req), ITEMS_PAGE, page) + res, num, _ := GetBook(coll, buildQuery(req), SEARCH_ITEMS_PAGE, page) var data searchData data.S = GetStatus(w, r) @@ -62,7 +58,7 @@ func searchHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request data.Books = res data.Found = num data.Page = page + 1 - if num > (page+1)*ITEMS_PAGE { + if num > (page+1)*SEARCH_ITEMS_PAGE { data.Next = "/search/?q=" + req + "&p=" + strconv.Itoa(page+1) } if page > 0 { diff --git a/template.go b/template.go index e64cd16..b5080cd 100644 --- a/template.go +++ b/template.go @@ -5,10 +5,6 @@ import ( "net/http" ) -const ( - TEMPLATE_DIR = "templates/" -) - type Status struct { Search string User string @@ -29,16 +25,16 @@ func GetStatus(w http.ResponseWriter, r *http.Request) Status { func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) { // TODO: when finish devel conver to global: - var templates = template.Must(template.ParseFiles(TEMPLATE_DIR+"header.html", - TEMPLATE_DIR+"footer.html", - TEMPLATE_DIR+"index.html", - TEMPLATE_DIR+"about.html", - TEMPLATE_DIR+"book.html", - TEMPLATE_DIR+"search.html", - TEMPLATE_DIR+"upload.html", - TEMPLATE_DIR+"new.html", - TEMPLATE_DIR+"read.html", - TEMPLATE_DIR+"edit.html", + var templates = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html", + TEMPLATE_PATH+"footer.html", + TEMPLATE_PATH+"index.html", + TEMPLATE_PATH+"about.html", + TEMPLATE_PATH+"book.html", + TEMPLATE_PATH+"search.html", + TEMPLATE_PATH+"upload.html", + TEMPLATE_PATH+"new.html", + TEMPLATE_PATH+"read.html", + TEMPLATE_PATH+"edit.html", )) err := templates.ExecuteTemplate(w, tmpl+".html", data) diff --git a/trantor.go b/trantor.go index 9344618..5b2434c 100644 --- a/trantor.go +++ b/trantor.go @@ -7,16 +7,6 @@ import ( "net/http" ) -const ( - IP = "127.0.0.1" - DB_NAME = "trantor" - BOOKS_COLL = "books" - NEW_BOOKS_COLL = "new" - USERS_COLL = "users" - PASS_SALT = "ImperialLibSalt" - TAGS_DISPLAY = 50 -) - type aboutData struct { S Status } @@ -123,7 +113,7 @@ func indexHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) } func main() { - session, err := mgo.Dial(IP) + session, err := mgo.Dial(DB_IP) if err != nil { panic(err) } diff --git a/upload.go b/upload.go index 5adb871..f4fe526 100644 --- a/upload.go +++ b/upload.go @@ -11,17 +11,11 @@ import ( "strings" ) -const ( - COVER_PATH = "cover/" - RESIZE = "/usr/bin/convert -resize 300 -quality 60 " - RESIZE_THUMB = "/usr/bin/convert -resize 60 -quality 60 " -) - func storePath(name string) string { - path := "new/" + name + path := NEW_PATH + name _, err := os.Stat(path) for i := 0; err == nil; i++ { - path = "new/" + strconv.Itoa(i) + "_" + name + path = NEW_PATH + strconv.Itoa(i) + "_" + name _, err = os.Stat(path) } return path @@ -84,11 +78,11 @@ func storeImg(img []byte, title, extension string) (string, string) { file.Write(img) /* resize img */ - resize := append(strings.Split(RESIZE, " "), imgPath, imgPath) + resize := append(strings.Split(RESIZE_CMD, " "), imgPath, imgPath) cmd := exec.Command(resize[0], resize[1:]...) cmd.Run() imgPathSmall := folder + name + "_small" + extension - resize = append(strings.Split(RESIZE_THUMB, " "), imgPath, imgPathSmall) + resize = append(strings.Split(RESIZE_THUMB_CMD, " "), imgPath, imgPathSmall) cmd = exec.Command(resize[0], resize[1:]...) cmd.Run() return "/" + imgPath, "/" + imgPathSmall