我希望从我的处理程序中提取一些重复逻辑并将其放入一些每个处理程序中间件:特别是像CSRF检查,检查现有会话值(即用于身份验证或用于预览页面)等.
我已经阅读了一些关于此的文章,但是很多例子都集中在每个服务器的中间件(包装http.Handler)上:我有一小部分需要中间件的处理程序.我的大多数其他页面都没有,因此如果我可以避免检查会话/等.对于那些要求更好.
到目前为止,我的中间件通常看起来像这样:
func checkCSRF(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// get the session, check/validate/create the token based on HTTP method, etc.
// return HTTP 403 on a failed check
// else invoke the wrapped handler h(w, r)
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在许多情况下,我想将变量传递给包装的处理程序:生成的CSRF令牌传递给模板,或者包含表单数据的结构 - 一个中间件检查会话是否存在某些已保存的表单数据用户点击一个/preview/URL,否则它会将它们重定向(因为它们没有任何内容可供预览!).
我想将该结构传递给包装的处理程序以节省必须复制session.Get/type断言/错误检查逻辑我刚刚在中间件中写道.
我可以这样写:
type CSRFHandlerFunc func(w http.ResponseWriter, r *http.Request, t string)
Run Code Online (Sandbox Code Playgroud)
...然后像这样编写中间件:
func csrfCheck(h CSRFHandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// get the session, check/validate/create …Run Code Online (Sandbox Code Playgroud) 有没有办法通过自定义状态代码在Go中通过HTTP提供静态文件(不重写大量私有代码)?
从我所看到的:
http.StatusOK标题.我想我已经知道了答案,但如果有人有替代解决方案,那就很有用了.
该用例正在服务500.html,404.html等.al文件.我通常使用nginx来捕获Go常用的简单http.Error响应并让nginx将文件提供给磁盘,但我处于一个不可选的环境中.
我不是一个数据库家伙,但我一直在讨论这个问题,似乎无法解决这个问题.我已经阅读了相关的文档页面(http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html),我无法看到我的语法可能成为问题的位置.
ERROR 1064 (42000) at line 84: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOREIGN KEY (user_id) REFERENCES user(id)
ON DELETE RESTRICT ON UPDATE C' at line 5
Run Code Online (Sandbox Code Playgroud)
这是相关的SQL - order(id)和user(id)是各自表中的自动递增int(10)字段.
DROP TABLE IF EXISTS `user_orders`;
CREATE TABLE `user_orders` (
`user_id` int(10) unsigned NOT NULL default '0',
`order_id` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`user_orders_user_id`, `user_orders_order_id`)
FOREIGN KEY (user_id) REFERENCES …Run Code Online (Sandbox Code Playgroud) 在Oracle 9i中声明FK时出现问题.我已经在SO和一些在线文档(例如http://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php)中查看了一些示例,没有任何真正的运气; 尝试与链接中的语法类似的语法会生成相同的错误:
Error at Command Line:19 Column:4
Error report:
SQL Error: ORA-02253: constraint specification not allowed here
02253. 00000 - "constraint specification not allowed here"
*Cause: Constraint specification is not allowed here in the statement.
*Action: Remove the constraint specification from the statement.
Run Code Online (Sandbox Code Playgroud)
SQL本身的摘录如下."第19行"是指以第一行开头的行CONSTRAINT
CREATE TABLE Flight (
flight_no varchar2(10) NOT NULL,
airplane_id varchar2(20) NOT NULL
CONSTRAINT flight_airplane_id_fk FOREIGN KEY (airplane_id) REFERENCES Airplane (airplane_id)
ON UPDATE RESTRICT ON DELETE RESTRICT,
dept_date date NOT NULL,
...
Run Code Online (Sandbox Code Playgroud)
或者,在没有 …
我目前在Heroku的Cedar堆栈上运行Octopress(基于Jekyll)站点 - 代码存在于此:https://github.com/elithrar/octopress
我想Cache-Control根据文件类型有选择地应用标头:
.html files的值为 public, max-age=3600.css|.js|.png|.ico(等)获取值public, max-age=604800- 或者,我想将此规则应用于从/stylesheets', '/javascripts', '/imgs'目录提供的任何内容.既没有运气又使用set :static_cache_control , [:public, :max_age => 3600]了香草语cache_control :public, :max_age => 3600.
我已设法自己设置public, max-age=3600文章(例如/2012/lazy-sundays/),但无法将标题应用于CSS/JS(例如/stylesheets/screen.css)
我config.ru目前看起来像这样(更新):
require 'bundler/setup'
require 'sinatra/base'
# The project root directory
$root = ::File.dirname(__FILE__)
class SinatraStaticServer < Sinatra::Base
get(/.+/) do
cache_control :public, :max_age => 7200
send_sinatra_file(request.path) {404}
end
not_found do
send_sinatra_file('404.html') …Run Code Online (Sandbox Code Playgroud) 我正在使用gorilla/schema来解压缩r.PostForm到一个结构中.
我的问题是,我试图找出一种"明智的"方式来获取<select>元素的选定值,使我能够轻松地使用html/template来重新选择字段(即从会话中重新填充表单时) )注意到没有一种简单的方法来测试相等性,只是通过传递结构的实例RenderTemplate.
为了说明我的所作所为:
type Listing struct {
Id string `schema:"-"`
Title string `schema:"title"`
Company string `schema:"company"`
Location string `schema:"location"`
...
Term string `schema:"term"`
}
Run Code Online (Sandbox Code Playgroud)
if r.Method == "POST" {
// error handling code removed for brevity, but trust me, it exists!
err = r.ParseForm()
err = decoder.Decode(listing, r.PostForm)
err = listing.Validate() // checks field lengths as I'm using a schema-less datastore
Run Code Online (Sandbox Code Playgroud)
<label for="term">Term</label>
<select data-placeholder="Term...?" id="term" name="term" …Run Code Online (Sandbox Code Playgroud)