2017-08-03 08:13:53 -04:00
|
|
|
package main
|
|
|
|
|
2017-08-04 15:16:00 -04:00
|
|
|
//go:generate go-bindata -pkg $GOPACKAGE -o assets.go ./frontend/...
|
2017-08-03 12:32:30 -04:00
|
|
|
|
2017-08-03 08:13:53 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-03 12:32:30 -04:00
|
|
|
"mime"
|
2017-08-03 08:13:53 -04:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-08-03 12:32:30 -04:00
|
|
|
"path"
|
|
|
|
"strings"
|
2017-08-04 15:16:00 -04:00
|
|
|
"text/template"
|
2017-08-03 08:13:53 -04:00
|
|
|
|
2017-08-03 12:32:30 -04:00
|
|
|
http_helpers "github.com/Luzifer/go_helpers/http"
|
2017-08-03 08:13:53 -04:00
|
|
|
"github.com/Luzifer/rconfig"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg struct {
|
|
|
|
Listen string `flag:"listen" default:":3000" description:"IP/Port to listen on"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Set log level (debug, info, warning, error)"`
|
|
|
|
StorageType string `flag:"storage-type" default:"mem" description:"Storage to use for putting secrets to" validate:"nonzero"`
|
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Print version information and exit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
product = "ots"
|
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
|
|
|
log.Fatalf("Error parsing CLI arguments: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l, err := log.ParseLevel(cfg.LogLevel); err == nil {
|
|
|
|
log.SetLevel(l)
|
|
|
|
} else {
|
|
|
|
log.Fatalf("Invalid log level: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
|
|
|
fmt.Printf("%s %s\n", product, version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
store, err := getStorageByType(cfg.StorageType)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not initialize storage: %s", err)
|
|
|
|
}
|
|
|
|
api := newAPI(store)
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
api.Register(r.PathPrefix("/api").Subrouter())
|
2017-08-03 12:32:30 -04:00
|
|
|
r.PathPrefix("/").HandlerFunc(assetDelivery)
|
|
|
|
|
|
|
|
log.Fatalf("HTTP server quit: %s", http.ListenAndServe(cfg.Listen, http_helpers.NewHTTPLogHandler(r)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func assetDelivery(res http.ResponseWriter, r *http.Request) {
|
|
|
|
assetName := r.URL.Path
|
|
|
|
if assetName == "/" {
|
|
|
|
assetName = "/index.html"
|
|
|
|
}
|
|
|
|
|
2017-08-03 14:12:15 -04:00
|
|
|
ext := assetName[strings.LastIndex(assetName, "."):]
|
2017-08-03 12:32:30 -04:00
|
|
|
assetData, err := Asset(path.Join("frontend", assetName))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(res, "404 not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2017-08-03 08:13:53 -04:00
|
|
|
|
2017-08-03 12:32:30 -04:00
|
|
|
res.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
2017-08-04 15:16:00 -04:00
|
|
|
|
2018-05-05 12:09:37 -04:00
|
|
|
if assetName != "/index.html" {
|
|
|
|
// Do not use template engine on other files than index.html
|
|
|
|
res.Write(assetData)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-04 15:16:00 -04:00
|
|
|
tpl, err := template.New(assetName).Funcs(getTFuncMap(r)).Parse(string(assetData))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Template for asset %q has an error: %s", assetName, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl.Execute(res, map[string]interface{}{
|
|
|
|
"version": version,
|
|
|
|
})
|
2017-08-03 08:13:53 -04:00
|
|
|
}
|