AJ ONeal
5 years ago
5 changed files with 154 additions and 8 deletions
Binary file not shown.
@ -1,11 +1,78 @@ |
|||
package jsondb |
|||
|
|||
import ( |
|||
"errors" |
|||
"encoding/json" |
|||
"fmt" |
|||
"net/url" |
|||
"os" |
|||
"strings" |
|||
|
|||
again "git.rootprojects.org/root/go-again" |
|||
) |
|||
|
|||
func List() ([]again.Schedule, error) { |
|||
return nil, errors.New("Not Implemented") |
|||
type JSONDB struct { |
|||
dburl string |
|||
file *os.File |
|||
json *dbjson |
|||
} |
|||
|
|||
type dbjson struct { |
|||
Schedules []again.Schedule `json:"schedules"` |
|||
} |
|||
|
|||
func Connect(dburl string) (*JSONDB, error) { |
|||
u, err := url.Parse(dburl) |
|||
if nil != err { |
|||
return nil, err |
|||
} |
|||
|
|||
// json:/abspath/to/db.json
|
|||
fmt.Println("url.Opaque:", u.Opaque) |
|||
// json:///abspath/to/db.json
|
|||
fmt.Println("url.Path:", u.Path) |
|||
fmt.Println(u) |
|||
|
|||
path := u.Opaque |
|||
if "" == path { |
|||
path = u.Path |
|||
if "" == path { |
|||
// json:relpath/to/db.json
|
|||
// json://relpath/to/db.json
|
|||
path = strings.TrimSuffix(u.Host+"/"+u.Path, "/") |
|||
} |
|||
} |
|||
|
|||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0700) |
|||
if nil != err { |
|||
return nil, fmt.Errorf("Couldn't open %q: %s", path, err) |
|||
} |
|||
|
|||
stat, err := f.Stat() |
|||
if 0 == stat.Size() { |
|||
_, err := f.Write([]byte(`{"schedules":[]}`)) |
|||
if nil != err { |
|||
return nil, err |
|||
} |
|||
f, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0700) |
|||
if nil != err { |
|||
return nil, err |
|||
} |
|||
} |
|||
|
|||
decoder := json.NewDecoder(f) |
|||
db := &dbjson{} |
|||
err = decoder.Decode(db) |
|||
if nil != err { |
|||
return nil, fmt.Errorf("Couldn't parse %q as JSON: %s", path, err) |
|||
} |
|||
|
|||
return &JSONDB{ |
|||
dburl: dburl, |
|||
file: f, |
|||
json: db, |
|||
}, nil |
|||
} |
|||
|
|||
func (db *JSONDB) List() ([]again.Schedule, error) { |
|||
return db.json.Schedules, nil |
|||
} |
|||
|
@ -0,0 +1,9 @@ |
|||
<!DOCTYPE html> |
|||
<html> |
|||
<head> |
|||
<title>Go Again</title> |
|||
</head> |
|||
<body> |
|||
<h1>Hello, World!</h1> |
|||
</body> |
|||
</html> |
Loading…
Reference in new issue