Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // To Do
- // - Implement writer feature.
- // - Implement accepted location with number of times visited.
- // - Create a weight system to put it lower on the list if called.
- // - Create scoring mechanism to rate locations.
- // - Slack bot
- // - Voting option
- // - Yelp API integration for names and location.
- // - Open table API integration for reservations.
- // - Calender / scheduling integration.
- package main
- import (
- "bufio"
- "fmt"
- mathrand "math/rand"
- "os"
- "strings"
- "time"
- "net/http"
- "html/template"
- "log"
- )
- var lunchPAGE = "tpl/lunch.gtpl"
- var lunchAddPAGE = "tpl/lunchadd.gtpl"
- var lunchPlacePAGE = "tpl/lunchplaces.gtpl"
- var lunchTimePAGE = "tpl/lunchtime.gtpl"
- var locations []string
- var previousPicks []string
- func readLines() {
- file, err := os.Open("./places.txt")
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- defer file.Close()
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- s := strings.TrimRight(scanner.Text(), ",")
- locations = append(locations,s)
- }
- return
- }
- func random(min, max int) int {
- mathrand.Seed(time.Now().UnixNano())
- return mathrand.Intn(max - min) + min
- }
- func stringInSlice(a string) bool {
- for _, b := range previousPicks {
- if b == a {
- return true
- }
- }
- return false
- }
- func writeLocation(place string) bool {
- locations = append(locations,place)
- return true
- }
- func selector() string {
- maxLength := len(locations)
- rando := random(0,maxLength)
- pickedLocation := locations[rando]
- return pickedLocation
- }
- func isValidEntry(place string) bool{
- if place != ""{
- return true
- } else {
- return false
- }
- }
- func lunch(w http.ResponseWriter, r *http.Request) {
- tmpl := template.Must(template.ParseFiles(lunchPAGE))
- pickedLocation := selector()
- tmpl.Execute(w, pickedLocation)
- }
- func lunchTime(w http.ResponseWriter, r *http.Request) {
- tmpl := template.Must(template.ParseFiles(lunchTimePAGE))
- pickedLocation := selector()
- tmpl.Execute(w, pickedLocation)
- }
- func lunchAdd(w http.ResponseWriter, r *http.Request) {
- fmt.Println("method:", r.Method) //get method
- if r.Method == "GET" {
- t, _ := template.ParseFiles(lunchAddPAGE)
- t.Execute(w, nil)
- } else { //post method
- r.ParseForm()
- place := strings.Join(r.Form["place"], ",")
- if place != "" {
- fmt.Println("place added: ",place)
- writeLocation(place)
- t, _ := template.ParseFiles(lunchAddPAGE)
- t.Execute(w, nil)
- } else {
- fmt.Println("no value found, throwing away.")
- t, _ := template.ParseFiles(lunchAddPAGE)
- t.Execute(w, nil)
- list(w, nil)
- }
- }
- }
- func list(w http.ResponseWriter, r *http.Request) {
- tmpl := template.Must(template.ParseFiles(lunchPlacePAGE))
- tmpl.Execute(w,locations)
- }
- func test(w http.ResponseWriter, r *http.Request) {
- tmpl := template.Must(template.ParseFiles("static/test.html"))
- tmpl.Execute(w,locations)
- }
- func main() {
- readLines()
- http.HandleFunc("/lunch", lunch) //web view
- http.HandleFunc("/lunchtime", lunchTime) //curl view
- http.HandleFunc("/lunchadd", lunchAdd)
- http.HandleFunc("/lunchplaces", list)
- http.HandleFunc("/test", test)
- http.Handle("/", http.FileServer(http.Dir("static/")))
- log.Fatal(http.ListenAndServe(":8081", nil))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement