当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 0

Simbory / mego
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mego.go 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
Simbory 提交于 2020-02-09 23:51 . 添加gzip压缩
package mego
import (
"errors"
"log"
"net/http"
"path"
)
type HandlerFunc func(http.ResponseWriter, *http.Request) interface{}
// UseStarter attach an starter to the server
func (s *Server) UseStarter(starter Starter) {
if starter == nil {
panic(errors.New("starter is nil"))
}
s.starters = append(s.starters, starter)
}
// MapRootPath Returns the physical file path that corresponds to the specified virtual path.
// @param virtualPath: the virtual path starts with
// @return the absolute file path
func (s *Server) MapRootPath(virtualPath string) string {
if len(virtualPath) == 0 {
return s.webRoot
}
return ClearFilePath(path.Join(s.webRoot, virtualPath))
}
// MapContentPath Returns the physical file path that corresponds to the specified virtual path.
// @param virtualPath: the virtual path starts with
// @return the absolute file path
func (s *Server) MapContentPath(virtualPath string) string {
if len(virtualPath) == 0 {
return s.contentRoot
}
return ClearFilePath(path.Join(s.contentRoot, virtualPath))
}
// Run run the application as http
func (s *Server) Run() {
s.init()
log.Printf("INFO: the server is listening on address %s\r\n", s.addr)
err := http.ListenAndServe(s.addr, s)
panicErr(err)
}
// RunTLS run the application as https
func (s *Server) RunTLS(certFile, keyFile string) {
s.init()
log.Printf("INFO: the server is listening on address %s, TLS\r\n", s.addr)
err := http.ListenAndServeTLS(s.addr, certFile, keyFile, s)
panicErr(err)
}
// NewServer create a new server
//
// webRoot: the root of this web server. and the content root is '${webRoot}/www'
//
// addr: the address the server is listen on
func NewServer(webRoot, addr string) *Server {
if len(webRoot) == 0 {
webRoot = GetExeDir()
}
webRoot = path.Clean(ClearFilePath(webRoot))
if len(addr) == 0 {
addr = ":8888"
}
var s = &Server{
webRoot: webRoot,
contentRoot: webRoot + "/www",
addr: addr,
routing: newRouteTree(),
}
return s
}
Go
1
https://gitee.com/simbory/mego.git
git@gitee.com:simbory/mego.git
simbory
mego
mego
v1.1.1

搜索帮助