-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.go
43 lines (36 loc) · 856 Bytes
/
hello.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
package main
import (
"html/template"
"log"
"net/http"
"os"
"path/filepath"
)
var (
indexTmpl = template.Must(
template.ParseFiles(filepath.Join("portfolio", "index.html")),
)
)
func main() {
http.HandleFunc("/", indexHandler)
public := http.StripPrefix("/portfolio", http.FileServer(http.Dir("portfolio")))
http.Handle("/portfolio/", public)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
if err := indexTmpl.Execute(w, struct {}{}); err != nil {
log.Printf("Error executing template: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}