-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (38 loc) · 1.23 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
func check(err error) {
if err != nil {
panic(err)
}
}
// Init constructs the server and routes. With a bigger project this might be separated into different files and accept config options
func main() {
log.Printf("Server logging started at: %s", time.Now())
ex, err := os.Executable()
check(err)
exPath := filepath.Dir(ex)
mux := mux.NewRouter()
fs := http.FileServer(http.Dir(exPath + string(os.PathSeparator) + "SPA" + string(os.PathSeparator) + "dist"))
mux.PathPrefix("/dist/").Handler(http.StripPrefix("/dist/", fs))
fs = http.FileServer(http.Dir(exPath + string(os.PathSeparator) + "SPA" + string(os.PathSeparator) + "assets"))
mux.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", fs))
genericHandle := func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, exPath+string(os.PathSeparator)+"SPA"+string(os.PathSeparator)+"/index.html")
}
mux.PathPrefix("/").HandlerFunc(genericHandle)
handler := cors.Default().Handler(mux)
fmt.Println("serving")
err = http.ListenAndServe(":8079", handler)
if err != nil {
log.Fatalf("Failed to serve, %s", err.Error())
}
}