File size: 4,651 Bytes
530729e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2019 GoAdmin Core Team. All rights reserved.
// Use of this source code is governed by a Apache-2.0 style
// license that can be found in the LICENSE file.

package iris

import (
	"bytes"
	"errors"
	"net/http"
	"net/url"
	"strings"

	"github.com/GoAdminGroup/go-admin/adapter"
	"github.com/GoAdminGroup/go-admin/plugins/admin/models"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/kataras/iris/v12"

	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/engine"
	"github.com/GoAdminGroup/go-admin/modules/config"
	"github.com/GoAdminGroup/go-admin/plugins"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/constant"
)

// Iris structure value is an Iris GoAdmin adapter.
type Iris struct {
	adapter.BaseAdapter
	ctx iris.Context
	app *iris.Application
}

func init() {
	engine.Register(new(Iris))
}

// User implements the method Adapter.User.
func (is *Iris) User(ctx interface{}) (models.UserModel, bool) {
	return is.GetUser(ctx, is)
}

// Use implements the method Adapter.Use.
func (is *Iris) Use(app interface{}, plugs []plugins.Plugin) error {
	return is.GetUse(app, plugs, is)
}

// Content implements the method Adapter.Content.
func (is *Iris) Content(ctx interface{}, getPanelFn types.GetPanelFn, fn context.NodeProcessor, btns ...types.Button) {
	is.GetContent(ctx, getPanelFn, is, btns, fn)
}

type HandlerFunc func(ctx iris.Context) (types.Panel, error)

func Content(handler HandlerFunc) iris.Handler {
	return func(ctx iris.Context) {
		engine.Content(ctx, func(ctx interface{}) (types.Panel, error) {
			return handler(ctx.(iris.Context))
		})
	}
}

// SetApp implements the method Adapter.SetApp.
func (is *Iris) SetApp(app interface{}) error {
	var (
		eng *iris.Application
		ok  bool
	)
	if eng, ok = app.(*iris.Application); !ok {
		return errors.New("iris adapter SetApp: wrong parameter")
	}
	is.app = eng
	return nil
}

// AddHandler implements the method Adapter.AddHandler.
func (is *Iris) AddHandler(method, path string, handlers context.Handlers) {
	is.app.Handle(strings.ToUpper(method), path, func(c iris.Context) {
		ctx := context.NewContext(c.Request())

		var params = map[string]string{}
		c.Params().Visit(func(key string, value string) {
			params[key] = value
		})

		for key, value := range params {
			if c.Request().URL.RawQuery == "" {
				c.Request().URL.RawQuery += strings.ReplaceAll(key, ":", "") + "=" + value
			} else {
				c.Request().URL.RawQuery += "&" + strings.ReplaceAll(key, ":", "") + "=" + value
			}
		}

		ctx.SetHandlers(handlers).Next()
		for key, head := range ctx.Response.Header {
			c.Header(key, head[0])
		}
		c.StatusCode(ctx.Response.StatusCode)
		if ctx.Response.Body != nil {
			buf := new(bytes.Buffer)
			_, _ = buf.ReadFrom(ctx.Response.Body)
			_, _ = c.WriteString(buf.String())
		}
	})
}

// Name implements the method Adapter.Name.
func (*Iris) Name() string {
	return "iris"
}

// SetContext implements the method Adapter.SetContext.
func (*Iris) SetContext(contextInterface interface{}) adapter.WebFrameWork {
	var (
		ctx iris.Context
		ok  bool
	)
	if ctx, ok = contextInterface.(iris.Context); !ok {
		panic("iris adapter SetContext: wrong parameter")
	}

	return &Iris{ctx: ctx}
}

// Redirect implements the method Adapter.Redirect.
func (is *Iris) Redirect() {
	is.ctx.Redirect(config.Url(config.GetLoginUrl()), http.StatusFound)
}

// SetContentType implements the method Adapter.SetContentType.
func (is *Iris) SetContentType() {
	is.ctx.Header("Content-Type", is.HTMLContentType())
}

// Write implements the method Adapter.Write.
func (is *Iris) Write(body []byte) {
	_, _ = is.ctx.Write(body)
}

// GetCookie implements the method Adapter.GetCookie.
func (is *Iris) GetCookie() (string, error) {
	return is.ctx.GetCookie(is.CookieKey()), nil
}

// Lang implements the method Adapter.Lang.
func (is *Iris) Lang() string {
	return is.ctx.Request().URL.Query().Get("__ga_lang")
}

// Path implements the method Adapter.Path.
func (is *Iris) Path() string {
	return is.ctx.Path()
}

// Method implements the method Adapter.Method.
func (is *Iris) Method() string {
	return is.ctx.Method()
}

// FormParam implements the method Adapter.FormParam.
func (is *Iris) FormParam() url.Values {
	return is.ctx.FormValues()
}

// IsPjax implements the method Adapter.IsPjax.
func (is *Iris) IsPjax() bool {
	return is.ctx.GetHeader(constant.PjaxHeader) == "true"
}

// Query implements the method Adapter.Query.
func (is *Iris) Query() url.Values {
	return is.ctx.Request().URL.Query()
}

// Request implements the method Adapter.Request.
func (is *Iris) Request() *http.Request {
	return is.ctx.Request()
}