-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
55 lines (42 loc) · 1.05 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
package main
import (
"embed"
"fmt"
"log"
"net/http"
)
const portNum uint16 = 3389
//go:embed templates/*
var folder embed.FS //embeds files to a virtual filesystem inside the go binary
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
fldr, err := folder.ReadFile("templates/404.html")
if err != nil {
log.Fatal(err)
}
// Write the 404 status page
w.WriteHeader(http.StatusNotFound)
w.Write(fldr)
return
}
fldr, err := folder.ReadFile("templates/index.html")
// Handle the error for the index.html file
if err != nil {
log.Fatal(err)
}
// Write the contents of the index.html file to the response
w.Write(fldr)
})
fs := http.FileServer(http.FS(folder))
http.Handle("/templates/", fs)
fmt.Printf("Active Port:%v\n", portNum)
// Health check
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
err := http.ListenAndServe(fmt.Sprintf(":%v", portNum), nil)
if err != nil {
log.Fatal(err)
}
}