spa-server/main.go

98 lines
2.1 KiB
Go
Raw Normal View History

2014-12-29 20:30:15 +00:00
package main
import (
2023-04-18 01:16:09 +00:00
"context"
"errors"
2014-12-29 20:30:15 +00:00
"log"
2015-01-03 06:01:03 +00:00
"mime"
2023-04-18 01:16:09 +00:00
"net"
2014-12-29 20:30:15 +00:00
"net/http"
"os"
2023-04-18 01:16:09 +00:00
"os/signal"
"path"
2015-01-03 06:01:03 +00:00
"path/filepath"
2023-04-18 01:16:09 +00:00
"time"
2014-12-29 20:30:15 +00:00
)
2023-04-18 01:16:09 +00:00
var baseDirectory string
2014-12-29 20:30:15 +00:00
func handler(w http.ResponseWriter, r *http.Request) {
log.Printf("Incoming request: %s %s", r.Method, r.URL.Path)
2023-04-18 01:16:09 +00:00
filePath := path.Join(baseDirectory, r.URL.Path)
file, err := os.Stat(filePath)
2014-12-29 20:58:43 +00:00
if err == nil && !file.IsDir() {
2014-12-29 20:30:15 +00:00
// file exists
2023-04-18 01:16:09 +00:00
gz := filePath + ".gz"
2015-01-03 06:01:03 +00:00
file, err := os.Stat(gz)
2023-04-18 01:16:09 +00:00
t := mime.TypeByExtension(filepath.Ext(filePath))
2015-01-03 06:01:03 +00:00
if err == nil && !file.IsDir() && t != "" {
w.Header().Add("Content-Encoding", "gzip")
w.Header().Add("Content-Type", t)
http.ServeFile(w, r, gz)
} else {
w.Header().Add("Content-Type", t)
2023-04-18 01:16:09 +00:00
http.ServeFile(w, r, filePath)
2015-01-03 06:01:03 +00:00
}
2014-12-29 20:30:15 +00:00
} else {
2014-12-29 20:58:43 +00:00
// file does not exist
2023-04-18 01:16:09 +00:00
index := path.Join(baseDirectory, "index.html")
2014-12-29 20:58:43 +00:00
file, err := os.Stat(index)
if err == nil && !file.IsDir() {
2014-12-29 20:30:15 +00:00
// index.html exists
w.Header().Add("Content-Type", "text/html")
2014-12-29 20:30:15 +00:00
http.ServeFile(w, r, index)
} else {
2014-12-29 20:58:43 +00:00
// index.html does not exist
2014-12-29 20:30:15 +00:00
http.NotFound(w, r)
}
}
}
func main() {
2023-04-18 01:16:09 +00:00
listeningPort, ok := os.LookupEnv("PORT")
if !ok {
listeningPort = "5050"
}
listeningHost, ok := os.LookupEnv("HOST")
if !ok {
listeningHost = "127.0.0.1"
}
baseDirectory, ok = os.LookupEnv("BASE_DIRECTORY")
if !ok {
baseDirectory = "."
}
router := http.NewServeMux()
router.HandleFunc("/", handler)
2014-12-29 20:41:51 +00:00
2023-04-18 01:16:09 +00:00
server := &http.Server{
Addr: net.JoinHostPort(listeningHost, listeningPort),
Handler: router,
2014-12-29 20:41:51 +00:00
}
2023-04-18 01:16:09 +00:00
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt)
go func() {
<-exitSignal
log.Println("Received shutdown signal, shutting down")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
err := server.Shutdown(ctx)
if err != nil {
log.Printf("Error occured during shutting down HTTP server: %s", err.Error())
}
}()
log.Printf("HTTP server listening on %s", server.Addr)
err := server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Error occured during listening to HTTP server: %s", err.Error())
2014-12-29 20:30:15 +00:00
}
}