2 ZH_Static
Lunny Xiao edited this page 8 years ago
This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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"}))