當前位置: 首頁>>代碼示例>>Golang>>正文


Golang web.Server類代碼示例

本文整理匯總了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)
}
開發者ID:hoisie,項目名稱:web,代碼行數:10,代碼來源:multiserver.go

示例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))
		})
}
開發者ID:yanatan16,項目名稱:go-todo-app,代碼行數:17,代碼來源:static.go

示例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),
			}
		}))
}
開發者ID:yanatan16,項目名稱:go-todo-app,代碼行數:24,代碼來源:view.go

示例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)
}
開發者ID:JanSichula,項目名稱:coconut,代碼行數:17,代碼來源:controller.go

示例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()
	})
}
開發者ID:yanatan16,項目名稱:go-todo-app,代碼行數:71,代碼來源:controller.go

示例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()))
	}

}
開發者ID:jjdevbiz,項目名稱:factomd,代碼行數:32,代碼來源:wsapi.go


注:本文中的github.com/hoisie/web.Server類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。