64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
// ---------- HTTP Handlers ----------
|
|
|
|
func HelloHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/hello" {
|
|
http.Error(w, "404 not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
if r.Method != "GET" {
|
|
http.Error(w, "method is not supported", http.StatusNotFound)
|
|
return
|
|
}
|
|
fmt.Fprintf(w, "Hello You")
|
|
}
|
|
|
|
func staticPage(path, file string) {
|
|
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, "./static/"+file)
|
|
})
|
|
}
|
|
|
|
// ---------- Main ----------
|
|
|
|
func main() {
|
|
port := flag.String("port", "8081", "port to listen on")
|
|
flag.Parse()
|
|
|
|
fs := http.FileServer(http.Dir("static"))
|
|
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/" {
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
return
|
|
}
|
|
http.ServeFile(w, r, "./static/about.html")
|
|
})
|
|
//http.HandleFunc("/portfolio", Portfolio)
|
|
staticPage("/infra", "infra.html")
|
|
staticPage("/finance", "finance.html")
|
|
staticPage("/cinema", "cinema.html")
|
|
staticPage("/engine", "engine.html")
|
|
staticPage("/42", "42.html")
|
|
staticPage("/secret", "secret.html")
|
|
|
|
//http.HandleFunc("/portfolio", Portfolio)
|
|
http.HandleFunc("/git", func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, "https://git.samantha42.xyz", http.StatusFound)
|
|
})
|
|
|
|
fmt.Printf("running on http://localhost:%s/\n", *port)
|
|
if err := http.ListenAndServe(":"+*port, nil); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|