-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·47 lines (43 loc) · 989 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
package main
import (
"bufio"
"data_migration/controller"
"log"
"net/http"
"os"
"strings"
)
func routing(mux *http.ServeMux) {
mux.HandleFunc("/", controller.Index)
mux.HandleFunc("/public/", handleAssets)
}
func handleAssets(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
path = strings.ReplaceAll(path, "/public/", "")
f, err := os.Open("./public/" + path)
if err != nil {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Not found!"))
return
}
rd := bufio.NewReader(f)
contentType := "text/plain"
if strings.Contains(path, ".css") {
contentType = "text/css"
}
if strings.Contains(path, ".js") {
contentType = "text/javascript"
}
w.Header().Add("Content-Type", contentType)
rd.WriteTo(w)
}
func main() {
port := "8000"
mux := http.NewServeMux()
routing(mux)
log.Println("Serving at http://localhost:" + port)
err := http.ListenAndServe(":"+port, mux)
if err != nil {
log.Fatalf("Could not start server: %s\n", err.Error())
}
}