2 ZH_Static
Lunny Xiao edited this page 2015-01-13 18:06:58 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Static 让你用一行代码可以完成一个静态服务器。

func main() {
    t := tango.New(tango.Static())
    t.Run()
}

然后,将你的文件放到 ./public 目录下,你就可以通过浏览器放问到他们。比如:

http://localhost/images/logo.png  --> ./public/images/logo.png

当然,你也可以加入你basicauth或者你自己的认证中间件,这样就变为了一个私有的文件服务器。

func main() {
    t := tango.New()
    t.Use(AuthHandler)
    t.Use(tango.Static())
    t.Run()
}

你也可以通过StaticOptions来改变Static的默认参数比如

t.Use(tango.Static(tango.StaticOptions{Prefix:"static"}))