Skip to content

Commit

Permalink
#### Version 1.0.0
Browse files Browse the repository at this point in the history
* dotweb正式进入1.0时代,感谢大家!
* HttpServer新增IndexPage(),返回当前Server默认首页名称,支持配置文件配置,如果未配置,自动读取默认值:DefaultIndexPage = "index.html"
* Context增加File、Attachment、Inline接口
* Context.File(file string),将指定文件响应到客户端,如果指定为目录,则根据HttpServer.IndexPage,默认寻找该目录下同名文件,不存在则返回404
* Context.Attachment(file, name string),将指定文件响应到客户端下载,具体参考MIME协议扩展"Content-disposition"说明
* Context.Inline(file, name string),将指定文件响应到客户端输出,具体参考MIME协议扩展"Content-disposition"说明
* 新增example\file目录,提供对Context.File、Attachment、Inline三个接口的demo
* 2017-07-29 17:00
  • Loading branch information
devfeel committed Jul 29, 2017
1 parent 01851b5 commit 5fcfeb4
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 12 deletions.
21 changes: 11 additions & 10 deletions config/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ type (
}

ServerNode struct {
EnabledListDir bool `xml:"enabledlistdir,attr"` //设置是否启用目录浏览,仅对Router.ServerFile有效,若设置该项,则可以浏览目录文件,默认不开启
EnabledGzip bool `xml:"enabledgzip,attr"` //是否启用gzip
EnabledAutoHEAD bool `xml:"enabledautohead,attr"` //设置是否自动启用Head路由,若设置该项,则会为除Websocket\HEAD外所有路由方式默认添加HEAD路由,默认不开启
EnabledAutoCORS bool `xml:"enabledautocors,attr"` //设置是否自动跨域支持,若设置,默认“GET, POST, PUT, DELETE, OPTIONS”全部请求均支持跨域
Port int `xml:"port,attr"` //端口
EnabledListDir bool `xml:"enabledlistdir,attr"` //设置是否启用目录浏览,仅对Router.ServerFile有效,若设置该项,则可以浏览目录文件,默认不开启
EnabledGzip bool `xml:"enabledgzip,attr"` //是否启用gzip
EnabledAutoHEAD bool `xml:"enabledautohead,attr"` //设置是否自动启用Head路由,若设置该项,则会为除Websocket\HEAD外所有路由方式默认添加HEAD路由,默认不开启
EnabledAutoCORS bool `xml:"enabledautocors,attr"` //设置是否自动跨域支持,若设置,默认“GET, POST, PUT, DELETE, OPTIONS”全部请求均支持跨域
Port int `xml:"port,attr"` //端口
IndexPage string `xml:"indexpage,attr"` //默认index页面
}

