示例#1
0
func SetRouters(m *macaron.Macaron) {
	m.Group("/v1", func() {
		//Session Router
		m.Group("/token", func() {
			m.Post("/", handler.W1PostToken)
		})

		//User Router
		m.Group("/user", func() {
			//Signin and Signup
			m.Post("/", binding.Bind(handler.UserSignup{}), handler.W1UserSignup)
			m.Post("/auth", handler.W1UserSignin)
			//List All Users
			m.Get("/list/:count/:page", handler.W1GetUserList)
			//Profile
			m.Put("/:user/profile", handler.W1PutUserProfile)
			m.Get("/:user/profile", handler.W1GetUserProfile)
			m.Post("/:user/gravatar", handler.W1PostUserGravatar)
			//Put Password
			m.Put("/:user/passwd", handler.W1PutUserPasswd)
			//List User Teams and Organizations
			m.Get("/:user/organizations", handler.W1GetUserOrganizations)
			m.Get("/:user/teams", handler.W1GetUserTeams)
		})

		//Organization Router
		m.Group("/org", func() {
			m.Post("/", handler.W1PostOrganization)
			m.Put("/:org", handler.W1PutOrganization)
			m.Get("/:org", handler.W1GetOrganization)
			m.Delete("/:org", handler.W1DeleteOrganization)

			//Team Router
			m.Group("/:org/team", func() {
				m.Post("/", handler.W1PostTeam)
				m.Get("/list", handler.W1GetTeams)
				m.Put("/:team", handler.W1PutTeam)
				m.Get("/:team", handler.W1GetTeam)
				m.Delete("/:team", handler.W1DeleteTeam)

				//User Management
				m.Group("/:team/user", func() {
					m.Get("/list", handler.W1GetTeamUsers)
					m.Put("/:user", handler.W1PutTeamUser)
					m.Delete("/:user", handler.W1DeleteTeamUser)
				})
			})
		})
	})
}
func main() {
	initParse()

	err := readConfig("service.conf", &c.Config)
	if err != nil {
		glog.V(1).Infof("Read Configure file fail:%#v", err)
		return
	}

	initDB(c.Config)

	m := macaron.Classic()
	macaron.Env = macaron.PROD
	m.Use(macaron.Renderer())
	m.Use(session.Sessioner(session.Options{
		Provider:       "redis",
		ProviderConfig: "addr=127.0.0.1:6379",
	}))

	m.Use(macaron.Static("public",
		macaron.StaticOptions{
			// 请求静态资源时的 URL 前缀,默认没有前缀
			Prefix: "public",
			// 禁止记录静态资源路由日志,默认为不禁止记录
			SkipLogging: true,
			// 当请求目录时的默认索引文件,默认为 "index.html"
			IndexFile: "index.html",
			// 用于返回自定义过期响应头,不能为不设置
			// https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
			Expires: func() string {
				return time.Now().Add(24 * 60 * time.Minute).UTC().Format("Mon, 02 Jan 2006 15:04:05 GMT")
			},
		}))

	m.Use(macaron.Renderer(macaron.RenderOptions{
		// 模板文件目录,默认为 "templates"
		Directory: "templates",
		// 模板文件后缀,默认为 [".tmpl", ".html"]
		Extensions: []string{".tmpl", ".html"},
		// 模板函数,默认为 []
		Funcs: []template.FuncMap{map[string]interface{}{
			"AppName": func() string {
				return "Macaron"
			},
			"AppVer": func() string {
				return "1.0.0"
			},
		}},
		// 模板语法分隔符,默认为 ["{{", "}}"]
		Delims: macaron.Delims{"{{", "}}"},
		// 追加的 Content-Type 头信息,默认为 "UTF-8"
		Charset: "UTF-8",
		// 渲染具有缩进格式的 JSON,默认为不缩进
		IndentJSON: true,
		// 渲染具有缩进格式的 XML,默认为不缩进
		IndentXML: true,
		// 渲染具有前缀的 JSON,默认为无前缀
		PrefixJSON: []byte(""),
		// 渲染具有前缀的 XML,默认为无前缀
		PrefixXML: []byte(""),
		// 允许输出格式为 XHTML 而不是 HTML,默认为 "text/html"
		HTMLContentType: "text/html",
	}))
	m.Map(x)
	m.SetDefaultCookieSecret("UYCNJSHA123JCN409")
	//	m.Post("/login",controller.Login)
	//	m.Get("/logout",controller.Logout)

	m.Group("/users", func() {
		m.Post("", binding.Bind(Customer{}), controller.PostCustomer)
		m.Get("", controller.GetCustomers)
	})

	m.Group("/goods", func() {
		m.Post("", binding.Bind(Goods{}), controller.PostGood)
		m.Get("", controller.GetGoods)
	})

	m.Group("/orders", func() {
		m.Post("/alipay", binding.Bind(Order{}), controller.PostOrder)
		m.Get("", controller.GetOrders)
	})

	m.Group("/alipay", func() {
		m.Post("/notify", controller.AlipayNotify)
		m.Get("/return", controller.AlipayReturn)
	})

	m.Run(8080)
}
示例#3
0
func main() {
	// initiate the app
	m := macaron.Classic()

	// register middleware
	m.Use(macaron.Recovery())
	m.Use(macaron.Gziper())
	m.Use(macaron.Static("public",
		macaron.StaticOptions{
			// Prefix is the optional prefix used to serve the static directory content. Default is empty string.
			Prefix: "public",
			// SkipLogging will disable [Static] log messages when a static file is served. Default is false.
			SkipLogging: true,
			// IndexFile defines which file to serve as index if it exists. Default is "index.html".
			IndexFile: "index.html",
			// Expires defines which user-defined function to use for producing a HTTP Expires Header. Default is nil.
			// https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
			Expires: func() string { return "max-age=0" },
		}))
	m.Use(session.Sessioner(session.Options{
		// Name of provider. Default is "memory".
		Provider: "memory",
		// Provider configuration, it's corresponding to provider.
		ProviderConfig: "",
		// Cookie name to save session ID. Default is "MacaronSession".
		CookieName: "MacaronSession",
		// Cookie path to store. Default is "/".
		CookiePath: "/",
		// GC interval time in seconds. Default is 3600.
		Gclifetime: 3600,
		// Max life time in seconds. Default is whatever GC interval time is.
		Maxlifetime: 3600,
		// Use HTTPS only. Default is false.
		Secure: false,
		// Cookie life time. Default is 0.
		CookieLifeTime: 0,
		// Cookie domain name. Default is empty.
		Domain: "",
		// Session ID length. Default is 16.
		IDLength: 16,
		// Configuration section name. Default is "session".
		Section: "session",
	}))
	m.Use(macaron.Renderer(macaron.RenderOptions{
		// Directory to load templates. Default is "templates".
		Directory: "templates",
		// Extensions to parse template files from. Defaults are [".tmpl", ".html"].
		Extensions: []string{".tmpl", ".html"},
		// Funcs is a slice of FuncMaps to apply to the template upon compilation. Default is [].
		Funcs: []template.FuncMap{map[string]interface{}{
			"AppName": func() string {
				return "Macaron"
			},
			"AppVer": func() string {
				return "1.0.0"
			},
		}},
		// Delims sets the action delimiters to the specified strings. Defaults are ["{{", "}}"].
		Delims: macaron.Delims{"{{", "}}"},
		// Appends the given charset to the Content-Type header. Default is "UTF-8".
		Charset: "UTF-8",
		// Outputs human readable JSON. Default is false.
		IndentJSON: true,
		// Outputs human readable XML. Default is false.
		IndentXML: true,
		// Prefixes the JSON output with the given bytes. Default is no prefix.
		// PrefixJSON: []byte("macaron"),
		// Prefixes the XML output with the given bytes. Default is no prefix.
		// PrefixXML: []byte("macaron"),
		// Allows changing of output to XHTML instead of HTML. Default is "text/html".
		HTMLContentType: "text/html",
	}))
	m.Use(cache.Cacher(cache.Options{
		// Name of adapter. Default is "memory".
		Adapter: "memory",
		// Adapter configuration, it's corresponding to adapter.
		AdapterConfig: "",
		// GC interval time in seconds. Default is 60.
		Interval: 60,
		// Configuration section name. Default is "cache".
		Section: "cache",
	}))

	// setup handlers
	m.Get("/", myHandler)
	m.Get("/welcome", myOtherHandler)
	m.Get("/query", myQueryStringHandler) // /query?name=Some+Name
	m.Get("/json", myJsonHandler)
	m.Post("/contact/submit", binding.Bind(ContactForm{}), mySubmitHandler)
	m.Get("/session", mySessionHandler)
	m.Get("/set/cookie/:value", mySetCookieHandler)
	m.Get("/get/cookie", myGetCookieHandler)
	m.Get("/database", myDatabaseHandler)
	m.Get("/database/list", myDatabaseListHandler)
	m.Get("/cache/write/:key/:value", myCacheWriteHandler)
	m.Get("/cache/read/:key", myCacheReadHandler)

	log.Println("Server is running on localhost:4000...")
	log.Println(http.ListenAndServe("0.0.0.0:4000", m))
}