-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofc.go
120 lines (104 loc) · 2.39 KB
/
ofc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strconv"
"strings"
"github.com/BurntSushi/toml"
)
var sessions map[int]*Session = make(map[int]*Session)
var nextSessionId int = 0
type Session struct {
deck []Card
curCard int
curSide int
}
func getDeckToml(addr string) ([]byte, error) {
response, err := http.Get(addr)
if err != nil {
fmt.Println("get_error")
return []byte(""), err
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("read_error")
return []byte(""), err
}
return body, nil
}
func (session *Session) loadDeck(deckAddress string) error {
deckString, err := getDeckToml(deckAddress)
if err != nil {
return err
}
var tomlConfig TomlConfig
err = toml.Unmarshal(deckString, &tomlConfig)
if err != nil {
return err
}
var orderedDeck []Card
for front, back := range tomlConfig.Deck {
orderedDeck = append(orderedDeck, Card{front, back})
}
deckLength := len(orderedDeck)
session.deck = make([]Card, deckLength, deckLength)
order := rand.Perm(deckLength)
for i, j := range order {
session.deck[i] = orderedDeck[j]
}
return nil
}
func (session *Session) next() string {
if session.curCard == len(session.deck) {
return "Done!"
}
if session.curSide == 0 {
text := session.deck[session.curCard].Front
session.curSide++
return text
}
text := session.deck[session.curCard].Back
session.curSide = 0
session.curCard++
return text
}
type TomlConfig struct {
Deck map[string]string `toml:"deck"`
}
type Card struct {
Front string
Back string
}
type Page struct {
CardText string
Link string
}
func handler(w http.ResponseWriter, r *http.Request) {
firstPath := strings.Split(r.URL.Path, "/")[1]
sessionId, err := strconv.Atoi(firstPath)
// We need to create a new session ID
if err != nil {
sessionId = nextSessionId
nextSessionId++
sessions[sessionId] = &Session{[]Card{}, 0, 0}
err := (*sessions[sessionId]).loadDeck("http://" + r.URL.Path[1:])
if err != nil {
fmt.Println(err)
}
}
session := sessions[sessionId]
link := fmt.Sprintf("/%d", sessionId)
page := &Page{CardText: session.next(), Link: link}
t, _ := template.ParseFiles("index.html")
t.Execute(w, page)
}
func nullHandler(w http.ResponseWriter, r *http.Request) {}
func main() {
http.HandleFunc("/favicon.ico", nullHandler)
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}