-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
76 lines (63 loc) · 1.71 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
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"strings"
"github.com/lissy93/who-dat/api"
)
//go:embed dist/*
var staticAssets embed.FS
func main() {
// Create a sub-directory filesystem from the embedded files
subFS, err := fs.Sub(staticAssets, "dist")
if err != nil {
log.Fatal(err)
}
// Create a file server for the sub-directory filesystem
embeddedServer := http.FileServer(http.FS(subFS))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/")
if path == "docs" {
http.ServeFile(w, r, "dist/docs.html")
return
}
// Serve embedded static files for the root
if path == "" {
embeddedServer.ServeHTTP(w, r)
return
}
// Serve embedded static files if path starts with "assets"
if strings.HasPrefix(path, "assets") {
embeddedServer.ServeHTTP(w, r)
return
}
// Handle API requests
// Check if it's a domain lookup or a multi-domain lookup
if path == "multi" {
api.MultiHandler(w, r)
} else {
api.MainHandler(w, r)
}
})
// Choose the port to start server on
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
serverAddress := fmt.Sprintf(":%s", port)
asciiArt := `
__ ___ _____ _ ___
\ \ / / | | __ \ | ||__ \
\ \ /\ / /| |__ ___ | | | | __ _| |_ ) |
\ \/ \/ / | '_ \ / _ \ | | | |/ _` + "`" + ` | __|/ /
\ /\ / | | | | (_) | | |__| | (_| | |_|_|
\/ \/ |_| |_|\___/ |_____/ \__,_|\__(_)
`
log.Println(asciiArt)
log.Printf("\nWelcome to Who-Dat - WHOIS Lookup Service.\nApp up and running at %s", serverAddress)
log.Fatal(http.ListenAndServe(serverAddress, nil))
}