ironsmith/webProject.go

246 lines
4.6 KiB
Go
Raw Normal View History

2016-04-06 11:31:22 -05:00
// Copyright 2016 Tim Shannon. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package main
import (
"bytes"
2016-04-06 11:31:22 -05:00
"net/http"
"net/url"
"strings"
"time"
2016-04-06 11:31:22 -05:00
)
// /path/<project-id>/<version>/<stage>
2016-04-06 11:31:22 -05:00
func splitPath(path string) (project, version, stage string) {
s := strings.Split(path, "/")
if len(s) < 3 {
return
}
project, _ = url.QueryUnescape(s[2])
if len(s) < 4 {
return
}
version, _ = url.QueryUnescape(s[3])
if len(s) < 5 {
return
}
stage, _ = url.QueryUnescape(s[4])
return
}
2016-04-07 09:49:54 -05:00
/*
/log/ - list all projects
/log/<project-id> - list all versions in a project, POST triggers new builds
2016-04-07 09:49:54 -05:00
/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
*/
func logGet(w http.ResponseWriter, r *http.Request) {
prj, ver, stg := splitPath(r.URL.Path)
if prj == "" {
2016-04-07 09:49:54 -05:00
///log/ - list all projects
pList, err := projects.webList()
if errHandled(err, w, r) {
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: pList,
})
return
}
project, ok := projects.get(prj)
if !ok {
four04(w, r)
return
}
//project found
if ver == "" {
///log/<project-id> - list all versions in a project
2016-04-07 09:49:54 -05:00
vers, err := project.versions()
if errHandled(err, w, r) {
2016-04-07 09:49:54 -05:00
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: vers,
})
return
}
//ver found
if stg == "" {
///log/<project-id>/<version> - list combined output of all stages for a given version
logs, err := project.versionLog(ver)
if errHandled(err, w, r) {
2016-04-07 09:49:54 -05:00
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: logs,
})
return
}
//stage found
///log/<project-id>/<version>/<stage> - list output of a given stage of a given version
log, err := project.stageLog(ver, stg)
if errHandled(err, w, r) {
2016-04-07 09:49:54 -05:00
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: log,
})
return
}
/*
/release/<project-id>/<version>
/release/<project-id> - list last release for a given project
?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
2016-04-07 09:49:54 -05:00
*/
func releaseGet(w http.ResponseWriter, r *http.Request) {
prj, ver, _ := splitPath(r.URL.Path)
2016-04-07 09:49:54 -05:00
values := r.URL.Query()
_, all := values["all"]
_, file := values["file"]
2016-04-07 09:49:54 -05:00
if prj == "" {
four04(w, r)
return
}
project, ok := projects.get(prj)
if !ok {
four04(w, r)
return
}
//project found
if ver == "" {
///release/<project-id> - list last release for a given project
// ?all returns all the releases for a project ?file returns the last release file
if all {
releases, err := project.releases()
if errHandled(err, w, r) {
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: releases,
})
return
2016-04-07 09:49:54 -05:00
}
last, err := project.lastRelease()
if errHandled(err, w, r) {
return
}
if file {
fileData, err := project.releaseFile(last.FileKey)
if errHandled(err, w, r) {
return
}
w.Header().Add("Content-disposition", `attachment; filename="`+last.FileName+`"`)
http.ServeContent(w, r, last.FileName, time.Time{}, bytes.NewReader(fileData))
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: last,
})
return
}
2016-04-06 11:31:22 -05:00
2016-04-07 09:49:54 -05:00
//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
}
w.Header().Add("Content-disposition", `attachment; filename="`+release.FileName+`"`)
http.ServeContent(w, r, release.FileName, time.Time{}, bytes.NewReader(fileData))
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: release,
})
2016-04-06 11:31:22 -05:00
}
type triggerInput struct {
Secret string `json:"secret"`
}
/*trigger routes
/trigger/<project-id>
Triggers a project to start a cycle
*/
func triggerPost(w http.ResponseWriter, r *http.Request) {
prj, _, _ := splitPath(r.URL.Path)
if prj == "" {
four04(w, r)
return
}
project, ok := projects.get(prj)
if !ok {
four04(w, r)
return
}
input := &triggerInput{}
if errHandled(parseInput(r, input), w, r) {
return
}
if input.Secret != project.TriggerSecret {
errHandled(&Fail{
Message: "Invalid trigger secret for this project",
HTTPStatus: http.StatusUnauthorized,
}, w, r)
return
}
go func() {
project.load()
}()
}