Exemple #1
0
//Genertor to generate a handler that loads a template and applies a Page loaded from json.
func GenJSONServer(root string) func(http.ResponseWriter, *http.Request) {

	return func(w http.ResponseWriter, req *http.Request) {
		filename := getFilename(req, "index")

		logger.Logf(1, "Serving document to %s: %s", req.RemoteAddr, filename)
		page, err := jsonLoadPage(root, filename)
		p := entity.GetPlayer(1337, "hunter02")

		if err == nil {

			page.ApplyPageTemplate(p)
			template, err := FileGet(root, "/template.html")

			if err != nil {
				Error(404, root, w, req)
				return
			}
			err = ApplyTemplate(w, page, template)

			if err != nil {
				Error(404, root, w, req)
				return
			}
		} else {
			logger.Logf(2, "Error: %s", err.Error())
			Error(404, root, w, req)
		}
	}
}
Exemple #2
0
//Root server generates a function that serves html content from the root directory and all subdirectories except res/.
//That is the folder dedicated to resources and need special treatment.
//It loads a file from disk and treats it as a template and processes it based on the player object
//it gets from the GetPlayer(...) metod.
func GenRootServer(root string) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, req *http.Request) {
		filename := getFilename(req, "index.html")

		logger.Logf(1, "Serving document to %s: %s", req.RemoteAddr, filename)

		b, err := FileGet(root, filename)
		if len(b) < 1 || err != nil {
			logger.Logf(5, "len(b): %d, err: %s", len(b), err)
			Error(404, root, w, req)
			return
		}

		p := entity.GetPlayer(1337, "hunter02")
		err = ApplyTemplate(w, p, b)
		if err != nil {
			logger.Logf(2, "Failed to apply template: %s", err)
			Error(404, root, w, req)
			return
		}
	}
}
//Using a websocket connection, credetials is accuired and
//used to get a player from the database.
//If no player is found in the database, a new player is created.
func GetPlayer(ws *websocket.Conn) *entity.Player {
	cred, _ := structures.GetWSCreds(ws)
	return entity.GetPlayer(cred.Split())
}