change(backend): serve static file from /static and serve index.html for /
This commit is contained in:
parent
ea0a76ff7d
commit
b66d655780
6 changed files with 32 additions and 9 deletions
|
@ -12,6 +12,7 @@ type config struct {
|
|||
redisUser string
|
||||
redisPassword string
|
||||
redisDB int
|
||||
urlLength int
|
||||
}
|
||||
|
||||
func setConfig() config {
|
||||
|
@ -33,6 +34,10 @@ func setConfig() config {
|
|||
redisDB = 0
|
||||
}
|
||||
|
||||
urlLength, err := strconv.Atoi("PLAKKEN_URL_LENGTH")
|
||||
if err != nil {
|
||||
urlLength = 3
|
||||
}
|
||||
s := config{
|
||||
host: host,
|
||||
port: port,
|
||||
|
@ -40,6 +45,7 @@ func setConfig() config {
|
|||
redisUser: redisUser,
|
||||
redisPassword: redisPassword,
|
||||
redisDB: redisDB,
|
||||
urlLength: urlLength,
|
||||
}
|
||||
|
||||
return s
|
||||
|
|
6
go.mod
6
go.mod
|
@ -1,3 +1,9 @@
|
|||
module plakken
|
||||
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/redis/go-redis/v9 v9.2.1 // indirect
|
||||
)
|
||||
|
|
29
main.go
29
main.go
|
@ -1,19 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
currentConfig := setConfig()
|
||||
listen := currentConfig.host + ":" + currentConfig.port
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := fmt.Fprintf(w, "Hello, you're at %s", r.URL.Path)
|
||||
if err != nil {
|
||||
return
|
||||
var currentConfig config
|
||||
|
||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
if path == "/" {
|
||||
http.ServeFile(w, r, "./static/index.html")
|
||||
|
||||
} else if strings.HasPrefix(path, "/static/") {
|
||||
fs := http.FileServer(http.Dir("./static"))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
currentConfig = setConfig()
|
||||
listen := currentConfig.host + ":" + currentConfig.port
|
||||
http.HandleFunc("/", handleRequest)
|
||||
|
||||
err := http.ListenAndServe(listen, nil)
|
||||
if err != nil {
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
name="viewport">
|
||||
<meta content="ie=edge" http-equiv="X-UA-Compatible">
|
||||
<title>Your plak • Plakken</title>
|
||||
<link href="style.css" rel="stylesheet">
|
||||
<link href="/static/style.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<label for="content"></label>
|
Loading…
Reference in a new issue