Exemple #1
0
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)
}
Exemple #2
0
// 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))
		})
}
Exemple #3
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)
}
Exemple #4
0
// 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),
			}
		}))
}
Exemple #5
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()
	})
}
Exemple #6
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()))
	}

}