-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (42 loc) · 941 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func homepage(w http.ResponseWriter, r *http.Request) {
index, err := ioutil.ReadFile("./index.html")
if err != nil {
fmt.Fprintf(w, "error in reading index: %v\n", err)
return
}
//it's an odd issue, I don't know why this happen
w.Header().Add("Content-Type", "text/html")
fmt.Fprintf(w, string(index))
}
type config struct {
Port string
}
func readPortFromFile() string {
dat, err := ioutil.ReadFile("./config.json")
if err != nil {
panic(err)
}
conf := config{Port: ":80"}
err = json.Unmarshal(dat, &conf)
if err != nil {
panic(err)
}
return conf.Port
}
func main() {
fmt.Printf("starting...\n")
port := readPortFromFile()
http.HandleFunc("/", homepage)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
err := http.ListenAndServe(port, nil)
if err != nil {
fmt.Printf("%v", err)
}
}