加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.go 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
jimmykuu 提交于 2018-12-22 15:01 . 去掉下载模块
/*
读取配置文件,设置URL,启动服务器
*/
package gopher
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
var (
logger = log.New(os.Stdout, "[gopher]:", log.LstdFlags)
)
func handlerFun(route Route) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
/*
defer func() {
if e := recover(); e != nil {
fmt.Println("panic:", e)
}
}()*/
handler := NewHandler(w, r)
defer handler.Session.Close()
url := r.Method + " " + r.URL.Path
if r.URL.RawQuery != "" {
url += "?" + r.URL.RawQuery
}
logger.Println(url)
if route.Permission&Everyone == Everyone {
route.HandlerFunc(handler)
}
var (
user *User
ok bool
)
if route.Permission&Authenticated == Authenticated {
user, ok = currentUser(handler)
if !ok {
http.Redirect(w, r, "/signin", http.StatusFound)
return
}
if route.Permission&AdministratorOnly == AdministratorOnly {
if !user.IsSuperuser {
message(handler, "没有权限", "对不起,你没有权限进行该操作", "error")
return
}
}
route.HandlerFunc(handler)
}
}
}
func StartServer() {
//http.Handle("/static/", http.FileServer(http.Dir(".")))
r := mux.NewRouter()
for _, route := range routes {
r.HandleFunc(route.URL, handlerFun(route))
}
r.PathPrefix("/static/").HandlerFunc(fileHandler)
http.Handle("/", r)
logger.Println("Server start on:", Config.Port)
// http server
// err := http.ListenAndServeTLS(fmt.Sprintf(":%d", Config.Port), "cert.pem", "key.pem", nil)
err := http.ListenAndServe(fmt.Sprintf(":%d", Config.Port), nil)
if err != nil {
logger.Fatal(err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化