-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (61 loc) · 1.52 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
package main
import (
"flag"
"fmt"
"image"
"image/jpeg"
"log"
"net"
"net/http"
"os"
"github.com/gophers-frm/go-funk-simplified/audio"
"github.com/gophers-frm/go-funk-simplified/renderer"
"github.com/gophers-frm/go-funk-simplified/sampler"
)
const filePath = "public/rick/fixture.wav"
func main() {
var port = flag.String("port", "8080", "server port")
flag.Parse()
addr := net.JoinHostPort("", *port)
sampler.RegSampler("", sampler.AbsAvg)
renderer.RegColorFunc("", renderer.Colors["black"])
data, err := getFileData(filePath)
if err != nil {
panic(err)
}
start(addr, data)
}
func start(addr string, data audio.WavData) {
http.HandleFunc("/", status)
http.HandleFunc("/samples", samplesHandler(data))
http.HandleFunc("/waveform", waveformHandler(data))
http.HandleFunc("/gif", gifHandler(data))
http.HandleFunc("/gif2", gif2Handler(data))
http.HandleFunc("/gopher", gopherHandler(data, gopherImage()))
http.Handle("/rick/", http.StripPrefix("/", http.FileServer(http.Dir("./public"))))
log.Fatal(http.ListenAndServe(addr, nil))
}
func getFileData(filePath string) (data audio.WavData, err error) {
file, err := os.Open(filePath)
if err != nil {
return
}
defer file.Close()
data, err = audio.NewWavData(file)
return
}
func gopherImage() image.Image {
f, err := os.Open("public/rick/gopher.jpg")
if err != nil {
panic(err)
}
defer f.Close()
img, err := jpeg.Decode(f)
if err != nil {
panic(err)
}
return img
}
func status(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "ok")
}