Beispiel #1
0
func (h *handler) handlePostFlowTxn() error {

	base.Logf("handlePostFlowTxn...")

	flowInstanceReq := db.NewFlowTxnRequest()

	_, err := h.readObject(flowInstanceReq) //read JSON request into ftr object
	base.Logf("Read Object...")

	if err != nil {
		return base.HTTPErrorf(http.StatusBadRequest, "Could not map JSON to FlowTxnRequest Object")
	}

	base.Logf("Flow Def Key is", flowInstanceReq.FlowDefKey)

	//use flow def key to retrive workflow raw data and convert it to wf object
	flowDefData, _ := h.db.GetDocRaw(flowInstanceReq.FlowDefKey)
	flowDef := &db.WorkflowDef{}
	_ = json.Unmarshal(flowDefData, flowDef)

	flowInstance := db.NewFlowInstance(flowInstanceReq, flowDef)
	base.Logf("Flow Instance Key:%s", flowInstance.InstanceKey)

	// Save Flow Instance to database
	data, _ := json.Marshal(flowInstance)
	_, _ = h.db.PutDocRaw(flowInstance.InstanceKey, data)

	//JSON output created flow instance
	h.writeJSONStatus(http.StatusCreated, &flowInstance)
	return nil
}
Beispiel #2
0
// HTTP handler for Flow creation
//TO DO: Need to handle validations. Last Version write for deriving correct Def Keys on update.
// need to remove flow def from URL
func (h *handler) handlePostFlowDef() error {

	// derive flow name from URL path
	flowDefName := h.PathVar("flowName")
	base.Logf("...Flow Definition Name...", flowDefName)

	//generate flow def key
	flowDefKey, err := NextFlowDefKey(h.db, flowDefName)
	base.Logf("Flow Def Key:%s", flowDefKey)

	if flowDefKey == "" {
		return base.HTTPErrorf(http.StatusBadRequest, "Could not generate Flow Def Key")
	}

	wf := db.NewWorkflowDef(flowDefKey)

	_, err = h.readObject(wf)
	if err != nil {
		return base.HTTPErrorf(http.StatusBadRequest, "Could not map JSON to WfDef Object") //TO DO: Need to relook at error
	}

	//base.Logf("Encoding Workflow Def ..%s",wf)
	data, _ := json.Marshal(wf)

	//base.Logf("Saving Workflow Def ...%s",wf)
	_, _ = h.db.PutDocRaw(flowDefKey, data)
	//base.Logf("Done save...")

	// this should be inside db trx TO DO
	//_, err =writeLastVersion(h.db , wf.Name , )

	h.writeJSONStatus(http.StatusCreated, db.Body{"ok": true, "flowDefKey": flowDefKey})
	return nil
}
Beispiel #3
0
// HTTP handler for Flow query
func (h *handler) handleGetFlowDef() error {
	// derive flow def key from  URL path
	flowDefKey := h.PathVar("flowDefKey")
	base.Logf("Flow Definition Key: ", flowDefKey)

	if flowDefKey == "" {
		return base.HTTPErrorf(http.StatusBadRequest,
			"Invalid Flow Def Key")
	}

	data, err := h.db.GetDocRaw(flowDefKey)
	if err != nil {
		return base.HTTPErrorf(http.StatusBadRequest,
			"WF Definition Query Failed")
	}

	if data == nil {
		return base.HTTPErrorf(http.StatusBadRequest,
			"WF Definition Not Found. Check Flow Def Key again.")
	}

	wf := db.NewWorkflowDef(flowDefKey)

	err = json.Unmarshal([]byte(data), &wf)

	if err != nil {
		return err
	}

	h.writeJSONStatus(http.StatusCreated, wf)
	return nil
}
Beispiel #4
0
// HTTP handler for Flow Update
func (h *handler) handlePutFlowDef() error {

	// derive flow def key from  URL path
	flowDefKey := h.PathVar("flowDefKey")
	base.Logf("Flow Definition Key: ", flowDefKey)

	if flowDefKey == "" {
		return base.HTTPErrorf(http.StatusBadRequest, "Invalid Flow Def Key")
	}
	//
	wf := db.NewWorkflowDef(flowDefKey)

	// read JSON HTTP input into Object
	_, err := h.readObject(&wf)

	//base.Logf("Encoding Workflow Def ..%s",wf)
	data, _ := json.Marshal(wf)

	//base.Logf("Saving Workflow Def ...%s",wf)
	_, err = h.db.PutDocRaw(flowDefKey, data)
	//base.Logf("Done save...")

	if err != nil {
		return base.HTTPErrorf(http.StatusBadRequest, "Flow Definition Could not be updated") //TO DO: Need to relook at error
	}
	_, err = h.db.PutDocRaw(wf.Name, []byte(flowDefKey))

	// this should be inside db trx TO DO
	//_, err =writeLastVersion(h.db , wf.Name , )

	//db.Body{"ok": true, "flowDefKey": flowDefKey}
	h.writeJSONStatus(http.StatusCreated, &wf)
	return nil
}
Beispiel #5
0
//get existing or add database from config
func (sc *ServerContext) getOrAddDatabaseFromConfig(config *DbConfig) (*db.DatabaseContext, error) {
	// Obtain write lock during add database, to avoid race condition when creating based on ConfigServer
	sc.lock.Lock()
	defer sc.lock.Unlock()

	//return existing DB if opened already
	if sc.database_ != nil {
		return sc.database_, nil
	}

	//derive values from config
	bucketName := config.Name          // Default bucket
	dbName := config.Name              // Name used to identify DB
	dbDirectory := config.Name + ".db" //directory + DB file name

	if config.Bucket != nil {
		bucketName = *config.Bucket
	}

	if dbName == "" {
		dbName = bucketName
	}

	base.Logf("Opening db /%s as bucket %q ",
		dbName, bucketName)

	if err := db.ValidateDatabaseName(dbName); err != nil {
		return nil, err
	}

	spec := base.DatabaseSpec{Directory: dbDirectory,
		BucketName: bucketName,
		Name:       dbName}

	dbHandle, err := db.OpenDatabase(spec)
	if err != nil {
		return nil, err
	}

	dbcontext, err := db.NewDatabaseContext(dbName, dbHandle)
	if err != nil {
		return nil, err
	}

	// Register it so HTTP handlers can find it:
	sc.database_ = dbcontext

	// Save the config
	sc.config.Database = config

	return dbcontext, nil
}
Beispiel #6
0
// HTTP handler for Flow query
func (h *handler) handleGetByNameFlowDef() error {
	// derive flow def key from  URL path
	flowDef := h.PathVar("flowDef")
	base.Logf("Flow Definition: ", flowDef)

	if flowDef == "" {
		return base.HTTPErrorf(http.StatusBadRequest,
			"Invalid Flow Def Name")
	}
	// get all flow def versions

	data, err := h.db.GetDocRaw(flowDef)
	if err != nil {
		return base.HTTPErrorf(http.StatusBadRequest,
			"WF Definition Query Failed")
	}

	base.Logf("Flow Def Key:%s", string(data))

	h.writeJSONStatus(http.StatusCreated, nil)
	return nil
}
Beispiel #7
0
func RunServer(config *ServerConfig) {

	//This variable used for pretty JSON reponses
	PrettyPrint = config.Pretty

	//New Server Context
	sc := NewServerContext(config)

	//Open Database and add to server context
	if _, err := sc.AddDatabaseFromConfig(config.Database); err != nil {
		base.LogFatal("Error opening database: %v", err)
	}

	defer sc.CloseDatabase()

	base.Logf("Starting server on %s ...", *config.Interface)
	config.serve(*config.Interface, CreatePublicHandler(sc))

}