Esempio n. 1
0
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)
}
Esempio n. 2
0
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()))
	}

}
Esempio n. 3
0
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()
	})
}