Exemplo n.º 1
0
// Listen and serve of the framework
func (self *Application) ListenAndServe(port int) error {
	// Print some (mostly) useless info
	Log.Printf("%d %s loaded", self.routesCount, utility.Is(self.routesCount > 1, "routes", "route"))
	Log.Printf("listening on localhost:%d", port)

	// Handle the connection
	return http.ListenAndServe(fmt.Sprintf(":%d", port), self)
}
Exemplo n.º 2
0
// A helper function that allows returning JSON error pages
func Error(code int, w http.ResponseWriter) {
	// Write headers
	w.WriteHeader(code)
	// Write a formatted JSON error document
	// Why not encoding/json?
	//  - Sprintf is slightly faster and I don't need structs
	// Why not goson?
	//  - Error 500 is triggered when a template doesn't exist.
	//    using goson would probably loop it
	w.Write(
		[]byte(
			fmt.Sprintf(
				`{"error":%d, "message":"%s"}`,
				code,
				utility.Is(
					len(messages[code]) != 0,
					messages[code],
					"Error not found", // this
				), // syntax
			), // is
		), // retarded
	)
}