//ResultHandler for result view func SearchResultJSON(w http.ResponseWriter, r *http.Request) { query := r.URL.Query().Get("q") page := r.URL.Query().Get("p") pageInt := 1 var err error if page != "" { pageInt, err = strconv.Atoi(page) if err != nil { log.Println(err) } } listings, err := models.Listing{}.Search(config.Get(), query, pageInt) if err != nil { log.Println(err) } ads, err := models.Advert{}.GetAll(config.Get()) if err != nil { log.Println(err) } posts := []Post{} k := 0 for i := 0; i < len(listings.Data); i++ { if (i+1)%3 == 0 && k < len(ads) { post := Post{} post.Advert = ads[k] post.Type = config.ADVERT k++ posts = append(posts, post) } post := Post{} post.Type = config.LISTING post.Listing = listings.Data[i] posts = append(posts, post) } newURL := r.URL.Query() newURL.Set("p", strconv.Itoa(listings.Page.NextVal)) log.Println(newURL) listings.Page.NextURL = r.URL.Path + "?" + newURL.Encode() data := struct { Posts []Post Page models.Page PageHeading string PageSubheading string }{ Posts: posts, Page: listings.Page, PageHeading: "Search", PageSubheading: query, } err = json.NewEncoder(w).Encode(data) if err != nil { log.Println(err) } }
func GetAdmins() ([]User, error) { result := []User{} collection := config.Get().Database.C("admin").With(config.Get().Database.Session.Copy()) err := collection.Find(bson.M{}).All(&result) if err != nil { return result, err } return result, nil }
func NewUserHandler(w http.ResponseWriter, r *http.Request) { var user User err := json.NewDecoder(r.Body).Decode(&user) if err != nil { log.Println(err) } collection := config.Get().Database.C("admin").With(config.Get().Database.Session.Copy()) err = collection.Insert(user) if err != nil { log.Println(err) } }
//ResultHandler for result view func GetPlusListingsJSON(w http.ResponseWriter, r *http.Request) { listings, err := models.Listing{}.GetAllPlusListings(config.Get()) if err != nil { log.Println(err) } ads, err := models.Advert{}.GetAll(config.Get()) if err != nil { log.Println(err) } posts := []Post{} k := 0 for i := 0; i < len(listings.Data); i++ { if (i+1)%3 == 0 && k > len(ads) { post := Post{} post.Advert = ads[k] post.Type = config.ADVERT k++ posts = append(posts, post) } post := Post{} post.Type = config.LISTING post.Listing = listings.Data[i] posts = append(posts, post) } newURL := r.URL.Query() newURL.Set("p", strconv.Itoa(listings.Page.NextVal)) log.Println(newURL) listings.Page.NextURL = r.URL.Path + "?" + newURL.Encode() data := struct { Posts []Post Page models.Page PageHeading string PageSubheading string }{ Posts: posts, Page: listings.Page, PageHeading: "Pluslistings", } err = json.NewEncoder(w).Encode(data) if err != nil { log.Println(err) } }
//Approvehandler to approve lsitings for view func Approvehandler(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get("q") err := models.Listing{}.Approve(config.Get(), id) if err != nil { log.Println(err) } }
//ResultHandler for result view func GetAdvertsJSON(w http.ResponseWriter, r *http.Request) { ads, err := models.Advert{}.GetAll(config.Get()) if err != nil { log.Println(err) } posts := []Post{} for i := 0; i < len(ads); i++ { post := Post{} post.Advert = ads[i] post.Type = "advert" posts = append(posts, post) } data := struct { Posts []Post PageHeading string PageSubheading string }{ Posts: posts, PageHeading: "Adverts", } err = json.NewEncoder(w).Encode(data) if err != nil { log.Println(err) } }
//ResultHandler for result view func GetAdvertsHandler(w http.ResponseWriter, r *http.Request) { ads, err := models.Advert{}.GetAll(config.Get()) if err != nil { log.Println(err) } posts := []Post{} for i := 0; i < len(ads); i++ { post := Post{} post.Advert = ads[i] post.Type = "advert" posts = append(posts, post) } data := struct { Posts []Post Page models.Page PageHeading string PageSubheading string }{ Posts: posts, Page: models.Page{}, PageHeading: "Adverts", } tmp := GetTemplates().Lookup("list.html") tmp.Execute(w, data) }
//NewAdHandler handler for adding new Adverts func NewAdHandler(w http.ResponseWriter, r *http.Request) { var formdata models.Advert err := json.NewDecoder(r.Body).Decode(&formdata) if err != nil { log.Println(err) } formdata.New(config.Get()) }
func AdminLogin(w http.ResponseWriter, r *http.Request) { var user User var result Admin err := json.NewDecoder(r.Body).Decode(&user) if err != nil { log.Println(err) } log.Println(user) collection := config.Get().Database.C("admin").With(config.Get().Database.Session.Copy()) log.Println(user.Username) err = collection.Find(bson.M{"username": user.Username, "password": user.Password}).One(&result) if err != nil { log.Println(err) } data, _ := json.Marshal(result) w.Write(data) }
//GetunapprovedHandler gets unapproved listings func Getunapproved(w http.ResponseWriter, r *http.Request) { data, err := models.Listing{}.GetAllUnapproved(config.Get()) if err != nil { log.Println(err) } result, err := json.Marshal(data) if err != nil { log.Println(err) } w.Header().Set("Content-Type", "application/json") w.Write(result) }
//HomeHandler for listing view func HomeHandler(w http.ResponseWriter, r *http.Request) { categories, err := models.Category{}.GetAll(config.Get()) if err != nil { log.Println(err) } data := struct { Categories []models.Category }{ Categories: categories, } tmp := GetTemplates().Lookup("index.html") tmp.Execute(w, data) }
func GetListing(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get("q") data, err := models.Listing{}.GetOne(config.Get(), id) if err != nil { log.Println(err) } result, err := json.Marshal(data) if err != nil { log.Println(err) } w.Header().Set("Content-Type", "application/json") w.Write(result) }
func TimeUpdate(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get("q") expiry := r.URL.Query().Get("expiry") if expiry == "3000" { expiry = "1" } else { expiry = "2" } err := models.Listing{}.TimeUpdate(config.Get(), id, expiry) if err != nil { log.Println(err) } http.ServeFile(w, r, "cust/partials/success.html") }
//AddHandler for adding Listings func AddListing(w http.ResponseWriter, r *http.Request) { var formdata models.Listing err := json.NewDecoder(r.Body).Decode(&formdata) if err != nil { log.Println(err) } formdata.Add(config.Get()) data := struct { Message string }{"Successs adding listing"} w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(data) }
//CategoryHandler for view func SingleListingHandler(w http.ResponseWriter, r *http.Request) { params := r.Context().Value("params").(httprouter.Params) slug := params.ByName("listing") log.Printf("params %+v", params) listing, err := models.Listing{}.GetOne(config.Get(), slug) if err != nil { log.Println(err) } data := struct { Listing models.Listing }{ Listing: listing, } tmp := GetTemplates().Lookup("single.html") log.Printf("%+v", tmp) tmp.Execute(w, data) }
func FBLogin(w http.ResponseWriter, r *http.Request) { var user models.User err := json.NewDecoder(r.Body).Decode(&user) if err != nil { log.Println(err) } user.Type = config.FACEBOOK err = user.Add(config.Get()) if err != nil { log.Println(err) } data := struct { Message string }{"login success"} w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(data) }
//GetHandler function func GetListings(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get("q") page := r.URL.Query().Get("p") pageInt := 1 var err error if page != "" { pageInt, err = strconv.Atoi(page) if err != nil { log.Println(err) } } data, err := models.Listing{}.GetAllInCategory(config.Get(), id, pageInt) if err != nil { log.Println(err) } result, err := json.Marshal(data) if err != nil { log.Println(err) } w.Header().Set("Content-Type", "application/json") w.Write(result) }
func main() { web.TemplateInit() config.Init() defer config.Get().Database.Session.Close() commonHandlers := alice.New(web.LoggingHandler) //web.RecoverHandler, context.ClearHandler, router := NewRouter() router.ServeFiles("/zohoverify/*filepath", http.Dir("assets")) router.Get("/google28373290a86b6ef4.html", commonHandlers.ThenFunc(func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./assets/google28373290a86b6ef4.html") })) router.ServeFiles("/assets/*filepath", http.Dir("assets")) router.Get("/", commonHandlers.ThenFunc(web.HomeHandler)) router.Get("/search", commonHandlers.ThenFunc(web.SearchResultHandler)) router.Get("/pluslistings", commonHandlers.ThenFunc(web.GetPlusListingsHandler)) router.Get("/adverts", commonHandlers.ThenFunc(web.GetAdvertsHandler)) router.Get("/categories/:category", commonHandlers.ThenFunc(web.CategoryListingsHandler)) router.Get("/listings/:listing", commonHandlers.ThenFunc(web.SingleListingHandler)) router.Get("/register_business", commonHandlers.ThenFunc(web.RegisterListing)) router.Get("/register_plus_business", commonHandlers.ThenFunc(web.RegisterPlusListing)) router.Get("/admin", commonHandlers.ThenFunc(web.FrontAdminHandler)) router.Get("/login", commonHandlers.ThenFunc(web.LoginAdmin)) router.Get("/Newlisting", commonHandlers.ThenFunc(web.ClientIndex)) router.Get("/addlistingtemp", commonHandlers.ThenFunc(web.AddListingViewHandler)) router.Get("/addlisting", commonHandlers.ThenFunc(web.AddListingView)) //router.Post("/login", commonHandlers.ThenFunc(web.Login)) router.Post("/adminlogin", commonHandlers.ThenFunc(web.AdminLogin)) router.Get("/viewlistingtemp", commonHandlers.ThenFunc(web.UnapprovedViewHandler)) router.Get("/api/categories/:category", commonHandlers.ThenFunc(web.CategoryListingsJSON)) router.Get("/api/search", commonHandlers.ThenFunc(web.SearchResultJSON)) router.Get("/api/pluslistings", commonHandlers.ThenFunc(web.GetPlusListingsJSON)) router.Get("/api/unapproved", commonHandlers.ThenFunc(web.Getunapproved)) router.Post("/api/addcat", commonHandlers.ThenFunc(web.AddCategory)) router.Get("/api/getcat", commonHandlers.ThenFunc(web.GetCategories)) router.Get("/api/adminList", commonHandlers.ThenFunc(web.GetAdminsHandler)) router.Post("/api/newuser", commonHandlers.ThenFunc(web.NewUserHandler)) router.Post("/api/newAd", commonHandlers.ThenFunc(web.NewAdHandler)) router.Post("/api/addlisting", commonHandlers.ThenFunc(web.AddListing)) router.Post("/api/approve", commonHandlers.ThenFunc(web.Approvehandler)) router.Post("/api/login/facebook", commonHandlers.ThenFunc(web.FBLogin)) router.Get("/newad", commonHandlers.ThenFunc(web.NewAdvertHandler)) router.Get("/addcattemp", commonHandlers.ThenFunc(func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "admin/partials/addcat.html") })) router.ServeFiles("/temp/*filepath", http.Dir("admin/partials")) router.ServeFiles("/cust/partials/*filepath", http.Dir("cust/partials")) router.ServeFiles("/cust/js/*filepath", http.Dir("cust/js")) router.Get("/api/adverts", commonHandlers.ThenFunc(web.GetAdvertsJSON)) /* router.Get("/client", commonHandlers.ThenFunc(ClientAdmin)) router.Get("/Newlisting", commonHandlers.ThenFunc(ClientIndex)) router.Get("/client/update", commonHandlers.ThenFunc(TimeUpdatehandler)) //router.Get("/api/plus", commonHandlers.ThenFunc(web.GetPlusPayHandler)) router.Get("/category/*routes", commonHandlers.ThenFunc(ClientViewHandler)) router.Get("/plus/*routes", commonHandlers.ThenFunc(ClientViewHandler)) router.Get("/advert/*routes", commonHandlers.ThenFunc(ClientViewHandler)) router.Get("/listing/*routes", commonHandlers.ThenFunc(ClientViewHandler)) router.Get("/result/*routes", commonHandlers.ThenFunc(ClientViewHandler)) router.Get("/cust", commonHandlers.ThenFunc(CustHandler)) //api requests below router.Post("/login", commonHandlers.ThenFunc(web.Login)) router.Post("/api/result", commonHandlers.ThenFunc(web.SearchHandler)) router.Get("/api/listings", commonHandlers.ThenFunc(web.GetListHandler)) router.Get("/api/getcatList", commonHandlers.ThenFunc(web.GetHandler)) router.Get("/api/getsingle", commonHandlers.ThenFunc(web.getCatHandler)) router.Get("/api/getsinglelist", commonHandlers.ThenFunc(web.getlistHandler)) router.Get("/api/newview", commonHandlers.ThenFunc(web.GetNewView)) router.Get("/api/falseview", commonHandlers.ThenFunc(web.FalseH)) router.Get("/false", commonHandlers.ThenFunc(web.Fictionalcat)) router.Get("/slider", commonHandlers.ThenFunc(web.SliderHandler)) router.Get("/Upload", commonHandlers.ThenFunc(web.CsvHandler)) router.Get("/fix", commonHandlers.ThenFunc(web.Fix)) //forpayment router.Get("/newapp", commonHandlers.ThenFunc(web.PaymentAfter)) router.Get("/napp", commonHandlers.ThenFunc(web.Post_Params)) router.Get("/error", commonHandlers.ThenFunc(web.NoPaymentAfter)) */ PORT := os.Getenv("PORT") if PORT == "" { log.Println("No Global port has been defined, using default") PORT = "8080" } handler := cors.New(cors.Options{ // AllowedOrigins: []string{"http://localhost:3000"}, AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "DELETE"}, AllowCredentials: true, AllowedHeaders: []string{"Accept", "Content-Type", "X-Auth-Token", "*"}, Debug: false, }).Handler(router) log.Println("serving ") log.Fatal(http.ListenAndServe(":"+PORT, handler)) }