Added last of web REST endpoints, need to test and start on UI

This commit is contained in:
Tim Shannon 2016-04-13 16:29:17 +00:00
parent e013f2bff4
commit d3850c24f8
6 changed files with 165 additions and 38 deletions

View File

@ -69,9 +69,9 @@ func (ds *Store) Close() error {
return ds.bolt.Close() return ds.bolt.Close()
} }
func (ds *Store) get(bucket string, key TimeKey, result interface{}) error { func (ds *Store) get(bucket string, key []byte, result interface{}) error {
return ds.bolt.View(func(tx *bolt.Tx) error { return ds.bolt.View(func(tx *bolt.Tx) error {
dsValue := tx.Bucket([]byte(bucket)).Get(key.Bytes()) dsValue := tx.Bucket([]byte(bucket)).Get(key)
if dsValue == nil { if dsValue == nil {
return ErrNotFound return ErrNotFound
@ -90,7 +90,7 @@ func (ds *Store) get(bucket string, key TimeKey, result interface{}) error {
}) })
} }
func (ds *Store) put(bucket string, key TimeKey, value interface{}) error { func (ds *Store) put(bucket string, key []byte, value interface{}) error {
var err error var err error
dsValue, ok := value.([]byte) dsValue, ok := value.([]byte)
if !ok { if !ok {
@ -101,12 +101,12 @@ func (ds *Store) put(bucket string, key TimeKey, value interface{}) error {
} }
return ds.bolt.Update(func(tx *bolt.Tx) error { return ds.bolt.Update(func(tx *bolt.Tx) error {
return tx.Bucket([]byte(bucket)).Put(key.Bytes(), dsValue) return tx.Bucket([]byte(bucket)).Put(key, dsValue)
}) })
} }
func (ds *Store) delete(bucket string, key TimeKey) error { func (ds *Store) delete(bucket string, key []byte) error {
return ds.bolt.Update(func(tx *bolt.Tx) error { return ds.bolt.Update(func(tx *bolt.Tx) error {
return tx.Bucket([]byte(bucket)).Delete(key.Bytes()) return tx.Bucket([]byte(bucket)).Delete(key)
}) })
} }

View File

@ -32,7 +32,7 @@ func (ds *Store) AddLog(version, stage, entry string) error {
Log: entry, Log: entry,
} }
return ds.put(bucketLog, key, data) return ds.put(bucketLog, key.Bytes(), data)
} }
// LastVersion returns the last version in the log for the given stage. If stage is blank, // LastVersion returns the last version in the log for the given stage. If stage is blank,

View File

@ -11,7 +11,8 @@ import (
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
) )
type release struct { // Release is a record of the fully built and ready to deploy release file
type Release struct {
When time.Time `json:"when"` When time.Time `json:"when"`
Version string `json:"version"` Version string `json:"version"`
FileName string `json:"fileName"` FileName string `json:"fileName"`
@ -27,7 +28,7 @@ const (
func (ds *Store) AddRelease(version, fileName string, fileData []byte) error { func (ds *Store) AddRelease(version, fileName string, fileData []byte) error {
fileKey := NewTimeKey() fileKey := NewTimeKey()
r := &release{ r := &Release{
When: fileKey.Time(), When: fileKey.Time(),
Version: version, Version: version,
FileName: fileName, FileName: fileName,
@ -49,33 +50,42 @@ func (ds *Store) AddRelease(version, fileName string, fileData []byte) error {
}) })
} }
func (ds *Store) Release(version string) { // ReleaseFile returns a specific file from a release for the given file key
func (ds *Store) ReleaseFile(fileKey TimeKey) ([]byte, error) {
var fileData []byte
err := ds.get(bucketFiles, fileKey.Bytes(), &fileData)
if err != nil {
return nil, err
}
return fileData, nil
}
// Release gets the release record for a specific version
func (ds *Store) Release(version string) (*Release, error) {
r := &Release{}
err := ds.get(bucketReleases, []byte(version), r)
if err != nil {
return nil, err
}
return r, nil
} }
// Releases lists all the releases in a given project // Releases lists all the releases in a given project
func (ds *Store) Releases() ([]*Log, error) { func (ds *Store) Releases() ([]*Release, error) {
var vers []*Log var vers []*Release
err := ds.bolt.View(func(tx *bolt.Tx) error { err := ds.bolt.View(func(tx *bolt.Tx) error {
c := tx.Bucket([]byte(bucketLog)).Cursor() c := tx.Bucket([]byte(bucketReleases)).Cursor()
var current = ""
for k, v := c.Last(); k != nil; k, v = c.Prev() { for k, v := c.Last(); k != nil; k, v = c.Prev() {
l := &Log{} r := &Release{}
err := json.Unmarshal(v, l) err := json.Unmarshal(v, r)
if err != nil { if err != nil {
return err return err
} }
// capture the newest entry for each version vers = append(vers, r)
if l.Version != current {
l.Log = "" // only care about date, ver and stage
vers = append(vers, l)
current = l.Version
}
} }
return nil return nil
@ -87,3 +97,31 @@ func (ds *Store) Releases() ([]*Log, error) {
return vers, nil return vers, nil
} }
// LastRelease lists the last release for a project
func (ds *Store) LastRelease() (*Release, error) {
var r *Release
err := ds.bolt.View(func(tx *bolt.Tx) error {
c := tx.Bucket([]byte(bucketReleases)).Cursor()
_, v := c.Last()
err := json.Unmarshal(v, r)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
if r == nil {
return nil, ErrNotFound
}
return r, nil
}

View File

@ -10,6 +10,8 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"git.townsourced.com/ironsmith/datastore"
) )
const ( const (
@ -19,11 +21,16 @@ const (
// Err404 is a standard 404 error response // Err404 is a standard 404 error response
var Err404 = errors.New("Resource not found") var Err404 = errors.New("Resource not found")
func errHandled(err error, w http.ResponseWriter) bool { func errHandled(err error, w http.ResponseWriter, r *http.Request) bool {
if err == nil { if err == nil {
return false return false
} }
if err == datastore.ErrNotFound {
four04(w, r)
return true
}
var status, errMsg string var status, errMsg string
errMsg = err.Error() errMsg = err.Error()

View File

@ -228,6 +228,35 @@ func (p *Project) stageLog(version, stage string) (*datastore.Log, error) {
return p.ds.StageLog(version, stage) return p.ds.StageLog(version, stage)
} }
func (p *Project) releases() ([]*datastore.Release, error) {
p.RLock()
defer p.RUnlock()
return p.ds.Releases()
}
func (p *Project) lastRelease() (*datastore.Release, error) {
p.RLock()
defer p.RUnlock()
return p.ds.LastRelease()
}
func (p *Project) releaseData(version string) (*datastore.Release, error) {
p.RLock()
defer p.RUnlock()
return p.ds.Release(version)
}
func (p *Project) releaseFile(fileKey datastore.TimeKey) ([]byte, error) {
p.RLock()
defer p.RUnlock()
return p.ds.ReleaseFile(fileKey)
}
// releaseFile
func (p *Project) setData(new *Project) { func (p *Project) setData(new *Project) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()

View File

@ -5,9 +5,11 @@
package main package main
import ( import (
"bytes"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"time"
) )
// /path/<project-id>/<version>/<stage> // /path/<project-id>/<version>/<stage>
@ -34,7 +36,7 @@ func splitPath(path string) (project, version, stage string) {
/* /*
/log/ - list all projects /log/ - list all projects
/log/<project-id> - list all versions in a project, triggers new builds /log/<project-id> - list all versions in a project, POST triggers new builds
/log/<project-id>/<version> - list combined output of all stages for a given version /log/<project-id>/<version> - list combined output of all stages for a given version
/log/<project-id>/<version>/<stage> - list output of a given stage of a given version /log/<project-id>/<version>/<stage> - list output of a given stage of a given version
*/ */
@ -44,7 +46,7 @@ func logGet(w http.ResponseWriter, r *http.Request) {
if prj == "" { if prj == "" {
///log/ - list all projects ///log/ - list all projects
pList, err := projects.webList() pList, err := projects.webList()
if errHandled(err, w) { if errHandled(err, w, r) {
return return
} }
@ -65,10 +67,10 @@ func logGet(w http.ResponseWriter, r *http.Request) {
//project found //project found
if ver == "" { if ver == "" {
///log/<project-id> - list all versions in a project, triggers new builds ///log/<project-id> - list all versions in a project
vers, err := project.versions() vers, err := project.versions()
if errHandled(err, w) { if errHandled(err, w, r) {
return return
} }
respondJsend(w, &JSend{ respondJsend(w, &JSend{
@ -82,7 +84,7 @@ func logGet(w http.ResponseWriter, r *http.Request) {
if stg == "" { if stg == "" {
///log/<project-id>/<version> - list combined output of all stages for a given version ///log/<project-id>/<version> - list combined output of all stages for a given version
logs, err := project.versionLog(ver) logs, err := project.versionLog(ver)
if errHandled(err, w) { if errHandled(err, w, r) {
return return
} }
respondJsend(w, &JSend{ respondJsend(w, &JSend{
@ -96,7 +98,7 @@ func logGet(w http.ResponseWriter, r *http.Request) {
///log/<project-id>/<version>/<stage> - list output of a given stage of a given version ///log/<project-id>/<version>/<stage> - list output of a given stage of a given version
log, err := project.stageLog(ver, stg) log, err := project.stageLog(ver, stg)
if errHandled(err, w) { if errHandled(err, w, r) {
return return
} }
@ -110,14 +112,18 @@ func logGet(w http.ResponseWriter, r *http.Request) {
/* /*
/release/<project-id>/<version> /release/<project-id>/<version>
/release/<project-id> - list last release for a given project ?all returns all the releases for a project /release/<project-id> - list last release for a given project
/release/<project-id>/<version> - list release for a given project version ?all returns all the releases for a project ?file returns the last release file
/release/<project-id>/<version> - list release for a given project version ?file returns the file for a given release version
*/ */
func releaseGet(w http.ResponseWriter, r *http.Request) { func releaseGet(w http.ResponseWriter, r *http.Request) {
prj, ver, stg := splitPath(r.URL.Path) prj, ver, _ := splitPath(r.URL.Path)
values := r.URL.Query() values := r.URL.Query()
_, all := values["all"]
_, file := values["file"]
if prj == "" { if prj == "" {
four04(w, r) four04(w, r)
return return
@ -132,18 +138,65 @@ func releaseGet(w http.ResponseWriter, r *http.Request) {
//project found //project found
if ver == "" { if ver == "" {
///release/<project-id> - list last release for a given project ?all returns all the releases for a project ///release/<project-id> - list last release for a given project
// ?all returns all the releases for a project ?file returns the last release file
vers, err := project.versions() if all {
if errHandled(err, w) { releases, err := project.releases()
if errHandled(err, w, r) {
return return
} }
respondJsend(w, &JSend{ respondJsend(w, &JSend{
Status: statusSuccess, Status: statusSuccess,
Data: vers, Data: releases,
}) })
return return
}
last, err := project.lastRelease()
if errHandled(err, w, r) {
return
}
if file {
fileData, err := project.releaseFile(last.FileKey)
if errHandled(err, w, r) {
return
}
http.ServeContent(w, r, last.FileName, time.Time{}, bytes.NewReader(fileData))
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: last,
})
return
} }
//ver found //ver found
// /release/<project-id>/<version> - list release for a given project version ?file returns the file for a given release version
release, err := project.releaseData(ver)
if errHandled(err, w, r) {
return
}
if file {
fileData, err := project.releaseFile(release.FileKey)
if errHandled(err, w, r) {
return
}
http.ServeContent(w, r, release.FileName, time.Time{}, bytes.NewReader(fileData))
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: release,
})
} }