Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.58 KB | None | 0 0
  1. //
  2. // To Do
  3. //    - Implement writer feature.
  4. //    - Implement accepted location with number of times visited.
  5. //          - Create a weight system to put it lower on the list if called.
  6. //    - Create scoring mechanism to rate locations.
  7. //    - Slack bot
  8. //    - Voting option
  9. //    - Yelp API integration for names and location.
  10. //    - Open table API integration for reservations.
  11. //    - Calender / scheduling integration.
  12.  
  13. package main
  14. import (
  15.     "bufio"
  16.     "fmt"
  17.     mathrand "math/rand"
  18.     "os"
  19.     "strings"
  20.     "time"
  21.     "net/http"
  22.     "html/template"
  23.     "log"
  24. )
  25.  
  26. var lunchPAGE = "tpl/lunch.gtpl"
  27. var lunchAddPAGE = "tpl/lunchadd.gtpl"
  28. var lunchPlacePAGE = "tpl/lunchplaces.gtpl"
  29. var lunchTimePAGE = "tpl/lunchtime.gtpl"
  30.  
  31. var locations []string
  32. var previousPicks []string
  33.  
  34. func readLines() {
  35.     file, err := os.Open("./places.txt")
  36.     if err != nil {
  37.         fmt.Println(err)
  38.         os.Exit(1)
  39.     }
  40.     defer file.Close()
  41.     scanner := bufio.NewScanner(file)
  42.     for scanner.Scan() {
  43.         s := strings.TrimRight(scanner.Text(), ",")
  44.         locations = append(locations,s)
  45.     }
  46.     return
  47. }
  48.  
  49. func random(min, max int) int {
  50.     mathrand.Seed(time.Now().UnixNano())
  51.     return mathrand.Intn(max - min) + min
  52. }
  53.  
  54. func stringInSlice(a string) bool {
  55.     for _, b := range previousPicks {
  56.         if b == a {
  57.             return true
  58.         }
  59.     }
  60.     return false
  61. }
  62.  
  63. func writeLocation(place string) bool {
  64.     locations = append(locations,place)
  65.     return true
  66. }
  67.  
  68. func selector() string {
  69.     maxLength := len(locations)
  70.     rando := random(0,maxLength)
  71.     pickedLocation := locations[rando]
  72.     return pickedLocation
  73. }
  74.  
  75. func isValidEntry(place string) bool{
  76.     if place != ""{
  77.         return true
  78.     } else {
  79.         return false
  80.     }
  81.  
  82. }
  83.  
  84. func lunch(w http.ResponseWriter, r *http.Request) {
  85.     tmpl := template.Must(template.ParseFiles(lunchPAGE))
  86.     pickedLocation := selector()
  87.     tmpl.Execute(w, pickedLocation)
  88. }
  89.  
  90. func lunchTime(w http.ResponseWriter, r *http.Request) {
  91.     tmpl := template.Must(template.ParseFiles(lunchTimePAGE))
  92.     pickedLocation := selector()
  93.     tmpl.Execute(w, pickedLocation)
  94. }
  95.  
  96. func lunchAdd(w http.ResponseWriter, r *http.Request) {
  97.     fmt.Println("method:", r.Method) //get method
  98.     if r.Method == "GET" {
  99.         t, _ := template.ParseFiles(lunchAddPAGE)
  100.         t.Execute(w, nil)
  101.     } else { //post method
  102.         r.ParseForm()
  103.         place := strings.Join(r.Form["place"], ",")
  104.         if place != "" {
  105.             fmt.Println("place added: ",place)
  106.             writeLocation(place)
  107.             t, _ := template.ParseFiles(lunchAddPAGE)
  108.             t.Execute(w, nil)
  109.         } else {
  110.             fmt.Println("no value found, throwing away.")
  111.             t, _ := template.ParseFiles(lunchAddPAGE)
  112.             t.Execute(w, nil)
  113.             list(w, nil)
  114.         }
  115.     }
  116. }
  117.  
  118. func list(w http.ResponseWriter, r *http.Request) {
  119.     tmpl := template.Must(template.ParseFiles(lunchPlacePAGE))
  120.     tmpl.Execute(w,locations)
  121. }
  122.  
  123. func test(w http.ResponseWriter, r *http.Request) {
  124.     tmpl := template.Must(template.ParseFiles("static/test.html"))
  125.     tmpl.Execute(w,locations)
  126. }
  127.  
  128. func main() {
  129.     readLines()
  130.     http.HandleFunc("/lunch", lunch)             //web view
  131.     http.HandleFunc("/lunchtime", lunchTime)     //curl view
  132.     http.HandleFunc("/lunchadd", lunchAdd)
  133.     http.HandleFunc("/lunchplaces", list)
  134.     http.HandleFunc("/test", test)
  135.     http.Handle("/", http.FileServer(http.Dir("static/")))
  136.     log.Fatal(http.ListenAndServe(":8081", nil))
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement