-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmain.go
86 lines (72 loc) · 2.17 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
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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
)
type PageVars struct {
Title string
Scalearp string
Key string
Pitch string
DuetImgPath string
ScaleImgPath string
GifPath string
AudioPath string
AudioPath2 string
DuetAudioBoth string
DuetAudio1 string
DuetAudio2 string
LeftLabel string
RightLabel string
ScaleOptions []ScaleOptions
DuetOptions []ScaleOptions
PitchOptions []ScaleOptions
KeyOptions []ScaleOptions
OctaveOptions []ScaleOptions
}
type ScaleOptions struct {
Name string
Value string
IsDisabled bool
IsChecked bool
Text string
}
func main() {
// this code below makes a file server and serves everything as a file
// fs := http.FileServer(http.Dir(""))
// http.Handle("/", fs)
// serve everything in the css folder, the img folder and mp3 folder as a file
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("img"))))
http.Handle("/mp3/", http.StripPrefix("/mp3/", http.FileServer(http.Dir("mp3"))))
// when navigating to /home it should serve the home page
http.HandleFunc("/", Home)
http.HandleFunc("/scale", Scale)
http.HandleFunc("/scaleshow", ScaleShow)
http.HandleFunc("/duets", Duets)
http.HandleFunc("/duetshow", DuetShow)
http.ListenAndServe(getPort(), nil)
}
// Detect $PORT and if present uses it for listen and serve else defaults to :8080
// This is so that app can run on Heroku
func getPort() string {
p := os.Getenv("PORT")
if p != "" {
return ":" + p
}
return ":8080"
}
func render(w http.ResponseWriter, tmpl string, pageVars PageVars) {
tmpl = fmt.Sprintf("templates/%s", tmpl) // prefix the name passed in with templates/
t, err := template.ParseFiles(tmpl) //parse the template file held in the templates folder
if err != nil { // if there is an error
log.Print("template parsing error: ", err) // log it
}
err = t.Execute(w, pageVars) //execute the template and pass in the variables to fill the gaps
if err != nil { // if there is an error
log.Print("template executing error: ", err) //log it
}
}