记录版本
安装
go get -u github.com/go-spring/[email protected]
go get -u github.com/go-spring/[email protected]
go get -u github.com/go-spring/[email protected]
最小化Web应用
package main
import (
"github.com/go-spring/spring-core/gs"
"github.com/go-spring/spring-core/web"
_ "github.com/go-spring/starter-gin"
"log"
)
func init() {
gs.GetMapping("/hello-go-spring", func(ctx web.Context) {
ctx.JSON(web.SUCCESS.Data("Hello Go-Spring!"))
})
}
func main() {
log.Fatal(gs.Run())
}
Go-Spring常用语法
1.对象注入spring bean
package main
import (
"fmt"
"github.com/go-spring/spring-core/gs"
"github.com/go-spring/spring-core/gs/cond"
"log"
)
type testA struct {
path string `value:"${GOPATH}"`
}
func (t *testA) String() string {
return "testA: " + t.path
}
type testB struct {
a *testA
}
func (t *testB) String() string {
return "testB: " + t.a.String()
}
func newTestB(a *testA) *testB {
return &testB{
a: a}
}
func init() {
gs.Object(new(testA)).Primary().Init(func(a *testA) {
fmt.Println("1." + a.String())
})
gs.Provide(new(testA)).Name("testA2").Init(func(a *testA) {
a.path = "newPath"
fmt.Println("2." + a.String())
})
gs.Object(new(testA)).Name("testA3").Destroy(func(a *testA) {
fmt.Println("destroy 3." + a.String())
}).Init(func(a *testA) {
fmt.Println("3." + a.String())
})
gs.Object(new(testA)).Name("testA4").On(cond.OnBean("testA3")).Init(func(a *testA) {
fmt.Println("4." + a.String())
})
gs.Object(new(testA)).Name("testA5").Order(1).Init(func(a *testA) {
fmt.Println("5." + a.String())
})
gs.Provide(newTestB).Init(func(b *testB) {
fmt.Println("6." + b.String())
})
}
func main() {
log.Fatal(gs.Run())
}
2.创建属性监听器
package main
import (
"fmt"
"github.com/go-spring/spring-core/gs"
"log"
)
type user struct {
Name string `value:"${name}"`
Age int `value:"${age}"`
}
func init() {
gs.OnProperty("GOPATH", func(s string) {
fmt.Println("4." + s)
})
gs.Setenv("user.name", "dragons")
gs.Setenv("user.age", "24")
gs.Property("xxx", "value")
gs.OnProperty("user", func(u user) {
fmt.Printf("5.%v\n", u)
})
gs.OnProperty("users", func(u []user) {
fmt.Printf("6.%v\n", u)
})
}
func main() {
log.Fatal(gs.Run())
}
Web应用常用语法
1.创建Web端点(Path)
1.1 Mapping方式
package main
import (
"github.com/go-spring/spring-core/gs"
"github.com/go-spring/spring-core/web"
_ "github.com/go-spring/starter-gin"
"log"
)
func init() {
gs.GetMapping("/get", func(ctx web.Context) {
ctx.GetHeader("user-agent")
ctx.QueryParam("")
_, _ = ctx.GetRawData()
ctx.JSON(web.SUCCESS)
ctx.JSON(map[string]interface{
}{
"code": 200, "msg": "SUCCESS"})
ctx.JSON(struct {
Code int `json:"code"`
Msg string `json:"msg"`
}{
200, "SUCCESS"})
ctx.String("message")
ctx.File("./config/application.yml")
})
gs.PostMapping("/post", func(ctx web.Context) {
ctx.FormValue("name")
})
gs.PutMapping("/put", func(ctx web.Context) {
})
gs.DeleteMapping("/delete", func(ctx web.Context) {
})
}
func main() {
log.Fatal(gs.Run())
}
1.2 Binding方式
package main
import (
"context"
"github.com/go-spring/spring-core/gs"
_ "github.com/go-spring/starter-gin"
"log"
)
type req struct {
Name string `json:"name" form:"name" xml:"name" yaml:"name" bson:"name"`
}
type resp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data string `json:"data"`
}
func init() {
gs.GetBinding("/get", func(ctx context.Context, r *req) *resp {
return &resp{
200, "请求成功", r.Name}
})
gs.PostBinding("/post", func(ctx context.Context, r *req) *resp {
return &resp{
200, "请求成功", r.Name}
})
gs.PutBinding("/put", func(ctx context.Context, r *req) *resp {
return &resp{
200, "请求成功", r.Name}
})
gs.DeleteBinding("/delete", func(ctx context.Context, r *req) *resp {
return &resp{
200, "请求成功", r.Name}
})
}
func main() {
log.Fatal(gs.Run())
}
1.3 Handle方式
package main
import (
"github.com/go-spring/spring-base/util"
"github.com/go-spring/spring-core/gs"
"github.com/go-spring/spring-core/web"
_ "github.com/go-spring/starter-gin"
"log"
)
type testHandler struct {
}
func (t *testHandler) Invoke(ctx web.Context) {
ctx.JSON(web.SUCCESS)
}
func (t *testHandler) FileLine() (file string, line int, fnName string) {
return util.FileLine(t.Invoke)
}
func init() {
gs.HandleGet("/get", &testHandler{
})
gs.HandlePost("/post", &testHandler{
})
gs.HandlePut("/put", &testHandler{
})
gs.HandleDelete("/delete", &testHandler{
})
}
func main() {
log.Fatal(gs.Run())
}
配置应用
多环境配置(以yml文件为例)
1.application-dev.yml
user:
name: dragons
age: 24
2.application-test.yml
user:
name: xxl
age: 14
3.main.go
package main
import (
"github.com/go-spring/spring-core/gs"
"github.com/go-spring/spring-core/web"
_ "github.com/go-spring/starter-gin"
"log"
)
type user struct {
Name string `value:"${name}"`
Age int `value:"${age}"`
}
func init() {
gs.Setenv("GS_SPRING_CONFIG_LOCATIONS", "config/")
gs.Setenv("GS_SPRING_PROFILES_ACTIVE", "dev")
gs.OnProperty("user", func(u user) {
gs.GetMapping("/user", func(ctx web.Context) {
ctx.JSON(web.SUCCESS.Data(u))
})
})
}
func main() {
log.Fatal(gs.Run())
}
配置方式
配置方式一:Tag value + autowire注入
1.config/application.yml
name: dragons
age: 24
2.main.go
package main
import (
"github.com/go-spring/spring-core/gs"
"github.com/go-spring/spring-core/web"
_ "github.com/go-spring/starter-gin"
"log"
)
type user struct {
Name string `json:"name" value:"${name:=}"`
Age int `json:"age" value:"${age:=0}"`
}
type userController struct {
u *user `autowire:"?"`
}
func (c *userController) echoUser(ctx web.Context) {
ctx.JSON(c.u)
}
func init() {
gs.Object(new(user))
gs.Object(new(userController)).Init(func(c *userController) {
gs.GetMapping("/echo-user", c.echoUser)
})
}
func main() {
log.Fatal(gs.Run())
}
配置方式二: Tag value 属性直接注入
1.config/application.yml
users:
- name: dragons
age: 24
- name: xxy
age: 14
2.main.go
package main
import (
"github.com/go-spring/spring-core/gs"
"github.com/go-spring/spring-core/web"
_ "github.com/go-spring/starter-gin"
"log"
)
type user struct {
Name string `json:"name" form:"name" yaml:"name" value:"${name}"`
Age int `json:"age" form:"age" yaml:"age" value:"${age}"`
}
type userController struct {
users []user `value:"${users}"`
}
func (c *userController) echoUsers(ctx web.Context) {
ctx.JSON(c.users)
}
func init() {
gs.Object(new(userController)).Init(func(controller * userController) {
gs.GetMapping("/echo-users", controller.echoUsers)
})
}
func main() {
log.Fatal(gs.Run())
}