本文整理匯總了Golang中github.com/hoisie/web.Server類的典型用法代碼示例。如果您正苦於以下問題:Golang Server類的具體用法?Golang Server怎麽用?Golang Server使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Server類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
var server1 web.Server
var server2 web.Server
server1.Get("/(.*)", hello1)
go server1.Run("0.0.0.0:9999")
server2.Get("/(.*)", hello2)
go server2.Run("0.0.0.0:8999")
<-make(chan int)
}
示例2: ServeStatic
// Web.go handler to serve static files
// This should not be used in production
func ServeStatic(root string, svr *web.Server) {
svr.Get("/static(.*)",
func(ctx *web.Context, path string) {
fn := root + path
log.Printf("Serving static file %s", fn)
data, err := ioutil.ReadFile(fn)
if err != nil {
ctx.NotFound("File not found!")
log.Println("Could not read file!", err)
return
}
ctx.WriteString(string(data))
})
}
示例3: Init
// Initalize the app's views.
// Arguments:
// svr - http server mux to listen on
// template_root - Root directory for templates
func Init(svr *web.Server, templateRoot string) {
helpers.SetTemplateRoot(templateRoot)
svr.Get("/todo", helpers.TemplateLayoutView(
"/todo.html.mustache",
"/base.html.mustache",
func() interface{} {
list := model.App.List.GetAll()
bootstrap, err := json.Marshal(list)
if err != nil {
log.Printf("ERROR: Parsing GetAll():", err, list)
return nil
}
return map[string]string{
"title": "Todo App",
"list": string(bootstrap),
}
}))
}
示例4: Init
func (c *Controller) Init(conf *Config, s *web.Server) {
logFile, err := os.Create("coconut.log")
if err != nil {
log.Fatalln("Error creating log file:", err)
}
c.log = log.New(logFile, "[coconut]", log.LstdFlags|log.Lshortfile)
s.Get("/", c.Front)
s.Get("/login", c.Login)
s.Get("/publish", c.Publish)
s.Post("/login", c.LoginPost)
s.Post("/publish", c.PublishPost)
for url, filename := range conf.Pages {
s.Get(url, c.makePageFunc(filename))
}
s.Get("/tag/(.*)", c.Tag)
s.Get("/(.*)", c.Article)
}
示例5: BindController
func BindController(svr *web.Server, path string, ctrl Controller) {
// Create
svr.Post(path, func(wctx *web.Context) {
ctx := NewContext(wctx)
body, err := ctx.readBody()
if err != nil {
ctx.writeError(err)
return
}
log.Println("body: ", body)
id := <-RandStr
ret, err := ctrl.Create(id, body)
if err != nil {
ctx.writeError(err)
return
}
ctx.writeJson(ret)
})
// Read
svr.Get(path+"/?(.*)", func(wctx *web.Context, id string) {
ctx := NewContext(wctx)
ret, err := ctrl.Read(id)
if err != nil {
ctx.writeError(err)
return
}
ctx.writeJson(ret)
})
// Update
svr.Put(path+"/(.+)", func(wctx *web.Context, id string) {
ctx := NewContext(wctx)
body, err := ctx.readBody()
if err != nil {
ctx.writeError(err)
return
}
log.Println("body: ", body)
ret, err := ctrl.Update(id, body)
if err != nil {
ctx.writeError(err)
return
}
// Accept nil responses
if ret != nil {
ctx.writeJson(ret)
} else {
ctx.NotModified()
}
})
// Delete
svr.Delete(path+"/(.+)", func(wctx *web.Context, id string) {
ctx := NewContext(wctx)
err := ctrl.Delete(id)
if err != nil {
ctx.writeError(err)
return
}
ctx.NotModified()
})
}
示例6: Start
func Start(state interfaces.IState) {
var server *web.Server
if Servers == nil {
Servers = make(map[int]*web.Server)
}
if Servers[state.GetPort()] == nil {
server = web.NewServer()
Servers[state.GetPort()] = server
server.Env["state"] = state
server.Post("/v1/factoid-submit/?", HandleFactoidSubmit)
server.Post("/v1/commit-chain/?", HandleCommitChain)
server.Post("/v1/reveal-chain/?", HandleRevealChain)
server.Post("/v1/commit-entry/?", HandleCommitEntry)
server.Post("/v1/reveal-entry/?", HandleRevealEntry)
server.Get("/v1/directory-block-head/?", HandleDirectoryBlockHead)
server.Get("/v1/get-raw-data/([^/]+)", HandleGetRaw)
server.Get("/v1/directory-block-by-keymr/([^/]+)", HandleDirectoryBlock)
server.Get("/v1/entry-block-by-keymr/([^/]+)", HandleEntryBlock)
server.Get("/v1/entry-by-hash/([^/]+)", HandleEntry)
server.Get("/v1/chain-head/([^/]+)", HandleChainHead)
server.Get("/v1/entry-credit-balance/([^/]+)", HandleEntryCreditBalance)
server.Get("/v1/factoid-balance/([^/]+)", HandleFactoidBalance)
server.Get("/v1/factoid-get-fee/", HandleGetFee)
log.Print("Starting server")
go server.Run(fmt.Sprintf("localhost:%d", state.GetPort()))
}
}