添加项目模板
This commit is contained in:
parent
5be48a34e5
commit
516e76bf8f
|
@ -73,6 +73,10 @@ func getAppKey() string {
|
|||
func handlerFunc(node routerNode) func(writer http.ResponseWriter, request *http.Request) {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
method := request.Method
|
||||
if node.patten != request.URL.Path {
|
||||
directOutput(writer, http.StatusNotFound, []byte("Not Found"))
|
||||
return
|
||||
}
|
||||
if node.method != "ALL" && node.method != method {
|
||||
directOutput(writer, http.StatusNotFound, []byte("Not Found"))
|
||||
return
|
||||
|
|
|
@ -15,8 +15,6 @@ type MiddleWare interface {
|
|||
Func() MiddlewareFunc
|
||||
}
|
||||
|
||||
type csrfSessionToken string
|
||||
|
||||
// 翻转中间件,因执行时是从最外层函数开始,定义时需要从最后一个中间件开始定义
|
||||
func reverseMiddleWare(middlewares []MiddleWare) []MiddleWare {
|
||||
length := len(middlewares)
|
||||
|
@ -29,7 +27,7 @@ func reverseMiddleWare(middlewares []MiddleWare) []MiddleWare {
|
|||
func startCsrfToken(session session.Store) string {
|
||||
csrfToken := getAppKey() + time.Now().String()
|
||||
csrfTokenMd5 := encrypt.Md5ToUpper(csrfToken)
|
||||
sessionKey := csrfSessionToken("CSRF-TOKENrr")
|
||||
sessionKey := "CSRF-TOKEN"
|
||||
|
||||
if token := session.Get(sessionKey); token != nil {
|
||||
return token.(string)
|
||||
|
@ -41,7 +39,10 @@ func startCsrfToken(session session.Store) string {
|
|||
|
||||
func checkCsrfToken(next HandlerFunc) HandlerFunc {
|
||||
return func(c *Context) error {
|
||||
|
||||
tokenInHeader := c.request.Header.Get("CSRF-TOKEN")
|
||||
if c.request.Method == "POST" && tokenInHeader != c.CsrfToken {
|
||||
return c.ResponseWrite([]byte("csrf token verify error"))
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,6 +59,10 @@ func sessioinStart(writer http.ResponseWriter, request *http.Request) session.St
|
|||
logger.Error("session start error:%v", sessErr)
|
||||
return blankSession
|
||||
}
|
||||
|
||||
if sessErr != nil {
|
||||
logger.Warning("session start warning:%v", sessErr)
|
||||
}
|
||||
if sessHandler == nil {
|
||||
return blankSession
|
||||
}
|
||||
|
|
|
@ -5,12 +5,12 @@ import (
|
|||
"gitee.com/zhucheer/cfg"
|
||||
)
|
||||
|
||||
var Config = cfg.New("./config/config.toml")
|
||||
var Config = cfg.New("")
|
||||
|
||||
func init() {
|
||||
|
||||
path := flag.String("config", "./config/config.toml", "config file path")
|
||||
flag.Parse()
|
||||
Config = cfg.New(*path)
|
||||
|
||||
Config = cfg.New(*path)
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ func NewMysql() DataBase {
|
|||
|
||||
// 注册所有已配置的mysql
|
||||
func (my *MysqlDB) RegisterAll() {
|
||||
logger.Info("##register all mysql##")
|
||||
databaseConfig := cfg.Config.GetMap("database.mysql")
|
||||
|
||||
my.count = len(databaseConfig)
|
||||
|
|
|
@ -34,7 +34,7 @@ func NewRedis() DataBase {
|
|||
return redisConn
|
||||
}
|
||||
|
||||
// 注册所有已配置的mysql
|
||||
// 注册所有已配置的 redis
|
||||
func (re *RedisDB) RegisterAll() {
|
||||
logger.Info("##register all redis##")
|
||||
redisConfig := cfg.Config.GetMap("database.redis")
|
||||
|
|
23
main.go
23
main.go
|
@ -1,13 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"gitee.com/zhucheer/orange/app"
|
||||
"gitee.com/zhucheer/orange/outapp"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
router := &outapp.Route{}
|
||||
app.AppStart(router)
|
||||
fmt.Println("run")
|
||||
argNum := len(os.Args)
|
||||
if argNum >= 3 {
|
||||
secondArg := os.Args[1]
|
||||
inputArg := os.Args[2]
|
||||
switch secondArg {
|
||||
case "create":
|
||||
createProject(inputArg)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func createProject(projectName string) {
|
||||
|
||||
fmt.Println("create project" + projectName)
|
||||
|
||||
}
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
package outapp
|
||||
|
||||
import (
|
||||
"gitee.com/zhucheer/orange/app"
|
||||
"gitee.com/zhucheer/orange/logger"
|
||||
"gitee.com/zhucheer/orange/outapp/controller"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
}
|
||||
|
||||
func (s *Route) ServeMux() {
|
||||
|
||||
mm := app.GroupRouter("/api", NewAuth())
|
||||
{
|
||||
|
||||
mm.ALL("/hello", controller.Test)
|
||||
mm.GET("/hello2", controller.Test2)
|
||||
mm.GET("/redis", controller.RedisTT)
|
||||
|
||||
mm.GET("/ttc", func(ctx *app.Context) error {
|
||||
logger.Info("tcc is commint")
|
||||
return ctx.ToJson(map[string]interface{}{"data": "ttc"})
|
||||
})
|
||||
|
||||
mm.GET("/ttc2", func(ctx *app.Context) error {
|
||||
return ctx.ToString("hello world")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
cc := app.GroupRouter("/wapi")
|
||||
{
|
||||
cc.POST("/ppt", func(ctx *app.Context) error {
|
||||
return ctx.ToJson(map[string]interface{}{"data": "123"})
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"gitee.com/zhucheer/orange/app"
|
||||
"gitee.com/zhucheer/orange/outapp/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
router := &http.Route{}
|
||||
app.AppStart(router)
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
[app]
|
||||
name = "orange"
|
||||
key = "be5356716b937d94eae948f102a2074f"
|
||||
httpAddr = "127.0.0.1"
|
||||
httpPort = 8088
|
||||
maxBody = 2096157
|
||||
csrfVerify = true
|
||||
[app.logger]
|
||||
level = "INFO"
|
||||
type = "text"
|
||||
path = ""
|
||||
syncInterval = 2
|
||||
[app.session]
|
||||
isOpen = true
|
||||
timeout = 1800
|
||||
[database]
|
||||
initCap = 2
|
||||
maxCap = 5
|
||||
idleTimeout = 5
|
||||
debug = true
|
||||
[database.mysql]
|
||||
[database.mysql.default]
|
||||
addr = "192.168.137.100:3306"
|
||||
username = "zhuqi"
|
||||
password = "123456"
|
||||
dbname = "weixin"
|
||||
[database.redis]
|
||||
[database.redis.default]
|
||||
addr = "192.168.137.100:6379"
|
||||
password = ""
|
||||
dbnum = 5
|
||||
|
|
@ -1,70 +1,19 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"gitee.com/zhucheer/orange/app"
|
||||
"gitee.com/zhucheer/orange/database"
|
||||
)
|
||||
|
||||
func Test(c *app.Context) error {
|
||||
req := struct {
|
||||
Ct string `json:"ct",form:"ct"`
|
||||
Age int `json:"age",form:"ct"`
|
||||
}{}
|
||||
|
||||
c.ParseForm(&req)
|
||||
|
||||
fmt.Println(req)
|
||||
tt := c.Session().Get("sss")
|
||||
func Welcome(c *app.Context) error {
|
||||
|
||||
return c.ToJson(map[string]interface{}{
|
||||
"tt": tt,
|
||||
"tt": "",
|
||||
})
|
||||
}
|
||||
|
||||
func Test2(c *app.Context) error {
|
||||
//database.RedisDo()
|
||||
|
||||
info := DoDB()
|
||||
return c.ToJson(map[string]interface{}{
|
||||
"tt": "222",
|
||||
"csrf-token": c.CsrfToken,
|
||||
"cate": info.Name,
|
||||
})
|
||||
}
|
||||
|
||||
func RedisTT(c *app.Context) error {
|
||||
|
||||
redisxxx, err := database.GetRedis("default")
|
||||
redis22, err := database.GetRedis("dxx")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return errors.New("xxxxxxx")
|
||||
}
|
||||
|
||||
redisxxx.Do("SET", "zhutttt", "1212211")
|
||||
|
||||
redis22.Do("SET", "zhutttteee", "1212211")
|
||||
func AuthCheck(c *app.Context) error {
|
||||
|
||||
return c.ToJson(map[string]interface{}{
|
||||
"tt": "redis",
|
||||
"auth": "auth is ok",
|
||||
})
|
||||
}
|
||||
|
||||
type ImgCate struct {
|
||||
ID uint `gorm:"primary_key"`
|
||||
Name string
|
||||
} // 默认表名是`users`
|
||||
func DoDB() *ImgCate {
|
||||
db, err := database.GetMysql("default")
|
||||
if err != nil {
|
||||
fmt.Println("db connect error", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
info := &ImgCate{}
|
||||
db.Table("qi_imgcate").Where("short = ?", "wzry").First(&info)
|
||||
|
||||
return info
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gitee.com/zhucheer/orange/app"
|
||||
)
|
||||
|
||||
type Auth struct {
|
||||
}
|
||||
|
||||
func NewAuth() *Auth {
|
||||
return &Auth{}
|
||||
}
|
||||
|
||||
// Func implements Middleware interface.
|
||||
func (w Auth) Func() app.MiddlewareFunc {
|
||||
return func(next app.HandlerFunc) app.HandlerFunc {
|
||||
return func(c *app.Context) error {
|
||||
|
||||
// 中间件处理逻辑
|
||||
if c.Request().Header.Get("auth") == "" {
|
||||
c.ResponseWrite([]byte("auth middleware break"))
|
||||
return errors.New("auth middleware break")
|
||||
}
|
||||
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"gitee.com/zhucheer/orange/app"
|
||||
"gitee.com/zhucheer/orange/outapp/controller"
|
||||
"gitee.com/zhucheer/orange/outapp/http/middleware"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
}
|
||||
|
||||
func (s *Route) ServeMux() {
|
||||
commonGp := app.GroupRouter("")
|
||||
{
|
||||
commonGp.GET("/", func(ctx *app.Context) error {
|
||||
return ctx.ToJson(map[string]interface{}{"data": "orange framework"})
|
||||
})
|
||||
}
|
||||
|
||||
apiGp := app.GroupRouter("/auth", middleware.NewAuth())
|
||||
{
|
||||
apiGp.ALL("/info", controller.AuthCheck)
|
||||
}
|
||||
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
package outapp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.com/zhucheer/orange/app"
|
||||
)
|
||||
|
||||
type Auth struct {
|
||||
}
|
||||
|
||||
// Func implements Middleware interface.
|
||||
func (w Auth) Func() app.MiddlewareFunc {
|
||||
return func(next app.HandlerFunc) app.HandlerFunc {
|
||||
|
||||
return func(c *app.Context) error {
|
||||
fmt.Println("return HandlerFunc111111111=========>")
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewAuth() *Auth {
|
||||
|
||||
return &Auth{}
|
||||
}
|
||||
|
||||
type Auth2 struct {
|
||||
}
|
||||
|
||||
// Func implements Middleware interface.
|
||||
func (w Auth2) Func() app.MiddlewareFunc {
|
||||
return func(next app.HandlerFunc) app.HandlerFunc {
|
||||
return func(c *app.Context) error {
|
||||
fmt.Println("return HandlerFunc Auth222=========>")
|
||||
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewAuth2() *Auth2 {
|
||||
|
||||
fmt.Println("auth222 middleware")
|
||||
|
||||
return &Auth2{}
|
||||
}
|
|
@ -135,6 +135,7 @@ func (pder *CookieProvider) SessionRead(sid string, r *http.Request) (Store, err
|
|||
return &CookieSessionStore{sid: sid, values: maps}, errors.New("cookie is empty")
|
||||
}
|
||||
cookieValue, errs := url.QueryUnescape(cookie.Value)
|
||||
|
||||
if errs != nil {
|
||||
maps = make(map[interface{}]interface{})
|
||||
return &CookieSessionStore{sid: sid, values: maps}, errs
|
||||
|
@ -145,6 +146,11 @@ func (pder *CookieProvider) SessionRead(sid string, r *http.Request) (Store, err
|
|||
pder.config.SecurityName,
|
||||
cookieValue, pder.maxlifetime)
|
||||
|
||||
if errs != nil {
|
||||
maps = make(map[interface{}]interface{})
|
||||
return &CookieSessionStore{sid: sid, values: maps}, errs
|
||||
}
|
||||
|
||||
if maps == nil {
|
||||
maps = make(map[interface{}]interface{})
|
||||
}
|
||||
|
|
|
@ -59,6 +59,7 @@ func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) {
|
|||
|
||||
// DecodeGob decode data to map
|
||||
func DecodeGob(encoded []byte) (map[interface{}]interface{}, error) {
|
||||
|
||||
buf := bytes.NewBuffer(encoded)
|
||||
dec := gob.NewDecoder(buf)
|
||||
var out map[interface{}]interface{}
|
||||
|
|
Loading…
Reference in New Issue