add api example

This commit is contained in:
medcl 2020-12-18 22:35:09 +08:00
parent 821f703dae
commit a7e315d573
5 changed files with 120 additions and 8 deletions

View File

@ -1,12 +1,95 @@
package index_management package index_management
import ( import (
"fmt"
"infini.sh/framework/core/api"
httprouter "infini.sh/framework/core/api/router" 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" "net/http"
) )
type APIHandler struct {
func API1(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { api.Handler
} }
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)
}

View File

@ -7,5 +7,10 @@ import (
) )
func Init() { func Init() {
ui.HandleUIMethod(api.POST, "/api/get_indices",index_management.API1) 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)
} }

View File

@ -6,8 +6,10 @@ import (
"infini.sh/framework" "infini.sh/framework"
"infini.sh/framework/core/env" "infini.sh/framework/core/env"
"infini.sh/framework/core/module" "infini.sh/framework/core/module"
"infini.sh/framework/core/orm"
"infini.sh/framework/modules" "infini.sh/framework/modules"
"infini.sh/search-center/config" "infini.sh/search-center/config"
"infini.sh/search-center/model"
) )
var appConfig *config.AppConfig var appConfig *config.AppConfig
@ -66,7 +68,7 @@ func main() {
module.Start() module.Start()
}, func() { }, func() {
orm.RegisterSchema(model.Dict{})
}) })
} }

6
model/dict.go Normal file
View File

@ -0,0 +1,6 @@
package model
type Dict struct {
ID string `json:"id" elastic_meta:"_id"`
Url string `json:"title,omitempty"`
}

View File

@ -1,11 +1,11 @@
elasticsearch: elasticsearch:
- name: default - name: default
enabled: true enabled: true
endpoint: http://localhost:9200 endpoint: https://192.168.3.98:9200
index_prefix: infini- index_prefix: infini-
basic_auth: basic_auth:
username: elastic username: elastic
password: 13A1EcwWs1He6w3BXHuJ password: ZBdkVQUUdF1Sir4X4BGB
web: web:
enabled: true enabled: true
@ -24,7 +24,7 @@ modules:
enabled: true enabled: true
indexer_enabled: false indexer_enabled: false
store_enabled: false store_enabled: false
orm_enabled: false orm_enabled: true
- name: web - name: web
enabled: true enabled: true
network: network:
@ -35,3 +35,19 @@ search-center:
ui_path: .public ui_path: .public
ui_vfs: true ui_vfs: true
ui_local: 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.