Skip to content
Snippets Groups Projects
Commit e78b4ce2 authored by meskio's avatar meskio :tent:
Browse files

Serve properly a single page app

parent fad4ff8c
No related branches found
No related tags found
No related merge requests found
...@@ -4,12 +4,38 @@ import ( ...@@ -4,12 +4,38 @@ import (
"flag" "flag"
"log" "log"
"net/http" "net/http"
"os"
"path/filepath"
"0xacab.org/meskio/cicer/api" "0xacab.org/meskio/cicer/api"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/olivere/env" "github.com/olivere/env"
) )
type handler struct {
assets string
}
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path, err := filepath.Abs(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
path = filepath.Join(h.assets, path)
_, err = os.Stat(path)
if os.IsNotExist(err) {
http.ServeFile(w, r, h.assets)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.FileServer(http.Dir(h.assets)).ServeHTTP(w, r)
}
func main() { func main() {
var ( var (
dbPath = flag.String("db-path", env.String("./test.db", "DB_PATH"), "Path where the sqlite will be located {DB_PATH}") dbPath = flag.String("db-path", env.String("./test.db", "DB_PATH"), "Path where the sqlite will be located {DB_PATH}")
...@@ -28,6 +54,7 @@ func main() { ...@@ -28,6 +54,7 @@ func main() {
} }
log.Println("assets:", *assets) log.Println("assets:", *assets)
r.PathPrefix("/").Handler(http.FileServer(http.Dir(*assets))) h := handler{*assets}
r.PathPrefix("/").Handler(h)
log.Fatal(http.ListenAndServe(*addr, r)) log.Fatal(http.ListenAndServe(*addr, r))
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment