-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (46 loc) · 1.01 KB
/
main.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
package main
import (
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"strconv"
)
func main() {
handler := http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))
http.Handle("/assets/", handler)
http.HandleFunc("/", excerciseHandler)
http.HandleFunc("/{cnt}", excerciseHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
type Page struct {
Cnt int
Pages []Exec
}
type Exec struct {
Id string
Title string
Description string
}
func excerciseHandler(w http.ResponseWriter, r *http.Request) {
ct, err := strconv.Atoi(r.PathValue("cnt"))
if err != nil {
ct = 5
}
maxExc := 30
if ct > maxExc {
ct = maxExc
}
var pages []Exec
for i := 1; i <= ct; i++ {
pid := rand.Intn(maxExc) + 1
pages = append(pages, Exec{Id: fmt.Sprintf("%02d", pid), Title: "Excercise " + strconv.Itoa(pid), Description: "Excercise " + strconv.Itoa(pid)})
}
p := Page{Cnt: ct, Pages: pages}
t, err := template.ParseFiles("page.html")
if err != nil {
log.Fatal(err)
}
t.Execute(w, p)
}