base safe

This commit is contained in:
Matthieu Lamalle 2019-11-21 13:54:40 +01:00
parent faaf625fa8
commit 84227209ee
10 changed files with 132 additions and 114 deletions

32
bdd/bdd.go Normal file
View File

@ -0,0 +1,32 @@
package bdd
import (
"bufio"
"encoding/csv"
"fmt"
bolt "go.etcd.io/bbolt"
"io"
"log"
"os"
)
// BUCKET est le nom de la base
var BUCKET = "FoodBucket"
// InitBDD initialise la BDD
func InitBDD() bolt.DB {
db, err := bolt.Open("foods.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte(BUCKET))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
log.Print(b)
return nil
})
}

View File

41
foodlist/foodlist.go Normal file
View File

@ -0,0 +1,41 @@
package foodlist
import (
"bufio"
"encoding/csv"
"io"
"log"
"os"
)
// FoodOfTheDay is the food list of the day
type FoodOfTheDay struct {
Date string
Foods []Food
}
// Food is a type of food
type Food struct {
Title string
Icon string
}
// GetFoodOfTheDay return the list of food of the day
func GetFoodOfTheDay(Today string) FoodOfTheDay {
foodcsv, _ := os.Open("foodlist/foods.csv")
foodreader := csv.NewReader(bufio.NewReader(foodcsv))
foods := FoodOfTheDay{}
for {
line, err := foodreader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
food := Food{line[0], line[1]}
foods.Foods = append(foods.Foods, food)
}
foods.Date = Today
return foods
}

BIN
my.db Normal file

Binary file not shown.

BIN
server

Binary file not shown.

View File

@ -1,23 +1,15 @@
package main package main
import ( import (
"bufio" "cadoles/foodoles/foodlist"
"encoding/csv"
"html/template" "html/template"
"io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
"time" "time"
"github.com/Joker/jade"
) )
type FoodOfTheDay struct { // Today is today
Date string var Today = time.Now().Format("02/01/2006")
Foods map[string]string
}
func main() { func main() {
@ -25,15 +17,14 @@ func main() {
http.HandleFunc("/results", results) http.HandleFunc("/results", results)
log.Print("ready: listening on localhost:8080\n") log.Print("ready: listening on localhost:8080\n")
food()
if err := http.ListenAndServe(":8080", nil); err != nil { if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err) panic(err)
} }
} }
func base(w http.ResponseWriter, r *http.Request) { func base(w http.ResponseWriter, r *http.Request) {
// TODO: Init la base avec la date du jour
if r.Method == http.MethodPost { if r.Method == http.MethodPost {
log.Printf("vote") log.Printf("vote")
} else if r.Method == http.MethodGet { } else if r.Method == http.MethodGet {
@ -45,58 +36,31 @@ func base(w http.ResponseWriter, r *http.Request) {
} }
func home(w http.ResponseWriter, r *http.Request) { func home(w http.ResponseWriter, r *http.Request) {
dat, err := ioutil.ReadFile("./templates/index.go") foods := foodlist.GetFoodOfTheDay(Today)
if err != nil { paths := []string{
log.Printf("ReadFile error: %v", err) "./templates/index.tmpl",
return
} }
t := template.Must(template.New("index.tmpl").ParseFiles(paths...))
tmpl, err := jade.Parse("index", []byte(dat)) err := t.Execute(w, foods)
if err != nil {
log.Printf("Parse error: %v", err)
return
}
w.Write([]byte(tmpl))
}
func results(w http.ResponseWriter, r *http.Request) {
dat, err := ioutil.ReadFile("./templates/results.go")
if err != nil {
log.Printf("ReadFile error: %v", err)
return
}
tmpl, err := jade.Parse("results", []byte(dat))
if err != nil {
log.Printf("Parse error: %v", err)
return
}
goTpl, err := template.New("html").Parse(tmpl)
if err != nil {
log.Printf("\nTemplate parse error: %v", err)
return
}
err = goTpl.Execute(w, "")
if err != nil { if err != nil {
log.Printf("\nExecute error: %v", err) log.Printf("\nExecute error: %v", err)
return return
} }
} }
func food() {
foodcsv, _ := os.Open("csv/foods.csv")
foodreader := csv.NewReader(bufio.NewReader(foodcsv))
foods := FoodOfTheDay{time.Now().Format("20060102"), make(map[string]string)}
for {
line, err := foodreader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
foods.Foods[line[0]] = line[1]
}
log.Printf("%+q", foods)
func results(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// option := r.Form.Get("option")
// foodlist.VoteForFood(Today, option)
// paths := []string{
// "./templates/results.tmpl",
// }
// t := template.Must(template.New("results.tmpl").ParseFiles(paths...))
// err := t.Execute(w, "")
// if err != nil {
// log.Printf("\nExecute error: %v", err)
// return
// }
} }

View File

@ -1,53 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Electronic Realtime Poll written in Go</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.css">
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.js"></script>
<style type="text/css"> h2 { margin: 2em 0em; } .ui.container { padding-top: 5em; padding-bottom: 5em; } </style>
<script type="text/javascript">function vote(option){$.post( "/", {option}, ( data ) => { $('.mini.modal').modal('show');});}</script>
</head>
<body>
<div class="ui container">
<h1 class="ui header">Foodoles</h1><br/>
<h2 class="ui header">Pick Your Preferred Food For Lunch</h2><br/>
<div class="ui four column grid link cards">
<div onclick="vote('sandwich')">
<div class="ui massive label">🥪</div>
</div>
<div onclick="vote('sushi')">
<div class="ui massive label">🍣</div>
</div>
<div onclick="vote('burger')">
<div class="ui massive label">🍔</div>
</div>
<div onclick="vote('tacos')">
<div class="ui massive label">🌯</div>
</div>
<div onclick="vote('italien')">
<div class="ui massive label">🍝</div>
</div>
<div onclick="vote('kebab')">
<div class="ui massive label">🥙</div>
</div>
<div onclick="vote('pizza')">
<div class="ui massive label">🍕</div>
</div>
<div onclick="vote('hotdog')">
<div class="ui massive label">🌭</div>
</div>
</div>
</div>
<div class="ui modal mini">
<div class="header">Foodoles</div>
<div class="content">
<p>Vote sent <strong>successfully</strong>, <a href="/results"> check results !</a></p>
</div>
<div class="actions">
<div class="ui cancel button blue">Close</div>
</div>
</div>
</body>
</html>

34
templates/index.tmpl Normal file
View File

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Foodoles written in Go</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.css">
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.js"></script>
<style type="text/css"> h2 { margin: 2em 0em; } .ui.container { padding-top: 5em; padding-bottom: 5em; } </style>
<script type="text/javascript">function vote(option){$.post( "/", {option}, ( data ) => { $('.mini.modal').modal('show');});}</script>
</head>
<body>
<div class="ui container">
<h1 class="ui header">Foodoles - {{.Date}}</h1><br/>
<h2 class="ui header">Pick Your Preferred Lunch's Food For Today</h2><br/>
<div class="ui four column grid link cards">
{{ range .Foods }}
<div onclick="vote('{{ .Title }}')">
<div class="ui massive label"> {{ .Icon }}</div>
</div>
{{ end }}
</div>
</div>
<div class="ui modal mini">
<div class="header">Foodoles</div>
<div class="content">
<p>Vote sent <strong>successfully</strong>, <a href="/results"> check results !</a></p>
</div>
<div class="actions">
<div class="ui cancel button blue">Close</div>
</div>
</div>
</body>
</html>