-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
36 lines (30 loc) · 769 Bytes
/
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
package main
import (
"embed"
"fmt"
"github.com/julienschmidt/httprouter"
"io/fs"
"log"
"net/http"
"time"
)
//go:embed static/*
var staticFS embed.FS
func main() {
f := fs.FS(staticFS)
v, _ := fs.Sub(f, "static")
router := httprouter.New()
router.ServeFiles(fmt.Sprintf("%s", "/static/*filepath"), http.FS(v))
router.HandlerFunc(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/img/cat1.jpg", http.StatusTemporaryRedirect)
})
port := "8080"
httpServer := &http.Server{
Addr: ":" + port,
Handler: router,
ReadTimeout: 120 * time.Second,
WriteTimeout: 120 * time.Second,
}
log.Printf("Server running at http://localhost:%s/\n", port)
log.Fatal(httpServer.ListenAndServe())
}