faet: more configurable options

This commit is contained in:
Reinaldy Rafli 2023-04-18 08:16:09 +07:00
parent 0c4498ccee
commit 4a4552b944
Signed by: aldy505
GPG Key ID: A3F8A7E23DA2AD94
11 changed files with 124 additions and 23 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -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

View File

@ -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>

View File

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

6
.idea/misc.xml Normal file
View File

@ -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>

8
.idea/modules.xml Normal file
View File

@ -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>

10
.idea/spa-server.iml Normal file
View File

@ -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>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -3,6 +3,21 @@ spa-server
Simple static file server for single-page application 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 ```sh
$ tree $ tree
. .

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module spa-server
go 1.20

0
go.sum Normal file
View File

79
main.go
View File

@ -1,61 +1,94 @@
package main package main
import ( import (
"flag" "context"
"fmt" "errors"
"log" "log"
"mime" "mime"
"net"
"net/http" "net/http"
"os" "os"
"os/signal"
"path"
"path/filepath" "path/filepath"
"time"
) )
var baseDirectory string
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
path := "." + r.URL.Path filePath := path.Join(baseDirectory, r.URL.Path)
fmt.Print(r.URL.Path) file, err := os.Stat(filePath)
file, err := os.Stat(path)
if err == nil && !file.IsDir() { if err == nil && !file.IsDir() {
// file exists // file exists
gz := path + ".gz" gz := filePath + ".gz"
file, err := os.Stat(gz) file, err := os.Stat(gz)
t := mime.TypeByExtension(filepath.Ext(path)) t := mime.TypeByExtension(filepath.Ext(filePath))
if err == nil && !file.IsDir() && t != "" { if err == nil && !file.IsDir() && t != "" {
fmt.Println(" => " + gz)
w.Header().Add("Content-Encoding", "gzip") w.Header().Add("Content-Encoding", "gzip")
w.Header().Add("Content-Type", t) w.Header().Add("Content-Type", t)
http.ServeFile(w, r, gz) http.ServeFile(w, r, gz)
} else { } else {
fmt.Println(" => " + path) http.ServeFile(w, r, filePath)
http.ServeFile(w, r, path)
} }
} else { } else {
// file does not exist // file does not exist
index := "index.html" index := path.Join(baseDirectory, "index.html")
file, err := os.Stat(index) file, err := os.Stat(index)
if err == nil && !file.IsDir() { if err == nil && !file.IsDir() {
// index.html exists // index.html exists
fmt.Println(" => " + index)
http.ServeFile(w, r, index) http.ServeFile(w, r, index)
} else { } else {
// index.html does not exist // index.html does not exist
fmt.Println(" => NotFound")
http.NotFound(w, r) http.NotFound(w, r)
} }
} }
} }
func main() { func main() {
listeningPort, ok := os.LookupEnv("PORT")
port := ":5050" if !ok {
flag.Parse() listeningPort = "5050"
if flag.NArg() != 0 {
port = ":" + flag.Arg(0)
} }
fmt.Println("spa-server starting on localhost" + port)
http.HandleFunc("/", handler) listeningHost, ok := os.LookupEnv("HOST")
err := http.ListenAndServe(port, nil) if !ok {
if err != nil { listeningHost = "127.0.0.1"
log.Fatal("spa-server: ", err) }
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())
} }
} }