SessionNode struct {
Expand Down Expand Up @@ -149,9 +150,9 @@ func InitConfig(configFile string, confType ...interface{}) (config *Config, err
}

if cType == ConfigType_Xml {
config, err = initConfig(realFile,cType,fromXml)
config, err = initConfig(realFile, cType, fromXml)
} else {
config, err = initConfig(realFile,cType,fromJson)
config, err = initConfig(realFile, cType, fromJson)
}

if err != nil {
Expand Down Expand Up @@ -190,16 +191,16 @@ func dealConfigDefaultSet(c *Config) {

}

func initConfig(configFile string,ctType string,f func([]byte,interface{}) error) (*Config, error) {
func initConfig(configFile string, ctType string, f func([]byte, interface{}) error) (*Config, error) {
content, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, errors.New("DotWeb:Config:initConfig 当前cType:"+ctType+" 配置文件[" + configFile + "]无法解析 - " + err.Error())
return nil, errors.New("DotWeb:Config:initConfig 当前cType:" + ctType + " 配置文件[" + configFile + "]无法解析 - " + err.Error())
}

var config *Config
err = f(content, &config)
if err != nil {
return nil, errors.New("DotWeb:Config:initConfig 当前cType:"+ctType+" 配置文件[" + configFile + "]解析失败 - " + err.Error())
return nil, errors.New("DotWeb:Config:initConfig 当前cType:" + ctType + " 配置文件[" + configFile + "]解析失败 - " + err.Error())
}
return config, nil
}
50 changes: 50 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/devfeel/dotweb/cache"
"github.com/devfeel/dotweb/core"
"github.com/devfeel/dotweb/session"
"os"
"path/filepath"
"time"
)

Expand Down Expand Up @@ -48,6 +50,9 @@ type (
QueryString(key string) string
FormValue(key string) string
PostFormValue(key string) string
File(file string) (err error)
Attachment(file string, name string) error
Inline(file string, name string) error
Bind(i interface{}) error
GetRouterName(key string) string
RemoteIP() string
Expand Down Expand Up @@ -307,6 +312,51 @@ func (ctx *HttpContext) PostFormValue(key string) string {
return ctx.request.PostFormValue(key)
}

// File sends a response with the content of the file
// if file not exists, response 404
func (ctx *HttpContext) File(file string) (err error) {
f, err := os.Open(file)
if err != nil {
HTTPNotFound(ctx)
return nil
}
defer f.Close()

fi, _ := f.Stat()
if fi.IsDir() {
file = filepath.Join(file, ctx.HttpServer().IndexPage())
f, err = os.Open(file)
if err != nil {
HTTPNotFound(ctx)
return nil
}
defer f.Close()
if fi, err = f.Stat(); err != nil {
return err
}
}
http.ServeContent(ctx.Response().Writer(), ctx.Request().Request, fi.Name(), fi.ModTime(), f)
return nil
}

// Attachment sends a response as attachment, prompting client to save the file.
func (ctx *HttpContext) Attachment(file, name string) (err error) {
return ctx.contentDisposition(file, name, "attachment")
}

// Inline sends a response as inline, opening the file in the browser.
// if file not exists, response 404
func (ctx *HttpContext) Inline(file, name string) (err error) {
return ctx.contentDisposition(file, name, "inline")
}

// contentDisposition set Content-disposition and response file
func (ctx *HttpContext) contentDisposition(file, name, dispositionType string) (err error) {
ctx.Response().SetHeader(HeaderContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, name))
ctx.File(file)
return
}

/*
* 支持Json、Xml、Form提交的属性绑定
*/
Expand Down
6 changes: 6 additions & 0 deletions dotweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func (app *DotWeb) MustStart() {
}

// ListenAndServe start server with addr
// not support pprof server auto start
func (app *DotWeb) ListenAndServe(addr string) error {
initInnerRouter(app.HttpServer)

Expand Down Expand Up @@ -393,6 +394,11 @@ func (ds *DotWeb) Shutdown(ctx context.Context) error {
return ds.HttpServer.stdServer.Shutdown(ctx)
}

// HTTPNotFound simple notfound function for Context
func HTTPNotFound(ctx Context) {
http.NotFound(ctx.Response().Writer(), ctx.Request().Request)
}

// init inner routers
func initInnerRouter(server *HttpServer) {
//默认支持pprof信息查看
Expand Down
2 changes: 1 addition & 1 deletion example/config/dotweb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<app logpath="d:/gotmp/" enabledlog="true" runmode="development"/>
<offline offline="false" offlinetext="server is offline!" offlineurl="" />
<server isrun="true" port="8080" enabledgzip="false" enabledlistdir="false" enabledautohead="true" requesttimeout="30000/>
<server isrun="true" indexpage="index.html" port="8080" enabledgzip="false" enabledlistdir="false" enabledautohead="true" requesttimeout="30000/>
<session enabled="true" mode="runtime" timeout="20"/>
<appset>
<set key="set1" value="1" />
Expand Down
2 changes: 1 addition & 1 deletion example/session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
//runtime mode
//app.HttpServer.SetSessionConfig(session.NewDefaultRuntimeConfig())
//redis mode
app.HttpServer.SetSessionConfig(session.NewDefaultRedisConfig("192.168.8.175:6381"))
app.HttpServer.SetSessionConfig(session.NewDefaultRedisConfig("192.168.8.175:6371"))

//设置路由
InitRoute(app.HttpServer)
Expand Down
10 changes: 10 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
const (
DefaultGzipLevel = 9
gzipScheme = "gzip"
DefaultIndexPage = "index.html"
)

type (
Expand Down Expand Up @@ -136,6 +137,15 @@ func (server *HttpServer) SetOffline(offline bool, offlineText string, offlineUr
server.offline = offline
}

// IndexPage default index page name
func (server *HttpServer) IndexPage() string {
if server.ServerConfig.IndexPage == "" {
return DefaultIndexPage
} else {
return server.ServerConfig.IndexPage
}
}

// SetSessionConfig set session store config
func (server *HttpServer) SetSessionConfig(storeConfig *session.StoreConfig) {
//sync session config
Expand Down
11 changes: 11 additions & 0 deletions version.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
##dotweb版本记录:

#### Version 1.0.0
* dotweb正式进入1.0时代,感谢大家!
* HttpServer新增IndexPage(),返回当前Server默认首页名称,支持配置文件配置,如果未配置,自动读取默认值:DefaultIndexPage = "index.html"
* Context增加File、Attachment、Inline接口
* Context.File(file string),将指定文件响应到客户端,如果指定为目录,则根据HttpServer.IndexPage,默认寻找该目录下同名文件,不存在则返回404
* Context.Attachment(file, name string),将指定文件响应到客户端下载,具体参考MIME协议扩展"Content-disposition"说明
* Context.Inline(file, name string),将指定文件响应到客户端输出,具体参考MIME协议扩展"Content-disposition"说明
* 新增example\file目录,提供对Context.File、Attachment、Inline三个接口的demo
* 2017-07-29 17:00


#### Version 0.3.21
* 新增DotWeb & HttpServer.ListenAndServer(addr string) 支持原生host监听
* 新增DotWeb Close() & Shutdown() 支持优雅关闭服务
Expand Down

0 comments on commit 5fcfeb4

Please sign in to comment.