-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
80 lines (72 loc) · 1.51 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
package main
import (
"bufio"
//"encoding/json"
"fmt"
//"bytes"
"html/template"
//"io"
//"flag"
"io/ioutil"
"log"
//"math"
"mime"
"net/http"
"os"
"path"
//"sort"
"strings"
//"sync"
//"time"
)
type Page struct {
Title string
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
file, err := os.Open(tmpl + ".html")
if err != nil {
return
}
reader := bufio.NewReader(file)
srcString := ""
for {
s, err := reader.ReadString('\n')
if err != nil {
break
}
srcString += s
}
t := template.Must(template.New("template").Delims("*(", ")*").Parse(srcString))
e := t.Execute(w, p)
if e != nil {
log.Fatal(e)
}
}
func serveFile(w http.ResponseWriter, r *http.Request) {
reqURI := strings.Split(r.RequestURI, ".")
w.Header().Set("Content-Type", mime.TypeByExtension("."+reqURI[len(reqURI)-1]))
cwd, err := os.Getwd()
byteArr, err := ioutil.ReadFile(path.Join(cwd, r.RequestURI))
if err != nil {
log.Println(err)
}
w.Write(byteArr)
}
/*func analyzeTextHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", mime.TypeByExtension(".json"))
text := r.FormValue("text")
wordList := sortWordsByQuality(text)
fmt.Println(len(wordList), wordList)
response, _ := json.Marshal(wordList)
w.Write(response)
}*/
func handler(w http.ResponseWriter, r *http.Request) {
r.RequestURI = "main.html"
serveFile(w, r)
}
func main() {
fmt.Println("Opening Server")
http.HandleFunc("/static/", serveFile)
http.HandleFunc("/", handler)
http.ListenAndServe(":80", nil)
}