dashboard: handle correctly baseurl option
On the first public.html GET, load the index.html file, as Go Template.
This commit is contained in:
parent
ca891cd9b2
commit
ded583008a
3 changed files with 46 additions and 22 deletions
|
@ -66,7 +66,7 @@ func StripPrefix(prefix string, h http.Handler) http.Handler {
|
||||||
func main() {
|
func main() {
|
||||||
// Read parameters from command line
|
// Read parameters from command line
|
||||||
var bind = flag.String("bind", "127.0.0.1:8082", "Bind port/socket")
|
var bind = flag.String("bind", "127.0.0.1:8082", "Bind port/socket")
|
||||||
var baseURL = flag.String("baseurl", "/", "URL prepended to each URL")
|
flag.StringVar(&BaseURL, "baseurl", BaseURL, "URL prepended to each URL")
|
||||||
flag.StringVar(&StaticDir, "static", "./htdocs-dashboard/", "Directory containing static files")
|
flag.StringVar(&StaticDir, "static", "./htdocs-dashboard/", "Directory containing static files")
|
||||||
flag.StringVar(&fic.FilesDir, "files", fic.FilesDir, "Base directory where found challenges files, local part")
|
flag.StringVar(&fic.FilesDir, "files", fic.FilesDir, "Base directory where found challenges files, local part")
|
||||||
flag.StringVar(&DashboardDir, "dashbord", "./DASHBOARD", "Base directory where save public JSON files")
|
flag.StringVar(&DashboardDir, "dashbord", "./DASHBOARD", "Base directory where save public JSON files")
|
||||||
|
@ -90,12 +90,10 @@ func main() {
|
||||||
if settings.SettingsDir, err = filepath.Abs(settings.SettingsDir); err != nil {
|
if settings.SettingsDir, err = filepath.Abs(settings.SettingsDir); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if *baseURL != "/" {
|
if BaseURL != "/" {
|
||||||
tmp := path.Clean(*baseURL)
|
BaseURL = path.Clean(BaseURL)
|
||||||
baseURL = &tmp
|
|
||||||
} else {
|
} else {
|
||||||
tmp := ""
|
BaseURL = ""
|
||||||
baseURL = &tmp
|
|
||||||
}
|
}
|
||||||
if fwdr != nil && len(*fwdr) > 0 {
|
if fwdr != nil && len(*fwdr) > 0 {
|
||||||
forwarder = fwdr
|
forwarder = fwdr
|
||||||
|
@ -107,7 +105,7 @@ func main() {
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: *bind,
|
Addr: *bind,
|
||||||
Handler: StripPrefix(*baseURL, api.Router()),
|
Handler: StripPrefix(BaseURL, api.Router()),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve content
|
// Serve content
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -14,16 +19,37 @@ import (
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var BaseURL = "/"
|
||||||
var forwarder *string = nil
|
var forwarder *string = nil
|
||||||
var fwdPublicJson = false
|
var fwdPublicJson = false
|
||||||
|
|
||||||
|
var indexTmpl []byte
|
||||||
|
|
||||||
|
func getIndexHtml(w io.Writer) {
|
||||||
|
if len(indexTmpl) == 0 {
|
||||||
|
if file, err := os.Open(path.Join(StaticDir, "index.html")); err != nil {
|
||||||
|
log.Println("Unable to open index.html: ", err)
|
||||||
|
} else {
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
if indexTmpl, err = ioutil.ReadAll(file); err != nil {
|
||||||
|
log.Println("Cannot read whole index.html: ", err)
|
||||||
|
} else {
|
||||||
|
indexTmpl = bytes.Replace(indexTmpl, []byte("{{.urlbase}}"), []byte(path.Clean(path.Join(BaseURL+"/", "nuke"))[:len(path.Clean(path.Join(BaseURL+"/", "nuke")))-4]), -1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write(indexTmpl)
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
http.Redirect(w, r, "public0.html", http.StatusFound)
|
||||||
})
|
})
|
||||||
for i := 0; i <= 9; i++ {
|
for i := 0; i <= 9; i++ {
|
||||||
api.Router().GET(fmt.Sprintf("/public%d.html", i), func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
api.Router().GET(fmt.Sprintf("/public%d.html", i), func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
getIndexHtml(w)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,11 +8,11 @@
|
||||||
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
|
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
|
||||||
<meta name="author" content="EPITA Laboratoire SRS">
|
<meta name="author" content="EPITA Laboratoire SRS">
|
||||||
<meta name="robots" content="all">
|
<meta name="robots" content="all">
|
||||||
<base href="/">
|
<base href="{{.urlbase}}">
|
||||||
<link href="/css/bootstrap.min.css" type="text/css" rel="stylesheet" media="screen">
|
<link href="{{.urlbase}}/css/bootstrap.min.css" type="text/css" rel="stylesheet" media="screen">
|
||||||
<link href="/css/glyphicon.css" type="text/css" rel="stylesheet" media="screen">
|
<link href="{{.urlbase}}/css/glyphicon.css" type="text/css" rel="stylesheet" media="screen">
|
||||||
<link href="/css/fic.css" type="text/css" rel="stylesheet" media="screen">
|
<link href="{{.urlbase}}/css/fic.css" type="text/css" rel="stylesheet" media="screen">
|
||||||
<script src="/js/angular.min.js"></script>
|
<script src="{{.urlbase}}/js/angular.min.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body class="bg-light bg-public" style="overflow: hidden;" class="container-fluid" ng-controller="DataController">
|
<body class="bg-light bg-public" style="overflow: hidden;" class="container-fluid" ng-controller="DataController">
|
||||||
<div class="row" style="margin:10px 0">
|
<div class="row" style="margin:10px 0">
|
||||||
|
@ -554,13 +554,13 @@
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="/js/jquery.min.js"></script>
|
<script src="{{.urlbase}}/js/jquery.min.js"></script>
|
||||||
<script src="/js/bootstrap.min.js"></script>
|
<script src="{{.urlbase}}/js/bootstrap.min.js"></script>
|
||||||
<script src="/js/angular-animate.min.js"></script>
|
<script src="{{.urlbase}}/js/angular-animate.min.js"></script>
|
||||||
<script src="/js/angular-route.min.js"></script>
|
<script src="{{.urlbase}}/js/angular-route.min.js"></script>
|
||||||
<script src="/js/angular-sanitize.min.js"></script>
|
<script src="{{.urlbase}}/js/angular-sanitize.min.js"></script>
|
||||||
<script src="/js/i18n/angular-locale_fr-fr.js"></script>
|
<script src="{{.urlbase}}/js/i18n/angular-locale_fr-fr.js"></script>
|
||||||
<script src="/js/dashboard.js"></script>
|
<script src="{{.urlbase}}/js/dashboard.js"></script>
|
||||||
<script src="/js/common.js"></script>
|
<script src="{{.urlbase}}/js/common.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Reference in a new issue