Skip to content

Commit

Permalink
Import code from goals-calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
nono committed Jan 8, 2010
1 parent ab6d55a commit ee6e2c4
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.8
*.6
*.out
*.a
10 changes: 10 additions & 0 deletions Makefile
@@ -0,0 +1,10 @@
include $(GOROOT)/src/Make.$(GOARCH)

TARG=goldorak
GOFILES=\
action.go\
config.go\
goldorak.go\
model.go\

include $(GOROOT)/src/Make.pkg
29 changes: 29 additions & 0 deletions action.go
@@ -0,0 +1,29 @@
package goldorak

import (
"json"
"io/ioutil"
"log"
)

var config map[string]string

func ReadConfig(filename string) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
log.Exitf("Impossible to read %s", filename)
}
data, err := json.Decode(string(contents))
if err != nil {
log.Exitf("Can't parse %s as JSON", filename)
}
config = map[string]string{ }
for key, value := range data.(map[string]interface{ }) {
config[key] = value.(string)
}
}

func GetConfig(key string) string {
return config[key];
}

16 changes: 16 additions & 0 deletions goldorak.go
@@ -0,0 +1,16 @@
package goldorak

import (
"web"
)

func Initialize() {
ReadConfig("config.json")
}

func Start() {
addr := GetConfig("interface") + ":" + GetConfig("port")
web.SetStaticDir(GetConfig("docroot"))
web.Run(addr)
}

45 changes: 45 additions & 0 deletions model.go
@@ -0,0 +1,45 @@
package goldorak

import (
"redis"
"strconv"
"strings"
)

type Model struct {
db int
client redis.Client
name string
}

const keySeparator = ":"

func NewModel(name string) *Model {
fn := GetConfig("appname") + keySeparator + name
db := GetConfig("db")
m := new(Model)
m.db, _ = strconv.Atoi(db)
s := redis.DefaultSpec().Db(m.db)
// TODO if e != nil ...
m.name = fn
m.client, _ = redis.NewSynchClientWithSpec(s)
// TODO
// if e != nil { log.Stderr ("failed to create the client", e); return "failed" }
return m
}

func (m *Model) FullKey(key string) string {
return m.name + keySeparator + key
}

func (m *Model) Get(key string) string {
value, _ := m.client.Get(m.FullKey(key))
// TODO
// if e!= nil { log.Stderr ("error on Get", e); return "failed 2" }
return string(value);
}

func (m *Model) Set(key string, value string) {
m.client.Set(m.FullKey(key), strings.Bytes(value))
}

13 changes: 13 additions & 0 deletions routes.go
@@ -0,0 +1,13 @@
package goldorak

import (
"web"
)

// TODO what about POST/PUT/DELETE?
func Get(route string, handler interface{}) {
web.Get(route, handler)
}

// TODO func Restful()

0 comments on commit ee6e2c4

Please sign in to comment.