~humaid/workflow-engine unlisted

b326f668fb73a3644645321686e7eb49cc5bea27 — Humaid AlQassimi 7 months ago 6cca767
Allow editting channel, step.

Also added the Post To in the new step form.
5 files changed, 156 insertions(+), 1 deletions(-)

M admin.go
M admin_forms.go
A templates/edit-channel.tmpl
A templates/edit-step.tmpl
M templates/new-step.tmpl
M admin.go => admin.go +84 -1
@@ 3,6 3,7 @@ package engine
import (
	"encoding/json"
	"fmt"
	"html/template"
	"io/ioutil"
	"log"
	"net/http"


@@ 37,7 38,20 @@ func contextInit(e *Engine) macaron.Handler {

func (e *Engine) RunWebInterface() {
	m := macaron.Classic()
	renderOpts := macaron.RenderOptions{}
	renderOpts := macaron.RenderOptions{
		Funcs: []template.FuncMap{map[string]interface{}{
			"csv": func(s []string) string {
				var str strings.Builder
				for _, v := range s {
					if str.Len() > 0 {
						str.WriteRune(',')
					}
					str.WriteString(v)
				}
				return str.String()
			},
		}},
	}
	staticOpts := macaron.StaticOptions{
		Expires: func() string {
			return time.Now().Add(24 * 60 * time.Minute).UTC().Format("Mon, 02 Jan 2006 15:04:05 GMT")


@@ 81,12 95,78 @@ func (e *Engine) RunWebInterface() {
		m.Group("/edit", func() {
			m.Get("/service/:service", editServiceHandler)
			m.Post("/service/:service", binding.BindIgnErr(newServiceForm{}), postEditServiceHandler)
			m.Get("/channel/:service/:channel", editChannelHandler)
			m.Post("/channel/:service/:channel", binding.BindIgnErr(newChannelForm{}), postEditChannelHandler)
			m.Get("/step/:service/:channel/:step", editStepHandler)
			m.Post("/step/:service/:channel/:step", binding.BindIgnErr(newStepForm{}), postEditStepHandler)
		})
		m.Get("/commit", commitHandler)
	}, requireLogin)
	log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", e.Options.AdminPort), m))
}

func editStepHandler(ctx *macaron.Context, f *session.Flash) {
	e := ctx.Data["Engine"].(*Engine)
	s := e.getStep(ctx.Params("service"), ctx.Params("channel"), ctx.Params("step"))
	if s == nil {
		f.Error("Step not found")
		ctx.Redirect("/dash")
		return
	}
	ctx.Data["Step"] = s
	ctx.HTML(http.StatusOK, "edit-step")
}

func postEditStepHandler(ctx *macaron.Context, f *session.Flash, errs binding.Errors, form newStepForm) {
	if errs.Len() > 0 {
		f.Error("Make sure to fill required fields")
		ctx.Redirect("/edit/service/" + ctx.Params("service") + "/" + ctx.Params("channel") + "/" + ctx.Params("step"))
		return
	}
	e := ctx.Data["Engine"].(*Engine)
	s := e.getStep(ctx.Params("service"), ctx.Params("channel"), ctx.Params("step"))
	if s == nil {
		f.Error("Step not found")
		ctx.Redirect("/dash")
		return
	}
	s.Status = form.Status
	s.Page = form.Page
	s.PostTo = strings.Split(form.PostTo, ",")
	s.DataIn = strings.Split(form.DataIn, ",")
	s.DataOut = strings.Split(form.DataOut, ",")
	ctx.Redirect("/s/" + ctx.Params("service") + "/" + ctx.Params("channel") + "/" + ctx.Params("step"))
}
func editChannelHandler(ctx *macaron.Context, f *session.Flash) {
	e := ctx.Data["Engine"].(*Engine)
	c := e.getChannel(ctx.Params("service"), ctx.Params("channel"))
	if c == nil {
		f.Error("Channel not found")
		ctx.Redirect("/dash")
		return
	}
	ctx.Data["Channel"] = c
	ctx.HTML(http.StatusOK, "edit-channel")
}

func postEditChannelHandler(ctx *macaron.Context, f *session.Flash, errs binding.Errors, form newChannelForm) {
	if errs.Len() > 0 {
		f.Error("Make sure to fill required fields")
		ctx.Redirect("/edit/service/" + ctx.Params("service") + "/" + ctx.Params("channel"))
		return
	}
	e := ctx.Data["Engine"].(*Engine)
	c := e.getChannel(ctx.Params("service"), ctx.Params("channel"))
	if c == nil {
		f.Error("Channel not found")
		ctx.Redirect("/dash")
		return
	}
	c.ChannelDescription = form.Description
	c.Initiators = strings.Split(form.Initiators, ",")
	ctx.Redirect("/s/" + ctx.Params("service") + "/" + ctx.Params("channel"))
}

func editServiceHandler(ctx *macaron.Context, f *session.Flash) {
	e := ctx.Data["Engine"].(*Engine)
	s := e.getService(ctx.Params("service"))


@@ 209,6 289,9 @@ func postNewStepHandler(ctx *macaron.Context, errs binding.Errors, form newStepF
	if len(form.DataOut) > 0 {
		st.DataOut = strings.Split(form.DataOut, ",")
	}
	if len(form.PostTo) > 0 {
		st.PostTo = strings.Split(form.PostTo, ",")
	}
	ch.Steps = append(ch.Steps, st)
	e.SchemasModified = true


M admin_forms.go => admin_forms.go +1 -0
@@ 16,6 16,7 @@ type newStepForm struct {
	StepID  string `form:"id" binding:"Required"`
	Status  string `form:"desc"`
	Page    string `form:"page"`
	PostTo  string `form:"post"`
	DataIn  string `form:"data-in"`
	DataOut string `form:"data-out"`
}

A templates/edit-channel.tmpl => templates/edit-channel.tmpl +27 -0
@@ 0,0 1,27 @@
{{template "base/head" .}}
<div class="content">
	{{template "partials/flash" .}}
	<h2>Edit channel</h2>
	<form method="post">
		<div class="col-5">
			<div class="form-group">
				<p><label for="id">Channel ID<sup>*</sup></label><p>
				<input type="text" id="id" name="id"
				value="{{.Channel.ChannelID}}" readonly="1">
			</div>
			<div class="form-group">
				<p><label for="desc">Description</label><p>
				<input type="text" id="desc" name="desc"
				value="{{.Channel.ChannelDescription}}">
			</div>
			<div class="form-group">
				<p><label for="inits">Initiators (comma-separated)</label><p>
				<input type="text" id="inits" name="inits" value="{{csv .Channel.Initiators}}">
			</div>
			<input type="hidden" name="_csrf" value="{{.csrf_token}}">
			<button type="submit" class="btn">Edit channel</button>
		</div>
	</form>

</div>
{{template "base/footer" .}}

A templates/edit-step.tmpl => templates/edit-step.tmpl +40 -0
@@ 0,0 1,40 @@
{{template "base/head" .}}
<div class="content">
	{{template "partials/flash" .}}
	<h2>Edit step: {{.Step.StepID}}</h2>
	<form method="post">
		<div class="col-5">
			<div class="form-group">
				<p><label for="id">Step ID<sup>*</sup></label><p>
				<input type="text" id="id" name="id" value="{{.Step.StepID}}">
			</div>
			<div class="form-group">
				<p><label for="desc">Status (description)</label><p>
				<input type="text" id="desc" name="desc"
				value="{{.Step.Status}}">
			</div>
			<div class="form-group">
				<p><label for="post">Post To (comma-separated)</label><p>
				<input type="text" id="post" name="post" value="{{csv .Step.PostTo}}">
			</div>
			<div class="form-group">
				<p><label for="page">Page (used by external services)</label><p>
				<input type="text" id="page" name="page" value="{{.Step.Page}}">
			</div>
			<div class="form-group">
				<p><label for="data-in">Data In (comma-separated)</label><p>
				<input type="text" id="data-in" name="data-in"
				value="{{csv .Step.DataIn}}">
			</div>
			<div class="form-group">
				<p><label for="data-out">Data Out (comma-separated)</label><p>
				<input type="text" id="data-out" name="data-out"
				value="{{csv .Step.DataOut}}">
			</div>
			<input type="hidden" name="_csrf" value="{{.csrf_token}}">
			<button type="submit" class="btn">Edit step</button>
		</div>
	</form>

</div>
{{template "base/footer" .}}

M templates/new-step.tmpl => templates/new-step.tmpl +4 -0
@@ 13,6 13,10 @@
				<input type="text" id="desc" name="desc">
			</div>
			<div class="form-group">
				<p><label for="post">Post To (comma-separated)</label><p>
				<input type="text" id="post" name="post">
			</div>
			<div class="form-group">
				<p><label for="page">Page (used by external services)</label><p>
				<input type="text" id="page" name="page">
			</div>