first commit

This commit is contained in:
Matthieu Lamalle 2019-11-20 17:11:29 +01:00
commit faaf625fa8
6 changed files with 186 additions and 0 deletions

0
csv/bdd.csv Normal file
View File

8
csv/foods.csv Normal file
View File

@ -0,0 +1,8 @@
sandwich,🥪
sushi,🍣
burger,🍔
tacos,🌯
italien,🍝
kebab,🥙
pizza,🍕
hotdog,🌭
1 sandwich 🥪
2 sushi 🍣
3 burger 🍔
4 tacos 🌯
5 italien 🍝
6 kebab 🥙
7 pizza 🍕
8 hotdog 🌭

BIN
server Executable file

Binary file not shown.

102
server.go Normal file
View File

@ -0,0 +1,102 @@
package main
import (
"bufio"
"encoding/csv"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/Joker/jade"
)
type FoodOfTheDay struct {
Date string
Foods map[string]string
}
func main() {
http.HandleFunc("/", base)
http.HandleFunc("/results", results)
log.Print("ready: listening on localhost:8080\n")
food()
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func base(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
log.Printf("vote")
} else if r.Method == http.MethodGet {
home(w, r)
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Unsupported Request Method!"))
}
}
func home(w http.ResponseWriter, r *http.Request) {
dat, err := ioutil.ReadFile("./templates/index.go")
if err != nil {
log.Printf("ReadFile error: %v", err)
return
}
tmpl, err := jade.Parse("index", []byte(dat))
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 {
log.Printf("\nExecute error: %v", err)
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)
}

53
templates/index.go Normal file
View File

@ -0,0 +1,53 @@
<!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>

23
templates/results.go Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<head>
<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 src="https://js.pusher.com/4.1/pusher.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.js"></script>
</head>
<body>
<div class="ui container">
<h2 class="ui header">TV Show - Poll Results</h2><br/>
<div class="ui one column grid link cards">
<div class="card">
<div id="canvas" style="height: 500px; width: 100%;"></div>
</div>
</div>
</div>
<div style="color:#fff" id="key">{{.Key}}</div>
<div style="color:#fff" id="cluster">{{.Cluster}}</div>
</body>
</html>