Ejemplo n.º 1
0
func (this *aboutController) get(w http.ResponseWriter, req *http.Request) {
	skills := models.GetSkills()
	skillsVM := []viewmodels.Skill{}

	for _, modelSkills := range skills {
		skillsVM = append(skillsVM, converter.ConvertSkillToViewModel(modelSkills))
	}

	vm := viewmodels.GetAbout()

	vm.Skills = skillsVM

	w.Header().Add("Content-Type", "text/html")

	responseWriter := util.GetResponseWriter(w, req)

	_, err := req.Cookie("goSessionId")
	if err == nil {
		vm.LoggedIn = true
	} else {
		vm.LoggedIn = false
	}

	this.template.Execute(responseWriter, vm)
	defer responseWriter.Close()
}
Ejemplo n.º 2
0
func main() {
	templates := populateTemplates()
	http.HandleFunc("/",
		func(w http.ResponseWriter, req *http.Request) {
			requestedFile := req.URL.Path[1:]
			//	log.Println("view event:", req)
			req.ParseForm()
			var context interface{} = nil
			template :=
				templates.Lookup(requestedFile + ".html")
			switch requestedFile {
			case "room":
				nickname := req.PostFormValue("nickname")
				if nickname == "" {
					nickname = "Visitor" + string(rand.Intn(100))
				}
				context = viewmodels.GetRoom(nickname)
			case "about":
				context = viewmodels.GetAbout()
			case "login":
				context = viewmodels.GetLogin()
			}
			if template != nil {
				template.Execute(w, context)
			} else {
				w.WriteHeader(404)
			}
		})

	http.HandleFunc("/api/", func(w http.ResponseWriter, req *http.Request) {
		requestedFile := req.URL.Path[5:]
		//log.Println("api event:", req)
		var context []byte
		switch requestedFile {
		case "getdata":
			context = apimodels.GetData(req)
		case "postdata":
			context = apimodels.PostData(req)
		}
		w.WriteHeader(200)
		w.Write(context)
	})

	http.HandleFunc("/img/", serveResource)
	http.HandleFunc("/css/", serveResource)
	http.HandleFunc("/scripts/", serveResource)
	log.Println("Listening in port 1720...")
	http.ListenAndServe(":1720", nil)
}