Move constants to a config file

This commit is contained in:
Las Zenow 2012-08-22 19:48:02 +02:00
parent 76dead1b45
commit e5d96ef7b7
6 changed files with 38 additions and 48 deletions

View file

@ -9,10 +9,6 @@ import (
"strconv" "strconv"
) )
const (
PATH = "books/"
)
func deleteHandler(coll *mgo.Collection, url string) func(http.ResponseWriter, *http.Request) { func deleteHandler(coll *mgo.Collection, url string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
sess := GetSession(r) sess := GetSession(r)
@ -151,14 +147,14 @@ func storeHandler(newColl, coll *mgo.Collection) func(http.ResponseWriter, *http
} }
book := books[0] book := books[0]
path := PATH + book.Title[:1] + "/" + book.Title + ".epub" path := BOOKS_PATH + book.Title[:1] + "/" + book.Title + ".epub"
_, err = os.Stat(path) _, err = os.Stat(path)
for i := 0; err == nil; i++ { 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) _, 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 := exec.Command("mv", book.Path, path)
cmd.Run() cmd.Run()
book.Path = path book.Path = path

18
config.go Normal file
View file

@ -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 "
)

View file

@ -8,10 +8,6 @@ import (
"strings" "strings"
) )
const (
ITEMS_PAGE = 10
)
func buildQuery(q string) bson.M { func buildQuery(q string) bson.M {
var reg []bson.RegEx var reg []bson.RegEx
query := bson.M{} query := bson.M{}
@ -54,7 +50,7 @@ func searchHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request
page = 0 page = 0
} }
} }
res, num, _ := GetBook(coll, buildQuery(req), ITEMS_PAGE, page) res, num, _ := GetBook(coll, buildQuery(req), SEARCH_ITEMS_PAGE, page)
var data searchData var data searchData
data.S = GetStatus(w, r) data.S = GetStatus(w, r)
@ -62,7 +58,7 @@ func searchHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request
data.Books = res data.Books = res
data.Found = num data.Found = num
data.Page = page + 1 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) data.Next = "/search/?q=" + req + "&p=" + strconv.Itoa(page+1)
} }
if page > 0 { if page > 0 {

View file

@ -5,10 +5,6 @@ import (
"net/http" "net/http"
) )
const (
TEMPLATE_DIR = "templates/"
)
type Status struct { type Status struct {
Search string Search string
User 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{}) { func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
// TODO: when finish devel conver to global: // TODO: when finish devel conver to global:
var templates = template.Must(template.ParseFiles(TEMPLATE_DIR+"header.html", var templates = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html",
TEMPLATE_DIR+"footer.html", TEMPLATE_PATH+"footer.html",
TEMPLATE_DIR+"index.html", TEMPLATE_PATH+"index.html",
TEMPLATE_DIR+"about.html", TEMPLATE_PATH+"about.html",
TEMPLATE_DIR+"book.html", TEMPLATE_PATH+"book.html",
TEMPLATE_DIR+"search.html", TEMPLATE_PATH+"search.html",
TEMPLATE_DIR+"upload.html", TEMPLATE_PATH+"upload.html",
TEMPLATE_DIR+"new.html", TEMPLATE_PATH+"new.html",
TEMPLATE_DIR+"read.html", TEMPLATE_PATH+"read.html",
TEMPLATE_DIR+"edit.html", TEMPLATE_PATH+"edit.html",
)) ))
err := templates.ExecuteTemplate(w, tmpl+".html", data) err := templates.ExecuteTemplate(w, tmpl+".html", data)

View file

@ -7,16 +7,6 @@ import (
"net/http" "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 { type aboutData struct {
S Status S Status
} }
@ -123,7 +113,7 @@ func indexHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request)
} }
func main() { func main() {
session, err := mgo.Dial(IP) session, err := mgo.Dial(DB_IP)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -11,17 +11,11 @@ import (
"strings" "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 { func storePath(name string) string {
path := "new/" + name path := NEW_PATH + name
_, err := os.Stat(path) _, err := os.Stat(path)
for i := 0; err == nil; i++ { for i := 0; err == nil; i++ {
path = "new/" + strconv.Itoa(i) + "_" + name path = NEW_PATH + strconv.Itoa(i) + "_" + name
_, err = os.Stat(path) _, err = os.Stat(path)
} }
return path return path
@ -84,11 +78,11 @@ func storeImg(img []byte, title, extension string) (string, string) {
file.Write(img) file.Write(img)
/* resize 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 := exec.Command(resize[0], resize[1:]...)
cmd.Run() cmd.Run()
imgPathSmall := folder + name + "_small" + extension 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 = exec.Command(resize[0], resize[1:]...)
cmd.Run() cmd.Run()
return "/" + imgPath, "/" + imgPathSmall return "/" + imgPath, "/" + imgPathSmall