From a7e315d5736ebd3206fb1d507a54192359d09cb9 Mon Sep 17 00:00:00 2001 From: medcl Date: Fri, 18 Dec 2020 22:35:09 +0800 Subject: [PATCH] add api example --- api/index_management/index.go | 87 ++++++++++++++++++++++++++++++++++- api/init.go | 9 +++- main.go | 4 +- model/dict.go | 6 +++ search-center.yml | 22 +++++++-- 5 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 model/dict.go diff --git a/api/index_management/index.go b/api/index_management/index.go index ce59e5b3..3e0525d7 100644 --- a/api/index_management/index.go +++ b/api/index_management/index.go @@ -1,12 +1,95 @@ package index_management import ( + "fmt" + "infini.sh/framework/core/api" httprouter "infini.sh/framework/core/api/router" + "infini.sh/framework/core/orm" + "infini.sh/framework/core/util" + model2 "infini.sh/search-center/model" "net/http" ) +type APIHandler struct { + api.Handler +} -func API1(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { -} \ No newline at end of file +func (handler APIHandler)CreateDictItemAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + //id := ps.ByName("id") + dict:=model2.Dict{} + dict.ID = util.GetUUID() + + err := orm.Save(dict) + if err!=nil{ + panic(err) + } +} + +func (handler APIHandler) DeleteDictItemAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + id := ps.ByName("id") + dict:=model2.Dict{} + dict.ID = id + + + err := orm.Delete(dict) + if err!=nil{ + panic(err) + } +} + +func (handler APIHandler) DeleteDictItemAction2(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + + field:=handler.GetParameterOrDefault(req,"help","help message") + fmt.Println(field) + + json,err:=handler.GetJSON(req) + if err!=nil{ + handler.Error(w,err) + return + } + + id,err:=json.String("id") + if err!=nil{ + handler.Error(w,err) + return + } + dict:=model2.Dict{} + dict.ID = id + + + err = orm.Delete(dict) + if err!=nil{ + handler.Error(w,err) + } +} + + + +// TaskAction handle task creation and return task list which support parameter: `from`, `size` and `host`, eg: +//curl -XGET http://127.0.0.1:8001/task?from=100&size=10&host=elasticsearch.cn +func (handler APIHandler) TaskAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + + //fr := handler.GetParameter(req, "from") + //si := handler.GetParameter(req, "size") + //host := handler.GetParameter(req, "host") + //status := handler.GetIntOrDefault(req, "status", -1) + // + //from, err := strconv.Atoi(fr) + //if err != nil { + // from = 0 + //} + //size, err := strconv.Atoi(si) + //if err != nil { + // size = 10 + //} + // + //orm.Search() + //total, tasks, err := model.GetTaskList(from, size, host, status) + //if err != nil { + // handler.Error(w, err) + //} else { + // handler.WriteJSONListResult(w, total, tasks, http.StatusOK) + } + diff --git a/api/init.go b/api/init.go index 1bf25418..fecc5284 100644 --- a/api/init.go +++ b/api/init.go @@ -7,5 +7,10 @@ import ( ) func Init() { - ui.HandleUIMethod(api.POST, "/api/get_indices",index_management.API1) -} \ No newline at end of file + handler:=index_management.APIHandler{} + //ui.HandleUIMethod(api.POST, "/api/get_indices",index_management.API1) + ui.HandleUIMethod(api.POST, "/api/dict/_create",handler.CreateDictItemAction) + //ui.HandleUIMethod(api.GET, "/api/dict/:id",handler.GetDictItemAction) + ui.HandleUIMethod(api.DELETE, "/api/dict/:id",handler.DeleteDictItemAction) + ui.HandleUIMethod(api.DELETE, "/api/dict/",handler.DeleteDictItemAction2) +} diff --git a/main.go b/main.go index 8b1c6221..5722b2aa 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,10 @@ import ( "infini.sh/framework" "infini.sh/framework/core/env" "infini.sh/framework/core/module" + "infini.sh/framework/core/orm" "infini.sh/framework/modules" "infini.sh/search-center/config" + "infini.sh/search-center/model" ) var appConfig *config.AppConfig @@ -66,7 +68,7 @@ func main() { module.Start() }, func() { - + orm.RegisterSchema(model.Dict{}) }) } diff --git a/model/dict.go b/model/dict.go new file mode 100644 index 00000000..5bc21f7e --- /dev/null +++ b/model/dict.go @@ -0,0 +1,6 @@ +package model + +type Dict struct { + ID string `json:"id" elastic_meta:"_id"` + Url string `json:"title,omitempty"` +} diff --git a/search-center.yml b/search-center.yml index e026a2d9..3f575a8b 100644 --- a/search-center.yml +++ b/search-center.yml @@ -1,11 +1,11 @@ elasticsearch: - name: default enabled: true - endpoint: http://localhost:9200 + endpoint: https://192.168.3.98:9200 index_prefix: infini- basic_auth: username: elastic - password: 13A1EcwWs1He6w3BXHuJ + password: ZBdkVQUUdF1Sir4X4BGB web: enabled: true @@ -24,7 +24,7 @@ modules: enabled: true indexer_enabled: false store_enabled: false - orm_enabled: false + orm_enabled: true - name: web enabled: true network: @@ -35,3 +35,19 @@ search-center: ui_path: .public ui_vfs: true ui_local: true + + + +queue: + min_msg_size: 1 + max_msg_size: 5000000000 + max_bytes_per_file: 53687091200 + sync_every_records: 100000 # sync by records count + sync_timeout_in_ms: 10000 # sync by time in million seconds + read_chan_buffer: 0 + +statsd: + enabled: false + host: 127.0.0.1 + port: 8125 + namespace: gateway.