faet: more configurable options
This commit is contained in:
parent
0c4498ccee
commit
4a4552b944
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
|
@ -0,0 +1,7 @@
|
|||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<ScalaCodeStyleSettings>
|
||||
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
|
||||
</ScalaCodeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
|
@ -0,0 +1,5 @@
|
|||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||
</state>
|
||||
</component>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/spa-server.iml" filepath="$PROJECT_DIR$/.idea/spa-server.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
15
README.md
15
README.md
|
@ -3,6 +3,21 @@ spa-server
|
|||
|
||||
Simple static file server for single-page application
|
||||
|
||||
This fork introduces a bunch of configuration and code cleanup. You can specify a custom listening hostname and port,
|
||||
as well as custom base directory for the files. It is as simple as:
|
||||
|
||||
```sh
|
||||
PORT=3000 HOST=127.0.0.1 BASE_DIRECTORY=/home/ubuntu/application spa-server
|
||||
```
|
||||
|
||||
Please build the binary yourself.
|
||||
|
||||
```sh
|
||||
# Assuming you already have Go
|
||||
go build .
|
||||
./spa-server
|
||||
```
|
||||
|
||||
```sh
|
||||
$ tree
|
||||
.
|
||||
|
|
79
main.go
79
main.go
|
@ -1,61 +1,94 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"mime"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
var baseDirectory string
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
path := "." + r.URL.Path
|
||||
fmt.Print(r.URL.Path)
|
||||
file, err := os.Stat(path)
|
||||
filePath := path.Join(baseDirectory, r.URL.Path)
|
||||
file, err := os.Stat(filePath)
|
||||
if err == nil && !file.IsDir() {
|
||||
// file exists
|
||||
gz := path + ".gz"
|
||||
gz := filePath + ".gz"
|
||||
file, err := os.Stat(gz)
|
||||
t := mime.TypeByExtension(filepath.Ext(path))
|
||||
t := mime.TypeByExtension(filepath.Ext(filePath))
|
||||
if err == nil && !file.IsDir() && t != "" {
|
||||
fmt.Println(" => " + gz)
|
||||
w.Header().Add("Content-Encoding", "gzip")
|
||||
w.Header().Add("Content-Type", t)
|
||||
http.ServeFile(w, r, gz)
|
||||
} else {
|
||||
fmt.Println(" => " + path)
|
||||
http.ServeFile(w, r, path)
|
||||
http.ServeFile(w, r, filePath)
|
||||
}
|
||||
} else {
|
||||
// file does not exist
|
||||
index := "index.html"
|
||||
index := path.Join(baseDirectory, "index.html")
|
||||
file, err := os.Stat(index)
|
||||
if err == nil && !file.IsDir() {
|
||||
// index.html exists
|
||||
fmt.Println(" => " + index)
|
||||
http.ServeFile(w, r, index)
|
||||
} else {
|
||||
// index.html does not exist
|
||||
fmt.Println(" => NotFound")
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
port := ":5050"
|
||||
flag.Parse()
|
||||
if flag.NArg() != 0 {
|
||||
port = ":" + flag.Arg(0)
|
||||
listeningPort, ok := os.LookupEnv("PORT")
|
||||
if !ok {
|
||||
listeningPort = "5050"
|
||||
}
|
||||
fmt.Println("spa-server starting on localhost" + port)
|
||||
|
||||
http.HandleFunc("/", handler)
|
||||
err := http.ListenAndServe(port, nil)
|
||||
if err != nil {
|
||||
log.Fatal("spa-server: ", err)
|
||||
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)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: net.JoinHostPort(listeningHost, listeningPort),
|
||||
Handler: router,
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue