2015-02-07 18:32:44 +00:00
package main
import (
"crypto/md5"
"fmt"
"net/http"
2019-05-24 22:03:06 +00:00
"os"
2015-02-07 18:32:44 +00:00
"time"
2015-07-06 20:23:46 +00:00
"github.com/gorilla/mux"
2019-05-24 22:03:06 +00:00
log "github.com/sirupsen/logrus"
2015-02-07 18:32:44 +00:00
2020-10-18 01:34:10 +00:00
httphelper "github.com/Luzifer/go_helpers/v2/http"
2019-05-24 22:03:06 +00:00
"github.com/Luzifer/mondash/storage"
2020-10-18 01:34:10 +00:00
"github.com/Luzifer/rconfig/v2"
2015-02-07 18:32:44 +00:00
)
2015-07-06 19:41:21 +00:00
var (
2019-05-24 22:03:06 +00:00
store storage . Storage
cfg = struct {
APIToken string ` flag:"api-token" env:"API_TOKEN" description:"API Token used for the /welcome dashboard (you can choose your own)" `
2021-05-01 13:14:21 +00:00
BaseURL string ` flag:"baseurl" env:"BASE_URL" default:"http://127.0.0.1:3000" description:"The Base-URL the application is running on for example https://mondash.org" `
2019-05-24 22:03:06 +00:00
FrontendDir string ` flag:"frontend-dir" default:"./frontend" description:"Directory to serve frontend assets from" `
2019-05-24 22:08:04 +00:00
Storage string ` flag:"storage" default:"file:///data" description:"Storage engine to use" `
2019-05-24 22:03:06 +00:00
Listen string ` flag:"listen" default:":3000" description:"Address to listen on" `
LogLevel string ` flag:"log-level" default:"info" description:"Set log level (debug, info, warning, error)" `
VersionAndExit bool ` flag:"version" default:"false" description:"Prints current version and exits" `
} { }
version = "dev"
2015-07-06 19:41:21 +00:00
)
2015-02-07 18:32:44 +00:00
2019-05-24 22:03:06 +00:00
func init ( ) {
rconfig . AutoEnv ( true )
if err := rconfig . ParseAndValidate ( & cfg ) ; err != nil {
log . Fatalf ( "Unable to parse commandline options: %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 ( "share %s\n" , version )
os . Exit ( 0 )
}
}
2015-02-07 18:32:44 +00:00
2019-05-24 22:03:06 +00:00
func main ( ) {
2015-07-06 19:41:21 +00:00
var err error
2019-05-24 22:03:06 +00:00
if store , err = storage . GetStorage ( cfg . Storage ) ; err != nil {
log . WithError ( err ) . Fatal ( "Unable to load storage handler" )
2015-07-06 18:28:44 +00:00
}
2015-02-07 18:32:44 +00:00
2015-07-06 20:23:46 +00:00
r := mux . NewRouter ( )
2019-05-24 22:03:06 +00:00
r . Use ( // Sort: Outermost to innermost wrapper
httphelper . NewHTTPLogHandler ,
httphelper . GzipHandler ,
genericHeader ,
)
2015-07-06 20:23:46 +00:00
r . HandleFunc ( "/" , handleRedirectWelcome ) .
2019-05-24 22:03:06 +00:00
Methods ( http . MethodGet )
r . HandleFunc ( "/app.js" , handleAppJS ) .
Methods ( http . MethodGet )
2015-07-06 20:23:46 +00:00
r . HandleFunc ( "/create" , handleCreateRandomDashboard ) .
2019-05-24 22:03:06 +00:00
Methods ( http . MethodGet )
2016-03-27 20:18:13 +00:00
r . HandleFunc ( "/{dashid}.json" , handleDisplayDashboardJSON ) .
2019-05-24 22:03:06 +00:00
Methods ( http . MethodGet )
2015-07-06 20:23:46 +00:00
r . HandleFunc ( "/{dashid}" , handleDisplayDashboard ) .
2019-05-24 22:03:06 +00:00
Methods ( http . MethodGet )
2015-02-07 18:32:44 +00:00
2015-07-06 20:23:46 +00:00
r . HandleFunc ( "/{dashid}/{metricid}" , handlePutMetric ) .
2019-05-24 22:03:06 +00:00
Methods ( http . MethodPut )
2015-02-07 18:32:44 +00:00
2015-07-06 20:23:46 +00:00
r . HandleFunc ( "/{dashid}" , handleDeleteDashboard ) .
2019-05-24 22:03:06 +00:00
Methods ( http . MethodDelete )
2015-07-06 20:23:46 +00:00
r . HandleFunc ( "/{dashid}/{metricid}" , handleDeleteMetric ) .
2019-05-24 22:03:06 +00:00
Methods ( http . MethodDelete )
2015-02-07 18:32:44 +00:00
2019-05-24 22:03:06 +00:00
go runWelcomePage ( )
2015-02-07 18:32:44 +00:00
2019-05-24 22:03:06 +00:00
if err := http . ListenAndServe ( cfg . Listen , r ) ; err != nil {
log . WithError ( err ) . Fatal ( "HTTP server ended unexpectedly" )
}
2015-02-07 18:32:44 +00:00
}
2019-05-24 22:03:06 +00:00
func genericHeader ( h http . Handler ) http . Handler {
2015-07-06 20:48:54 +00:00
return http . HandlerFunc ( func ( res http . ResponseWriter , r * http . Request ) {
2019-05-24 22:03:06 +00:00
res . Header ( ) . Set ( "X-Application-Version" , version )
h . ServeHTTP ( res , r )
2015-07-06 20:48:54 +00:00
} )
}
2015-02-07 18:32:44 +00:00
func generateAPIKey ( ) string {
t := time . Now ( ) . String ( )
sum := md5 . Sum ( [ ] byte ( t ) )
return fmt . Sprintf ( "%x" , sum )
}