Commit 07356613 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Add static file hosting to irma server

parent 934c2f34
...@@ -78,6 +78,8 @@ func setFlags(cmd *cobra.Command) error { ...@@ -78,6 +78,8 @@ func setFlags(cmd *cobra.Command) error {
flags.String("schemes-assets-path", "", "if specified, copy schemes from here into --schemes-path") flags.String("schemes-assets-path", "", "if specified, copy schemes from here into --schemes-path")
flags.Int("schemes-update", 60, "update IRMA schemes every x minutes (0 to disable)") flags.Int("schemes-update", 60, "update IRMA schemes every x minutes (0 to disable)")
flags.StringP("privkeys", "k", "", "path to IRMA private keys") flags.StringP("privkeys", "k", "", "path to IRMA private keys")
flags.String("static-path", "", "Host files under this path as static files (leave empty to disable)")
flags.String("static-prefix", "/", "Host static files under this URL prefix")
flags.StringP("url", "u", defaulturl, "external URL to server to which the IRMA client connects") flags.StringP("url", "u", defaulturl, "external URL to server to which the IRMA client connects")
flags.IntP("port", "p", 8088, "port at which to listen") flags.IntP("port", "p", 8088, "port at which to listen")
...@@ -203,6 +205,8 @@ func configure(cmd *cobra.Command) error { ...@@ -203,6 +205,8 @@ func configure(cmd *cobra.Command) error {
Verbose: viper.GetInt("verbose"), Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"), Quiet: viper.GetBool("quiet"),
LogJSON: viper.GetBool("log-json"), LogJSON: viper.GetBool("log-json"),
StaticPath: viper.GetString("static-path"),
StaticPrefix: viper.GetString("static-prefix"),
TlsCertificate: viper.GetString("tls-cert"), TlsCertificate: viper.GetString("tls-cert"),
TlsCertificateFile: viper.GetString("tls-cert-file"), TlsCertificateFile: viper.GetString("tls-cert-file"),
......
...@@ -60,6 +60,11 @@ type Configuration struct { ...@@ -60,6 +60,11 @@ type Configuration struct {
// Max age in seconds of a session request JWT (using iat field) // Max age in seconds of a session request JWT (using iat field)
MaxRequestAge int `json:"max_request_age" mapstructure:"max_request_age"` MaxRequestAge int `json:"max_request_age" mapstructure:"max_request_age"`
// Host files under this path as static files (leave empty to disable)
StaticPath string `json:"static_path" mapstructure:"static_path"`
// Host static files under this URL prefix
StaticPrefix string `json:"static_prefix" mapstructure:"static_prefix"`
Verbose int `json:"verbose" mapstructure:"verbose"` Verbose int `json:"verbose" mapstructure:"verbose"`
Quiet bool `json:"quiet" mapstructure:"quiet"` Quiet bool `json:"quiet" mapstructure:"quiet"`
LogJSON bool `json:"log_json" mapstructure:"log_json"` LogJSON bool `json:"log_json" mapstructure:"log_json"`
...@@ -212,6 +217,18 @@ func (conf *Configuration) initialize() error { ...@@ -212,6 +217,18 @@ func (conf *Configuration) initialize() error {
return err return err
} }
if conf.StaticPath != "" {
if err := fs.AssertPathExists(conf.StaticPath); err != nil {
return errors.WrapPrefix(err, "Invalid static_path", 0)
}
if conf.StaticPrefix[0] != '/' {
return errors.New("static_prefix must start with a slash, was " + conf.StaticPrefix)
}
if len(conf.StaticPrefix) > 1 && !strings.HasSuffix(conf.StaticPrefix, "/") {
conf.StaticPrefix = conf.StaticPrefix + "/"
}
}
if conf.URL != "" { if conf.URL != "" {
if !strings.HasSuffix(conf.URL, "/") { if !strings.HasSuffix(conf.URL, "/") {
conf.URL = conf.URL + "/" conf.URL = conf.URL + "/"
......
...@@ -11,11 +11,13 @@ import ( ...@@ -11,11 +11,13 @@ import (
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"time" "time"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors" "github.com/go-chi/cors"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/privacybydesign/irmago" "github.com/privacybydesign/irmago"
...@@ -125,6 +127,19 @@ func (s *Server) ClientHandler() http.Handler { ...@@ -125,6 +127,19 @@ func (s *Server) ClientHandler() http.Handler {
router.Use(cors.New(corsOptions).Handler) router.Use(cors.New(corsOptions).Handler)
router.Mount("/irma/", s.irmaserv.HandlerFunc()) router.Mount("/irma/", s.irmaserv.HandlerFunc())
if s.conf.StaticPath != "" {
url := s.conf.URL[:len(s.conf.URL)-6] + s.conf.StaticPrefix
s.conf.Logger.Infof("Hosting files at %s under %s", s.conf.StaticPath, url)
middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{
Logger: log.New(s.conf.Logger.WriterLevel(logrus.TraceLevel), "static: ", 0),
NoColor: true,
})
router.Mount(s.conf.StaticPrefix,
http.StripPrefix(s.conf.StaticPrefix, middleware.Logger(http.FileServer(http.Dir(s.conf.StaticPath)))),
)
}
return router return router
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment