Akgo is used for rapid development of RESTful APIs, web apps and backend services in Go.
├── demo // 根据Akgo编写的例程
| └── filesharer // demo1: 静态文件上传下载服务器
├── http // HTTP框架
| ├── errors // 错误类型封装
| ├── context.go // HTTP上下文封装
| └── server.go // 框架核心
└── lib // 模块、库
└── crypto // 加解密模块
go get github.com/akzk/akgo
package main
import "github.com/akzk/akgo/http"
func main() {
server := http.NewServer()
server.Serve(8080)
}
go run hello.go
Go to http://localhost:8080
The browser will show
hello, welcome to akgo
Congratulations!You've just built your first akgo app.
-
register URL and link the handler function
func main() { server := http.NewServer() server.Get("/hello", sayhello) server.Serve(8080) }
-
define handler function
func sayhello(context *http.Context) interface{} { params := struct { UserName string `get:"username"` }{} err := context.ParseURL(¶ms) if err != nil { return err } return []byte("hello, " + params.UserName) }
-
run hello.go
go run hello.go
-
go to http://localhost:8080/hello?username=akzk
will show
hello, akzk
-
register URL and link the handler function
server.Post("/login", login)
-
define handler function
func login(context *http.Context) interface{} { params := struct { UserName string `json:"username"` Passwd string `json:"passwd"` }{} err := context.ParseBody(¶ms) if err != nil { return err } return []byte("login successfully, " + params.UserName) }
-
access the api with json body
this is a example
POST /login HTTP/1.1 Host: 127.0.0.1:8080 Content-Type: application/x-www-form-urlencoded Content-Length: 37 {"username":"akzk","passwd":"123456"}
-
register URLs
server.Down("/download", "/usr/local/project/downloadfiles") server.Up("/upload", "/usr/local/project/uploadfiles")
then, you can download file by "http://localhost:8080/download/path/to/file" and upload file by "http://localhost:8080/upload"
- error
- Error
- []byte
- Response
- struct