本文整理汇总了Golang中github.com/ant0ine/go-json-rest/rest.Post函数的典型用法代码示例。如果您正苦于以下问题:Golang Post函数的具体用法?Golang Post怎么用?Golang Post使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Post函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: MsgServerStart
func MsgServerStart() {
mqmsg := msgqueue.MqMsg{
Msg: message.NewMsg(),
}
persistent := PerMqAgent{
Msg: message.NewMsg(),
}
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
//rest.Get("/test", msg.TestMsg),
rest.Get("/testmq/:mqid", msgqueue.DefaultMM.TestMq),
rest.Post("/send_msg/:mqid", mqmsg.PostMsg),
rest.Get("/receive_msg/:mqid", mqmsg.GetMsg),
rest.Post("/send/:mqid", persistent.PostMsg),
rest.Get("/receive/:mqid", persistent.GetMsg),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":"+ListenPort, api.MakeHandler()))
}
示例2: main
func main() {
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
rest.Get("/test", testApi),
rest.Post("/addTask", plain.AddTask),
rest.Get("/getTasks", plain.GetTasks),
rest.Post("/removeTask", plain.RemoveTask),
rest.Post("/completeTask", plain.CloseTask),
rest.Post("/rearrangeTasks", plain.RearrangeTasks),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
http.Handle("/api/", http.StripPrefix("/api", api.MakeHandler()))
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("frontend"))))
log.Println("Listening...")
http.ListenAndServe(":3000", nil)
}
示例3: MakeHandler
// MakeHandler creates the api request handler
func MakeHandler() *http.Handler {
api := rest.NewApi()
authMiddleware, err := CreateAuthMiddleware()
if err != nil {
panic(err)
}
api.Use(&rest.IfMiddleware{
// Only authenticate non login or register requests
Condition: func(request *rest.Request) bool {
return (request.URL.Path != variables.APIPathLoginUserServer) && (request.URL.Path != variables.APIPathRegisterUserServer)
},
IfTrue: authMiddleware,
})
api.Use(rest.DefaultProdStack...)
router, err := rest.MakeRouter(
rest.Post(variables.APIPathLoginUserServer, authMiddleware.LoginHandler),
rest.Get(variables.APIPathRefreshUserServer, authMiddleware.RefreshHandler),
rest.Post(variables.APIPathRegisterUserServer, PostRegister),
rest.Get(variables.APIPathUserServer, GetUser),
rest.Post(variables.APIPathUserServer, PostUser),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
handler := api.MakeHandler()
return &handler
}
示例4: NewRest
// NewRest configures REST api handlers and wrappers.
func NewRest() *rest.Api {
api := rest.NewApi()
api.MakeHandler()
api.Use(rest.DefaultDevStack...)
api.Use(&rest.CorsMiddleware{
RejectNonCorsRequests: false,
OriginValidator: func(origin string, request *rest.Request) bool {
return true
},
AllowedMethods: []string{"GET", "POST"},
AllowedHeaders: []string{
"Accept", "Content-Type", "X-Custom-Header", "Origin"},
AccessControlAllowCredentials: true,
AccessControlMaxAge: 3600,
})
router, err := rest.MakeRouter(
rest.Post("/translations", translations),
rest.Post("/suggestions", suggestions),
rest.Get("/health", health),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
return api
}
示例5: initRoutes
func initRoutes(restApi *rest.Api) {
authController := &AuthController{}
appController := &ApplicationController{}
typesController := &TypesController{}
router, err := rest.MakeRouter(
rest.Put(authController.Path(), authController.RegisterUserHandler),
rest.Post(authController.Path(), authController.LoginUserHandler),
rest.Put("/:appId"+authController.Path(), authController.AppRegisterUserHandler),
rest.Post("/:appId"+authController.Path(), authController.AppLoginUserHandler),
rest.Post(appController.Path(), appController.CreateApplicationHandler),
rest.Get(appController.Path(), appController.GetApplicationsHandler),
rest.Get(appController.Path()+"/:appId", appController.GetApplicationHandler),
rest.Delete(appController.Path()+"/:appId", appController.DeleteApplicationHandler),
rest.Put(appController.Path()+"/:appId", appController.UpdateApplicationHandler),
rest.Post("/:appId"+typesController.Path(), typesController.CreateTypeHandler),
rest.Delete("/:appId"+typesController.Path()+"/:typeName", typesController.DeleteType),
rest.Post("/:appId"+typesController.Path()+"/:typeName", typesController.InsertInTypeHandler),
rest.Get("/:appId"+typesController.Path()+"/:typeName", typesController.GetTypeDataHandler),
rest.Get("/:appId"+typesController.Path()+"/:typeName/:itemId", typesController.GetTypeItemById),
rest.Put("/:appId"+typesController.Path()+"/:typeName/:itemId", typesController.UpdateTypeItemById),
rest.Delete("/:appId"+typesController.Path()+"/:typeName/:itemId", typesController.DeleteTypeItemById),
)
if err != nil {
log.Fatal(err)
}
restApi.SetApp(router)
}
示例6: main
func main() {
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
//配置API跨域CORS
api.Use(&rest.CorsMiddleware{
RejectNonCorsRequests: false,
OriginValidator: func(origin string, req *rest.Request) bool {
return origin == req.Header.Get("Origin")
},
AllowedMethods: []string{"GET", "POST", "PUT"},
AllowedHeaders: []string{
"Accept", "Content-Type", "X-Custom-Header", "Origin"},
AccessControlAllowCredentials: true,
AccessControlMaxAge: 3600,
})
//设置API基本Router
router, err := rest.MakeRouter(
rest.Post("/:raw.json", RawWrite),
rest.Post("/auth/login/:id", AuthLogin),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
}
示例7: Start
// Start start the http rest api plugin
func (cli *HTTPAPIServer) Start(config map[string]interface{}) error {
wwwroot, _ := utils.GetString("static", config, "")
addr, _ := utils.GetString("listen", config, ":8080")
secret, ok := utils.GetString("secret", config, randomString(128))
if !ok {
logrus.Warnf("Plugins: httpapi: generated new secret: %s", secret)
}
jwt_middleware := &jwt.JWTMiddleware{
Key: []byte(secret),
Realm: "jwt auth",
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
Authenticator: func(userId string, password string) bool {
return userId == "admin" && password == "admin"
}}
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
api.Use(&rest.IfMiddleware{
Condition: func(request *rest.Request) bool {
return request.URL.Path != "/login"
},
IfTrue: jwt_middleware,
})
router, err := rest.MakeRouter(
rest.Post("/login", jwt_middleware.LoginHandler),
rest.Get("/refresh_token", jwt_middleware.RefreshHandler),
rest.Get("/", getApiVersion),
rest.Get("/sensors", getAllSensors),
rest.Get("/sensors/:backend.:sensor", getSensor),
rest.Get("/services", getAllServices),
rest.Get("/services/:backend.:service", getService),
rest.Post("/services/:backend.:service", callService),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
http.Handle("/v1/", http.StripPrefix("/v1", api.MakeHandler()))
if wwwroot != "" {
logrus.Infof("Plugin: httpapi: service static content from %s", wwwroot)
http.Handle("/ui/", http.StripPrefix("/ui", http.FileServer(http.Dir(wwwroot))))
}
go func() {
logrus.Infof("Plugin: httpapi: starting on %s", addr)
manners.ListenAndServe(addr, http.DefaultServeMux)
}()
return nil
}
示例8: main
func main() {
var err error
api := rest.NewApi()
// Enable basic middlewares.
api.Use(rest.DefaultDevStack...)
// Enable CORS middleware.
api.Use(&rest.CorsMiddleware{
RejectNonCorsRequests: false,
OriginValidator: func(origin string, request *rest.Request) bool {
return true
},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{
"Accept", "Content-Type", "X-Custom-Header", "Origin"},
AccessControlAllowCredentials: true,
AccessControlMaxAge: 3600,
})
// Enable token validation middleware.
api.Use(&mw.AuthMW{})
router, err := rest.MakeRouter(
// cards.
rest.Get("/cards", GetUserCards),
rest.Get("/cards/:cid", GetCard),
rest.Post("/cards", CreateCard),
rest.Put("/cards/:cid", UpdateCard),
rest.Delete("/cards/:cid", DeleteCard),
rest.Get("/challenge", GetChallenge),
rest.Post("/results", SaveResult),
// user endpoints.
rest.Get("/users/:uid", GetUser),
rest.Put("/users/:uid", UpdateUser),
rest.Delete("/users/:uid", DeleteUser),
// token
rest.Post("/token", GetToken),
// ping
rest.Get("/ping", Ping),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
}
示例9: main
func main() {
DataFile, err := os.Open(configFile)
if err != nil {
log.Fatal("Error opening Config file", err.Error())
os.Exit(2)
}
jsonParser := json.NewDecoder(DataFile)
if err = jsonParser.Decode(&serverData); err != nil {
log.Fatal("Error parsing config file", err.Error())
os.Exit(2)
}
Println(timeNow, " --- Config file ", configFile, " loaded")
Println(timeNow, " --- Starting web server listener on host:", serverData.Host, "on port:", serverData.Port)
jwt_middleware := &jwt.JWTMiddleware{
Key: []byte("secret key"),
Realm: "jwt auth",
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
Authenticator: func(userId string, password string) bool {
return userId == "admin" && password == "admin"
}}
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
// we use the IfMiddleware to remove certain paths from needing authentication
api.Use(&rest.IfMiddleware{
Condition: func(request *rest.Request) bool {
return request.URL.Path != "/login"
},
IfTrue: jwt_middleware,
})
api_router, _ := rest.MakeRouter(
rest.Post("/login", jwt_middleware.LoginHandler),
rest.Get("/auth_test", handle_auth),
rest.Get("/refresh_token", jwt_middleware.RefreshHandler),
rest.Get("/index", Index),
rest.Get("/messages", GetAllMessages),
rest.Post("/messages", PostMessage),
rest.Get("/countries/:code", GetMessage),
// rest.Delete("/countries/:code", DeleteMessage),
)
api.SetApp(api_router)
http.Handle("/api/", http.StripPrefix("/api", api.MakeHandler()))
log.Fatal(http.ListenAndServe(serverData.Host+":"+serverData.Port, nil))
}
示例10: Serve
// Serve a json api
func Serve() {
redisPool = newPool(redisServer, redisPassword)
api := rest.NewApi()
api.Use(&rest.IfMiddleware{
Condition: func(request *rest.Request) bool {
return request.URL.Path != "/login"
},
IfTrue: &tokenauth.AuthTokenMiddleware{
Realm: authRealm,
Authenticator: func(token string) string {
rd := redisPool.Get()
defer rd.Close()
user, _ := redis.String(rd.Do("GET", tokenNamespace+tokenauth.Hash(token)))
return user
},
},
IfFalse: &rest.AuthBasicMiddleware{
Realm: authRealm,
Authenticator: func(user string, password string) bool {
if user == "user" && password == "password" {
return true
}
return false
},
},
})
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
rest.Post("/login", login),
rest.Get("/containers", list),
rest.Get("/container/:id", inspect),
rest.Post("/image/build", build),
rest.Post("/image/run", create),
rest.Get("/container/start/:id", start),
rest.Get("/container/stop/:id", stop),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Println("Port", port)
log.Fatal(http.ListenAndServe(":"+port, api.MakeHandler()))
}
示例11: RunApiServer
func (fx_api *FxDaemonApi) RunApiServer() {
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
rest.Post("/childs", fx_api.ChildServers),
rest.Post("/child/iplookup", fx_api.ChildServerIpLookup),
rest.Post("/transfer/container", fx_api.TransferContainer),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(fx_api.Fxd.ListenHost, api.MakeHandler()))
}
示例12: main
func main() {
// context
ctx = context.Background()
// db
hoge.BuildInstances()
// redis
redis_pool := newPool()
ctx = context.WithValue(ctx, "redis", redis_pool)
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
rest.Post("/test", baseHandlerFunc(controller.Test)),
)
// 存在しないルート時
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
httpErr := http.ListenAndServe(":9999", api.MakeHandler())
if httpErr != nil {
log.Fatal(err, "http error!!")
}
}
示例13: main
func main() {
// context
ctx = context.Background()
db := initDb()
ctx = context.WithValue(ctx, "test", "aaabbbccc")
ctx = context.WithValue(ctx, "DB", db)
// redis
redis_pool := newPool()
ctx = context.WithValue(ctx, "redis", redis_pool)
str := ctx.Value("test")
log.Println(str)
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
rest.Post("/test", baseHandlerFunc(controller.Test)),
)
// 存在しないルート時
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":9999", api.MakeHandler()))
}
示例14: main
func main() {
jwt_middleware := &jwt.JWTMiddleware{
Key: []byte("secret key"),
Realm: "jwt auth",
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
Authenticator: func(userId string, password string) bool {
return userId == "admin" && password == "admin"
}}
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
// we use the IfMiddleware to remove certain paths from needing authentication
api.Use(&rest.IfMiddleware{
Condition: func(request *rest.Request) bool {
return request.URL.Path != "/login"
},
IfTrue: jwt_middleware,
})
api_router, _ := rest.MakeRouter(
rest.Post("/login", jwt_middleware.LoginHandler),
rest.Get("/auth_test", handle_auth),
rest.Get("/refresh_token", jwt_middleware.RefreshHandler),
)
api.SetApp(api_router)
http.Handle("/api/", http.StripPrefix("/api", api.MakeHandler()))
log.Fatal(http.ListenAndServe(":8080", nil))
}
示例15: StartServer
func (app *App) StartServer(addr string, dbFileName string) {
err := app.InitDB(dbFileName)
if err != nil {
log.Fatal(err)
}
api := rest.NewApi()
api.Use(rest.DefaultProdStack...)
api.Use(&rest.CorsMiddleware{
RejectNonCorsRequests: false,
AllowedMethods: []string{"GET", "POST"},
AllowedHeaders: []string{
"Accept", "Content-Type", "Origin"},
AccessControlAllowCredentials: true,
AccessControlMaxAge: 2592000,
OriginValidator: func(origin string, request *rest.Request) bool {
return true
},
})
router, err := rest.MakeRouter(
rest.Get("/latest", app.GetLatest),
rest.Get("/last", app.GetLast),
rest.Get("/all", app.GetAll),
rest.Post("/postEntry", app.PostEntry),
//rest.Post("/postSignedEntry", app.PostJwtEntry),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(addr, api.MakeHandler()))
}