Skip to content

akzk/akgo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Akgo

Akgo is used for rapid development of RESTful APIs, web apps and backend services in Go.

Directory Structure

├── demo            // 根据Akgo编写的例程
|   └── filesharer  // demo1: 静态文件上传下载服务器
├── http            // HTTP框架
|   ├── errors      // 错误类型封装
|   ├── context.go  // HTTP上下文封装
|   └── server.go   // 框架核心
└── lib             // 模块、库
    └── crypto      // 加解密模块

Quick Start

download and install

go get github.com/akzk/akgo

create hello.go

package main

import "github.com/akzk/akgo/http"

func main() {
	server := http.NewServer()
	server.Serve(8080)
}

run hello.go

go run hello.go

The browser will show

hello, welcome to akgo

Congratulations!You've just built your first akgo app.

Document

Create a RESTful API (GET method)

  1. register URL and link the handler function

    func main() {
    	server := http.NewServer()
    	server.Get("/hello", sayhello)
    	server.Serve(8080)
    }
    
  2. define handler function

    func sayhello(context *http.Context) interface{} {
    
    	params := struct {
    		UserName string `get:"username"`
    	}{}
    
    	err := context.ParseURL(&params)
    	if err != nil {
    		return err
    	}
    
    	return []byte("hello, " + params.UserName)
    }
  3. run hello.go

    go run hello.go
    
  4. go to http://localhost:8080/hello?username=akzk

    will show

    hello, akzk
    

Create a RESTful API (POST method)

  1. register URL and link the handler function

    server.Post("/login", login)
    
  2. define handler function

    func login(context *http.Context) interface{} {
    
    	params := struct {
    		UserName string `json:"username"`
    		Passwd   string `json:"passwd"`
    	}{}
    
    	err := context.ParseBody(&params)
    	if err != nil {
    		return err
    	}
    
    	return []byte("login successfully, " + params.UserName)
    }
  3. 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"}
    

Download and Upload files

  1. 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"

Return type in handler function

  1. error
  2. Error
  3. []byte
  4. Response
  5. struct

About

a experimental go framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages