github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/server/editor.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package server
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"github.com/cloudwan/gohan/extension"
    22  	"github.com/cloudwan/gohan/schema"
    23  	"github.com/cloudwan/gohan/util"
    24  )
    25  
    26  func setupEditor(server *Server) {
    27  	manager := schema.GetManager()
    28  	config := util.GetConfig()
    29  	editableSchemaFile := config.GetString("editable_schema", "")
    30  	extension.RegisterGoCallback("handle_schema",
    31  		func(event string, context map[string]interface{}) error {
    32  			auth := context["auth"].(schema.Authorization)
    33  			if event == "pre_list" {
    34  				list := []interface{}{}
    35  				total := 0
    36  				for _, currentSchema := range manager.OrderedSchemas() {
    37  					trimmedSchema, err := schema.GetSchema(currentSchema, auth)
    38  					if err != nil {
    39  						return err
    40  					}
    41  					if trimmedSchema != nil {
    42  						s := trimmedSchema.Data()
    43  						s["url"] = currentSchema.URL
    44  						list = append(list, s)
    45  						total = total + 1
    46  					}
    47  				}
    48  				context["total"] = total
    49  				context["response"] = map[string]interface{}{
    50  					"schemas": list,
    51  				}
    52  				return nil
    53  			} else if event == "pre_show" {
    54  				ID := context["id"].(string)
    55  				currentSchema, _ := manager.Schema(ID)
    56  				object, _ := schema.GetSchema(currentSchema, auth)
    57  				s := object.Data()
    58  				s["url"] = currentSchema.URL
    59  				context["response"] = map[string]interface{}{
    60  					"schema": s,
    61  				}
    62  				return nil
    63  			}
    64  			if event != "pre_create" && event != "pre_update" && event != "pre_delete" {
    65  				return nil
    66  			}
    67  
    68  			if editableSchemaFile == "" {
    69  				return nil
    70  			}
    71  
    72  			ID := context["id"].(string)
    73  
    74  			schemasInFile, err := util.LoadFile(editableSchemaFile)
    75  			if err != nil {
    76  				return nil
    77  			}
    78  			schemas := schemasInFile["schemas"].([]interface{})
    79  			updatedSchemas := []interface{}{}
    80  			var existingSchema map[string]interface{}
    81  			for _, rawSchema := range schemas {
    82  				s := rawSchema.(map[string]interface{})
    83  				if s["id"] == ID {
    84  					existingSchema = s
    85  				} else {
    86  					updatedSchemas = append(updatedSchemas, s)
    87  				}
    88  			}
    89  
    90  			if event == "pre_create" {
    91  				if existingSchema != nil {
    92  					return fmt.Errorf("ID has been taken")
    93  				}
    94  				schemasInFile["schemas"] = append(updatedSchemas, context["resource"].(map[string]interface{}))
    95  				context["response"] = map[string]interface{}{
    96  					"schema": context["resource"],
    97  				}
    98  				context["exception"] = map[string]interface{}{
    99  					"name":    "CustomException",
   100  					"message": context["response"],
   101  					"code":    201,
   102  				}
   103  			} else if event == "pre_update" {
   104  				if existingSchema == nil {
   105  					return fmt.Errorf("Not found or Update isn't supported for this schema")
   106  				}
   107  				for key, value := range context["resource"].(map[string]interface{}) {
   108  					existingSchema[key] = value
   109  				}
   110  				schemasInFile["schemas"] = append(updatedSchemas, existingSchema)
   111  				context["response"] = map[string]interface{}{
   112  					"schema": context["resource"],
   113  				}
   114  				context["exception"] = map[string]interface{}{
   115  					"name":    "CustomException",
   116  					"message": context["response"],
   117  					"code":    200,
   118  				}
   119  
   120  			} else if event == "pre_delete" {
   121  				if existingSchema == nil {
   122  					return fmt.Errorf("Not found or Delete isn't supported for this schema")
   123  				}
   124  				schemasInFile["schemas"] = updatedSchemas
   125  				deletedSchema, ok := manager.Schema(ID)
   126  				if ok {
   127  					manager.UnRegisterSchema(deletedSchema)
   128  				}
   129  				context["exception"] = map[string]interface{}{
   130  					"name":    "CustomException",
   131  					"message": map[string]interface{}{"result": "deleted"},
   132  					"code":    204,
   133  				}
   134  			}
   135  			util.SaveFile(editableSchemaFile, schemasInFile)
   136  			err = manager.LoadSchemaFromFile(editableSchemaFile)
   137  			if err != nil {
   138  				return err
   139  			}
   140  			server.initDB()
   141  			server.resetRouter()
   142  			server.mapRoutes()
   143  			return nil
   144  		})
   145  }