This commit is contained in:
hoteas 2019-05-19 15:33:01 +00:00
parent 029325df54
commit a3d3155e9b
2 changed files with 41 additions and 43 deletions

View File

@ -4,15 +4,15 @@ import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"net/url"
)
type Application struct {
@ -39,44 +39,40 @@ func (this *Application) ServeHTTP(w http.ResponseWriter, req *http.Request) {
//启动实例
func (this *Application) Run(router Router) {
this.Router = router
//重新设置MethodRouter//直达路由
this.MethodRouter=MethodRouter{}
modeRouterStrict:=true
if this.Config.Get("modeRouterStrict").(bool)==false{
modeRouterStrict=false
this.MethodRouter = MethodRouter{}
modeRouterStrict := true
if this.Config.Get("modeRouterStrict").(bool) == false {
modeRouterStrict = false
}
if router!=nil{
for pk,pv:= range router{
if !modeRouterStrict{
pk=strings.ToLower(pk)
if router != nil {
for pk, pv := range router {
if !modeRouterStrict {
pk = strings.ToLower(pk)
}
if pv!=nil{
for ck,cv:=range pv{
if !modeRouterStrict{
ck=strings.ToLower(ck)
if pv != nil {
for ck, cv := range pv {
if !modeRouterStrict {
ck = strings.ToLower(ck)
}
if cv!=nil{
for mk,mv:=range cv{
if !modeRouterStrict{
mk=strings.ToLower(mk)
}
this.MethodRouter["/"+pk+"/"+ck+"/"+mk]=mv
if cv != nil {
for mk, mv := range cv {
if !modeRouterStrict {
mk = strings.ToLower(mk)
}
this.MethodRouter["/"+pk+"/"+ck+"/"+mk] = mv
}
}
}
}
}
}
//this.Port = port
this.Port = this.Config.GetString("port")
if this.connectDbFunc != nil && (this.Db.DB==nil||this.Db.DB.Ping() != nil) {
if this.connectDbFunc != nil && (this.Db.DB == nil || this.Db.DB.Ping() != nil) {
this.Db.SetConnect(this.connectDbFunc)
}
@ -168,8 +164,8 @@ func (this *Application) SetConfig(configPath ...string) {
cmap.JsonToMap(string(btes), &this.Error)
for k, v := range cmap {
this.Config.Put(k, v)//程序配置
Config.Put(k,v)//系统配置
this.Config.Put(k, v) //程序配置
Config.Put(k, v) //系统配置
}
}
//else {
@ -239,7 +235,6 @@ func (this *Application) urlSer(url string) (string, []string) {
//访问
func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
o, s := this.urlSer(req.RequestURI)
@ -266,9 +261,9 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
sessionId = cookie.Value
}
unescapeUrl,err:=url.QueryUnescape(req.RequestURI)
if err!=nil{
unescapeUrl=req.RequestURI
unescapeUrl, err := url.QueryUnescape(req.RequestURI)
if err != nil {
unescapeUrl = req.RequestURI
}
//访问实例
context := Context{SessionIns: SessionIns{SessionId: sessionId,
@ -284,7 +279,6 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
//url去掉参数并序列化
context.HandlerStr, context.RouterString = this.urlSer(context.HandlerStr)
//访问拦截true继续false暂停
connectListenerLen := len(this.connectListener)
if connectListenerLen != 0 {
@ -311,17 +305,17 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
//
//}
//验证接口严格模式
modeRouterStrict:=this.Config.Get("modeRouterStrict").(bool)
tempHandlerStr:=context.HandlerStr
if !modeRouterStrict{
tempHandlerStr=strings.ToLower(tempHandlerStr)
modeRouterStrict := this.Config.Get("modeRouterStrict").(bool)
tempHandlerStr := context.HandlerStr
if !modeRouterStrict {
tempHandlerStr = strings.ToLower(tempHandlerStr)
}
//执行接口
if this.MethodRouter[tempHandlerStr]!=nil{
this.MethodRouter[tempHandlerStr](&context)
context.View()
return
if this.MethodRouter[tempHandlerStr] != nil {
this.MethodRouter[tempHandlerStr](&context)
context.View()
return
}
//url赋值
@ -352,11 +346,15 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
}
//设置header
delete(header,"Content-Type")
if this.Config.GetInt("debug")!=1{
delete(header, "Content-Type")
if this.Config.GetInt("debug") != 1 {
header.Set("Cache-Control", "public")
}
if strings.Index(path, ".m3u8") != -1 {
header.Add("Content-Type", "audio/mpegurl")
}
//w.Write(data)
http.ServeFile(w, req, path)

View File

@ -1,4 +1,3 @@
package hotime
var MimeMaps = map[string]string{
@ -12,6 +11,7 @@ var MimeMaps = map[string]string{
".abc": "text/vndabc",
".ace": "application/x-ace-compressed",
".acgi": "text/html",
".m3u8": "audio/mpegurl",
".afl": "video/animaflex",
".ai": "application/postscript",
".aif": "audio/aiff",