Categorygithub.com/gkyh/gts
modulepackage
1.12.5
Repository: https://github.com/gkyh/gts.git
Documentation: pkg.go.dev

# README

gts Web Framework

golang搭建极简的原生WEB后台项目


  import (  
    "github.com/gkyh/gts"  
    "net/http"  
   )  
 
   func main() {  
     route := gts.New()  
     
     //3600 session 超时时间;单位:秒;
     //0 cookie 储存时间,0 关闭浏览器失效,>0超时时间
     route.Cookie("gosessionid", 3600, 0)  
     //实现RouteLogger接口的均可
     //type RouteLogger interface {
     //     Println(v ...interface{})
     // }
     var logger *log.Logger
     route.Logger(logger)
       

    //静态文件,参数1 请求url路径,参数2 请求文件路径
    route.Static("/public", "./public/") 

    //Favicon.ico文件路径  
    route.Favicon("./")

    //中间件 
    route.UseErrResp() 
    route.Use(ws)  
    route.Use(ws2)   
    route.Route("/test", testHandler, HandleIterceptor)  
    route.Group("/group", groupHandler)  
      
    route.Get("/login", func(req *http.Request,ctx *gts.Context) {  

      session:= ctx.Session()
      session.Set("username", "root")
      ctx.WriteString(200, "login")   
    })  

    //注册路由必须现实 func(req *http.Request, c *gts.Context) 格式的函数
    route.Get("/user", func(req *http.Request, c *gts.Context) {  

      session:= c.Session()  
      user := c.Get("username")  
      if user !=nil {  
        c.WriteString(200, user.(string))   
      } else {  
        c.WriteString(404, "not found")  
      }  
    })  

    //route.Any("/any", func)  添加get 和 post 方法
    //route.Post("/post", func)  
    //route.Get("/get", func)  
    //route.Delete("/delete", func)  
   
    route.Run(":8080")

  }
  func groupHandler(route *gts.Router){  
    
    //路由:/group/test
  	route.Get("/test",testFunc)
	  route.Post("/test",testFunc)
  }
  //中间件定义
  func ws(next gts.HandlerFunc) gts.HandlerFunc {  
     return func(ctx *gts.Context) {  

     ip := getRemoteIp(ctx.Request)
      fmt.Println("request ip:" + ip)

	   v := map[string]interface{}{
			"reqIP": ip,
	   }
	  ctx.Set("context", v)
	  next(ctx)

    }  
  }  
  
  //拦截器,(也是中间件的一种)
  func HandleIterceptor(next gts.HandlerFunc) gts.HandlerFunc {. 
    return func(c *gts.Context) {   
  
    ip := r.RemoteAddr   
    fmt.Println("handleIterceptor,ip:" + ip)   

    user, _ := c.SessionVal("username")
    if user != nil { 

      fmt.Println(user.(string))   
  
      v := gts.ContextValue{   
        "reqIP":    ip,   
        "username": user.(string),   
      }  
  
      c.Set("context", v)  
      next(c)  
      return  
    }  
  
    c.Redirect("/login")    
    return   
  
  }. 
  }

###TestConterller


var testHandler *TestConterller = new(TestConterller)   
  
type TestConterller struct {    
}   
//必须在Router方法中添加路由
func (p *TestConterller) Router(router *gts.Router) {

    router.Any("/list", p.mlogHandler)  

}  

func (p *TestConterller) ListHanlder(req *http.Request,c *gts.Context) {  

    req.ParseForm()
    data := req.FormValue("data")
    
    // 接收数据解析到struct
    form := &ReqForm{}
    err := gts.Bind(req, form)

}  

Context 返回数据方法


  Write(b []byte)  
  
  WriteString(status int, s string)

  HTML(status int, html string)  
  
  JSON(status int, m map[string]interface{})  
  
  Result(s string)   
  
  Msg(s string) //=》{"code": 200, "msg": s}  
  
  OK()  //=〉{"code": 200, "msg": "处理成功"}
  
  Redirect(url string)   

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Decode docode with custom alphabet.
No description provided by the author
Encode encode with custom alphabet.
No description provided by the author
No description provided by the author
No description provided by the author
NewAlphabet create a custom alphabet from 58-length string.
创建会话管理器(cookieName:在浏览器中cookie的名字;maxLifeTime:最长生命周期).
创建会话管理器(cookieName:在浏览器中cookie的名字;maxLifeTime:最长生命周期).
No description provided by the author
success default is true, code default is 200.
No description provided by the author
success default is true, code default is 200.
No description provided by the author

# Constants

外部异常,通常为请求第三方.
提交错误.
服务器异常,程序抛出异常.
请求业务无法成功受理,业务自定义.
访问被禁止.
认证失败或操作超时.
不接受请求,如IP被限制.
没有操作权限.
请求处理已完成或重复请求.
请求业务状态错误.
fail,reason is not found.
success.
fail,reason is unknown.
请求未能应答.

# Variables

Alphabet: copy from https://en.wikipedia.org/wiki/Base58.
Errors.
Alphabet: copy from https://en.wikipedia.org/wiki/Base58.
Alphabet: copy from https://en.wikipedia.org/wiki/Base58.
Alphabet: copy from https://en.wikipedia.org/wiki/Base58.
No description provided by the author

# Structs

Alphabet The base58 alphabet object.
No description provided by the author
Session会话管理*/.
——————————————————————————会话*/.
No description provided by the author
Session会话管理*/.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author