-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (52 loc) · 1.44 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
package main
import (
"io"
"log"
"net/http"
"os"
"github.com/McCooll75/appchad/database"
"github.com/McCooll75/appchad/router"
"github.com/joho/godotenv"
)
func FaviconHandler(w http.ResponseWriter, r *http.Request) {
// Open the favicon.ico file
file, err := os.Open("assets/favicon.ico")
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
defer file.Close()
// Set the appropriate content type for the response
w.Header().Set("Content-Type", "image/x-icon")
// Copy the file's content to the response writer
_, err = io.Copy(w, file)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}
// main
func main() {
// environment
if err := godotenv.Load(); err != nil {
log.Fatal(err)
}
// database
database.InitDatabase()
defer database.Database.Close()
for _, stmt := range database.Statements {
defer stmt.Close()
}
// http
r := router.RouterSetup()
// static files
assetsFileServer := http.FileServer(http.Dir("assets"))
r.Handle("/assets/*", http.StripPrefix("/assets", assetsFileServer))
// uploaded images
imgFileServer := http.FileServer(http.Dir("images/files"))
r.Handle("/img/*", http.StripPrefix("/img", imgFileServer))
// favicon
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./assets/favicon.ico")
})
log.Fatal("error listening:", http.ListenAndServe(":8000", r))
}