Permalink
Browse files

move conf functions into conf pkg

  • Loading branch information...
1 parent 3e22910 commit cbba7e44b321a61796c96c372a0322bb555f8a9b ♠ ace hacker committed May 8, 2013
Showing with 46 additions and 69 deletions.
  1. +41 −5 conf/conf.go
  2. +2 −1 outlet/http_outlet.go
  3. +3 −63 utils/utils.go
View
46 conf/conf.go
@@ -11,13 +11,24 @@
package conf
import (
+ "errors"
+ "strconv"
+ "net/url"
"strings"
"flag"
- "l2met/utils"
"os"
"time"
)
+var AppName string
+
+func init() {
+ AppName = os.Getenv("APP_NAME")
+ if len(AppName) == 0 {
+ AppName = "l2met"
+ }
+}
+
var Outlet string
func init() {
@@ -31,10 +42,10 @@ var (
)
func init() {
- b := utils.EnvInt("REQUEST_BUFFER", 1000)
+ b := envInt("REQUEST_BUFFER", 1000)
flag.IntVar(&BufferSize, "buffer", b, "Number of items to buffer prior to flush.")
- c := utils.EnvInt("CONCURRENCY", 100)
+ c := envInt("CONCURRENCY", 100)
flag.IntVar(&Concurrency, "concurrency", c, "Number local routines to start.")
t := time.Millisecond * 200
@@ -58,7 +69,7 @@ func init() {
return
}
- RedisHost, RedisPass, err = utils.ParseRedisUrl(rurl)
+ RedisHost, RedisPass, err = parseRedisUrl(rurl)
if err != nil {
return
}
@@ -96,7 +107,7 @@ var (
func init() {
flag.BoolVar(&Verbose, "v", false, "Enable verbose stastics.")
- p := utils.EnvInt("PORT", 8080)
+ p := envInt("PORT", 8080)
flag.IntVar(&Port, "port", p, "HTTP server will bind to this port.")
}
@@ -110,3 +121,28 @@ func init() {
func init() {
flag.Parse()
}
+
+//Helper Functions
+
+func parseRedisUrl(s string) (string, string, error) {
+ u, err := url.Parse(s)
+ if err != nil {
+ return "", "", errors.New("Missing REDIS_URL")
+ }
+ var password string
+ if u.User != nil {
+ password, _ = u.User.Password()
+ }
+ return u.Host, password, nil
+}
+
+func envInt(name string, defaultVal int) int {
+ tmp := os.Getenv(name)
+ if len(tmp) != 0 {
+ n, err := strconv.Atoi(tmp)
+ if err == nil {
+ return n
+ }
+ }
+ return defaultVal
+}
View
3 outlet/http_outlet.go
@@ -5,6 +5,7 @@ import (
"l2met/bucket"
"l2met/store"
"l2met/utils"
+ "l2met/auth"
"math"
"net/http"
"net/url"
@@ -20,7 +21,7 @@ type HttpOutlet struct {
func (h *HttpOutlet) ServeReadBucket(w http.ResponseWriter, r *http.Request) {
// need to extract: token, source, name, time
// https://l2met:token@l2met.net/buckets/:name
- user, pass, err := utils.ParseAuth(r)
+ user, pass, err := auth.Parse(r)
if err != nil {
http.Error(w, "Inavalid Authentication", 401)
return
View
66 utils/utils.go
@@ -1,68 +1,20 @@
package utils
import (
+ "l2met/conf"
"encoding/json"
- "errors"
"fmt"
"net/http"
- "net/url"
- "os"
- "strconv"
"time"
)
-var (
- appName string
-)
-
-func init() {
- appName = os.Getenv("APP_NAME")
- if len(appName) == 0 {
- appName = "l2met"
- }
-}
-
-func EnvUint64(name string, defaultVal uint64) uint64 {
- tmp := os.Getenv(name)
- if len(tmp) != 0 {
- n, err := strconv.ParseUint(tmp, 10, 64)
- if err == nil {
- return n
- }
- }
- return defaultVal
-}
-
-func EnvString(name string, defaultVal string) string {
- tmp := os.Getenv(name)
- if len(tmp) != 0 {
- return tmp
- }
- return defaultVal
-}
-
-func EnvInt(name string, defaultVal int) int {
- tmp := os.Getenv(name)
- if len(tmp) != 0 {
- n, err := strconv.Atoi(tmp)
- if err == nil {
- return n
- }
- }
- return defaultVal
-}
-
-func EnvDuration(name string, defaultVal int) time.Duration {
- return time.Duration(EnvInt(name, defaultVal))
-}
-
func MeasureI(name, units string, val int64) {
- m := appName + "." + name
+ m := conf.AppName + "." + name
fmt.Printf("measure.%s=%d\n", m, val)
}
func MeasureT(name string, t time.Time) {
- m := appName + "." + name
+ m := conf.AppName + "." + name
fmt.Printf("measure.%s=%dms\n", m, time.Since(t)/time.Millisecond)
}
@@ -86,15 +38,3 @@ func WriteJson(w http.ResponseWriter, status int, data interface{}) {
func RoundTime(t time.Time, d time.Duration) time.Time {
return time.Unix(0, int64((time.Duration(t.UnixNano())/d)*d))
}
-
-func ParseRedisUrl(s string) (string, string, error) {
- u, err := url.Parse(s)
- if err != nil {
- return "", "", errors.New("utils: Missing REDIS_URL")
- }
- var password string
- if u.User != nil {
- password, _ = u.User.Password()
- }
- return u.Host, password, nil
-}

0 comments on commit cbba7e4

Please sign in to comment.