pull/49/head v0.44
Hunter Long 2018-08-16 13:55:30 -07:00
parent 60eaab83b4
commit 9ea644ab40
16 changed files with 88 additions and 87 deletions

View File

@ -1,4 +1,4 @@
VERSION=0.43 VERSION=0.44
BINARY_NAME=statup BINARY_NAME=statup
GOPATH:=$(GOPATH) GOPATH:=$(GOPATH)
GOCMD=go GOCMD=go
@ -11,6 +11,7 @@ BUILDVERSION=-ldflags "-X main.VERSION=$(VERSION) -X main.COMMIT=$(TRAVIS_COMMIT
RICE=$(GOPATH)/bin/rice RICE=$(GOPATH)/bin/rice
PATH:=/usr/local/bin:$(GOPATH)/bin:$(PATH) PATH:=/usr/local/bin:$(GOPATH)/bin:$(PATH)
PUBLISH_BODY='{ "request": { "branch": "master", "config": { "env": { "VERSION": "$(VERSION)" } } } }' PUBLISH_BODY='{ "request": { "branch": "master", "config": { "env": { "VERSION": "$(VERSION)" } } } }'
TEST_DIR=$(GOPATH)/src/github.com/hunterlong/statup
all: deps compile install clean all: deps compile install clean
@ -31,7 +32,7 @@ compile:
sass source/scss/base.scss source/css/base.css sass source/scss/base.scss source/css/base.css
test: clean compile install test: clean compile install
go test -v -p=1 $(BUILDVERSION) -coverprofile=coverage.out ./... STATUP_DIR=$(TEST_DIR) GO_ENV=test go test -v -p=1 $(BUILDVERSION) -coverprofile=coverage.out ./...
gocov convert coverage.out > coverage.json gocov convert coverage.out > coverage.json
test-all: compile databases test-all: compile databases
@ -84,7 +85,6 @@ databases:
sleep 30 sleep 30
deps: deps:
$(GOGET) github.com/wellington/wellington/wt
$(GOGET) github.com/stretchr/testify/assert $(GOGET) github.com/stretchr/testify/assert
$(GOGET) golang.org/x/tools/cmd/cover $(GOGET) golang.org/x/tools/cmd/cover
$(GOGET) github.com/mattn/goveralls $(GOGET) github.com/mattn/goveralls
@ -103,24 +103,17 @@ deps:
$(GOGET) -d ./... $(GOGET) -d ./...
clean: clean:
rm -rf build rm -rf ./{logs,assets,plugins,statup.db,config.yml,.sass-cache,config.yml,statup,build}
rm -f statup rm -rf cmd/{logs,assets,plugins,statup.db,config.yml,.sass-cache}
rm -rf logs rm -rf core/{logs,assets,plugins,statup.db,config.yml,.sass-cache}
rm -rf cmd/logs rm -rf handlers/{logs,assets,plugins,statup.db,config.yml,.sass-cache}
rm -rf cmd/plugins rm -rf notifiers/{logs,assets,plugins,statup.db,config.yml,.sass-cache}
rm -rf cmd/statup.db rm -rf source/{logs,assets,plugins,statup.db,config.yml,.sass-cache}
rm -rf cmd/config.yml rm -rf types/{logs,assets,plugins,statup.db,config.yml,.sass-cache}
rm -rf cmd/.sass-cache rm -rf utils/{logs,assets,plugins,statup.db,config.yml,.sass-cache}
rm -rf core/logs
rm -rf core/.sass-cache
rm -rf core/config.yml
rm -f core/statup.db
rm -rf handlers/config.yml
rm -rf handlers/statup.db
rm -rf source/logs
rm -rf utils/logs
rm -rf .sass-cache rm -rf .sass-cache
rm -f coverage.out rm -f coverage.out
rm -f coverage.json
tag: tag:
git tag "v$(VERSION)" --force git tag "v$(VERSION)" --force

View File

@ -72,7 +72,6 @@ func main() {
LoadDotEnvs() LoadDotEnvs()
utils.Log(1, fmt.Sprintf("Starting Statup v%v", VERSION)) utils.Log(1, fmt.Sprintf("Starting Statup v%v", VERSION))
source.HasAssets(directory)
core.Configs, err = core.LoadConfig() core.Configs, err = core.LoadConfig()
if err != nil { if err != nil {

View File

@ -77,7 +77,7 @@ func TestRunAll(t *testing.T) {
RunDatabaseMigrations(t, dbt) RunDatabaseMigrations(t, dbt)
}) })
t.Run(dbt+" Sample Data", func(t *testing.T) { t.Run(dbt+" Sample Data", func(t *testing.T) {
RunInsertMysqlSample(t) RunInsertSampleData(t)
}) })
t.Run(dbt+" Load Configs", func(t *testing.T) { t.Run(dbt+" Load Configs", func(t *testing.T) {
RunLoadConfig(t) RunLoadConfig(t)
@ -180,7 +180,7 @@ func TestRunAll(t *testing.T) {
RunSettingsHandler(t) RunSettingsHandler(t)
}) })
t.Run(dbt+" Cleanup", func(t *testing.T) { t.Run(dbt+" Cleanup", func(t *testing.T) {
Cleanup(t) //Cleanup(t)
}) })
} }
@ -230,7 +230,7 @@ func RunDatabaseMigrations(t *testing.T, db string) {
assert.Nil(t, err) assert.Nil(t, err)
} }
func RunInsertMysqlSample(t *testing.T) { func RunInsertSampleData(t *testing.T) {
err := core.LoadSampleData() err := core.LoadSampleData()
assert.Nil(t, err) assert.Nil(t, err)
} }

View File

@ -88,25 +88,25 @@ func UpdateCore(c *Core) (*Core, error) {
} }
func (c Core) UsingAssets() bool { func (c Core) UsingAssets() bool {
return source.UsingAssets return source.UsingAssets(utils.Directory)
} }
func (c Core) SassVars() string { func (c Core) SassVars() string {
if !source.UsingAssets { if !source.UsingAssets(utils.Directory) {
return "" return ""
} }
return source.OpenAsset(utils.Directory, "scss/variables.scss") return source.OpenAsset(utils.Directory, "scss/variables.scss")
} }
func (c Core) BaseSASS() string { func (c Core) BaseSASS() string {
if !source.UsingAssets { if !source.UsingAssets(utils.Directory) {
return "" return ""
} }
return source.OpenAsset(utils.Directory, "scss/base.scss") return source.OpenAsset(utils.Directory, "scss/base.scss")
} }
func (c Core) MobileSASS() string { func (c Core) MobileSASS() string {
if !source.UsingAssets { if !source.UsingAssets(utils.Directory) {
return "" return ""
} }
return source.OpenAsset(utils.Directory, "scss/mobile.scss") return source.OpenAsset(utils.Directory, "scss/mobile.scss")

View File

@ -41,6 +41,9 @@ func DashboardHandler(w http.ResponseWriter, r *http.Request) {
} }
func LoginHandler(w http.ResponseWriter, r *http.Request) { func LoginHandler(w http.ResponseWriter, r *http.Request) {
if Store == nil {
resetCookies()
}
session, _ := Store.Get(r, COOKIE_KEY) session, _ := Store.Get(r, COOKIE_KEY)
r.ParseForm() r.ParseForm()
username := r.PostForm.Get("username") username := r.PostForm.Get("username")

View File

@ -34,6 +34,7 @@ const (
var ( var (
Store *sessions.CookieStore Store *sessions.CookieStore
httpServer *http.Server
) )
func RunHTTPServer(ip string, port int) error { func RunHTTPServer(ip string, port int) error {
@ -43,18 +44,20 @@ func RunHTTPServer(ip string, port int) error {
// info := p.GetInfo() // info := p.GetInfo()
// for _, route := range p.Routes() { // for _, route := range p.Routes() {
// path := fmt.Sprintf("%v", route.URL) // path := fmt.Sprintf("%v", route.URL)
// r.Handle(path, http.HandlerFunc(route.Handler)).Methods(route.Method) // router.Handle(path, http.HandlerFunc(route.Handler)).Methods(route.Method)
// utils.Log(1, fmt.Sprintf("Added Route %v for plugin %v\n", path, info.Name)) // utils.Log(1, fmt.Sprintf("Added Route %v for plugin %v\n", path, info.Name))
// } // }
//} //}
srv := &http.Server{ router = Router()
httpServer = &http.Server{
Addr: host, Addr: host,
WriteTimeout: time.Second * 15, WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15, ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60, IdleTimeout: time.Second * 60,
Handler: Router(), Handler: router,
} }
return srv.ListenAndServe() resetCookies()
return httpServer.ListenAndServe()
} }
func IsAuthenticated(r *http.Request) bool { func IsAuthenticated(r *http.Request) bool {

View File

@ -28,9 +28,14 @@ import (
"testing" "testing"
) )
var (
dir string
)
func init() { func init() {
utils.InitLogs() utils.InitLogs()
source.Assets() source.Assets()
dir = utils.Directory
} }
func IsRouteAuthenticated(req *http.Request) bool { func IsRouteAuthenticated(req *http.Request) bool {
@ -462,7 +467,7 @@ func TestSaveAssetsHandler(t *testing.T) {
assert.FileExists(t, utils.Directory+"/assets/css/base.css") assert.FileExists(t, utils.Directory+"/assets/css/base.css")
assert.FileExists(t, utils.Directory+"/assets/js/main.js") assert.FileExists(t, utils.Directory+"/assets/js/main.js")
assert.DirExists(t, utils.Directory+"/assets") assert.DirExists(t, utils.Directory+"/assets")
assert.True(t, source.UsingAssets) assert.True(t, source.UsingAssets(dir))
assert.True(t, IsRouteAuthenticated(req)) assert.True(t, IsRouteAuthenticated(req))
} }
@ -472,7 +477,7 @@ func TestDeleteAssetsHandler(t *testing.T) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req) Router().ServeHTTP(rr, req)
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.False(t, source.UsingAssets) assert.False(t, source.UsingAssets(dir))
assert.True(t, IsRouteAuthenticated(req)) assert.True(t, IsRouteAuthenticated(req))
} }

View File

@ -34,7 +34,7 @@ func PluginSavedHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
r.ParseForm() r.ParseForm()
//vars := mux.Vars(r) //vars := mux.Vars(router)
//plug := SelectPlugin(vars["name"]) //plug := SelectPlugin(vars["name"])
data := make(map[string]string) data := make(map[string]string)
for k, v := range r.PostForm { for k, v := range r.PostForm {
@ -50,7 +50,7 @@ func PluginsDownloadHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
return return
} }
//vars := mux.Vars(r) //vars := mux.Vars(router)
//name := vars["name"] //name := vars["name"]
//DownloadPlugin(name) //DownloadPlugin(name)
core.LoadConfig() core.LoadConfig()

View File

@ -21,18 +21,33 @@ import (
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
"github.com/hunterlong/statup/core" "github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/source" "github.com/hunterlong/statup/source"
"github.com/hunterlong/statup/utils"
"net/http" "net/http"
"time" "time"
) )
var ( var (
r *mux.Router router *mux.Router
) )
func Router() *mux.Router { func Router() *mux.Router {
r = mux.NewRouter() dir := utils.Directory
r := mux.NewRouter()
r.Handle("/", http.HandlerFunc(IndexHandler)) r.Handle("/", http.HandlerFunc(IndexHandler))
LocalizedAssets(r) if source.UsingAssets(dir) {
indexHandler := http.FileServer(http.Dir(dir + "/assets/"))
r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(http.Dir(dir+"/assets/css"))))
r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(http.Dir(dir+"/assets/js"))))
r.PathPrefix("/robots.txt").Handler(indexHandler)
r.PathPrefix("/favicon.ico").Handler(indexHandler)
r.PathPrefix("/statup.png").Handler(indexHandler)
} else {
r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(source.CssBox.HTTPBox())))
r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(source.JsBox.HTTPBox())))
r.PathPrefix("/robots.txt").Handler(http.FileServer(source.TmplBox.HTTPBox()))
r.PathPrefix("/favicon.ico").Handler(http.FileServer(source.TmplBox.HTTPBox()))
r.PathPrefix("/statup.png").Handler(http.FileServer(source.TmplBox.HTTPBox()))
}
r.Handle("/charts.js", http.HandlerFunc(RenderServiceChartsHandler)) r.Handle("/charts.js", http.HandlerFunc(RenderServiceChartsHandler))
r.Handle("/setup", http.HandlerFunc(SetupHandler)).Methods("GET") r.Handle("/setup", http.HandlerFunc(SetupHandler)).Methods("GET")
r.Handle("/setup", http.HandlerFunc(ProcessSetupHandler)).Methods("POST") r.Handle("/setup", http.HandlerFunc(ProcessSetupHandler)).Methods("POST")
@ -74,31 +89,19 @@ func Router() *mux.Router {
r.Handle("/api/users/{id}", http.HandlerFunc(ApiUserHandler)) r.Handle("/api/users/{id}", http.HandlerFunc(ApiUserHandler))
r.Handle("/metrics", http.HandlerFunc(PrometheusHandler)) r.Handle("/metrics", http.HandlerFunc(PrometheusHandler))
r.NotFoundHandler = http.HandlerFunc(Error404Handler) r.NotFoundHandler = http.HandlerFunc(Error404Handler)
return r
}
func resetRouter() {
router = Router()
httpServer.Handler = router
}
func resetCookies() {
if core.CoreApp != nil { if core.CoreApp != nil {
cookie := fmt.Sprintf("%v_%v", core.CoreApp.ApiSecret, time.Now().Nanosecond()) cookie := fmt.Sprintf("%v_%v", core.CoreApp.ApiSecret, time.Now().Nanosecond())
Store = sessions.NewCookieStore([]byte(cookie)) Store = sessions.NewCookieStore([]byte(cookie))
} else { } else {
Store = sessions.NewCookieStore([]byte("secretinfo")) Store = sessions.NewCookieStore([]byte("secretinfo"))
} }
return r
}
func LocalizedAssets(r *mux.Router) *mux.Router {
if source.UsingAssets {
cssHandler := http.FileServer(http.Dir("./assets/css"))
jsHandler := http.FileServer(http.Dir("./assets/js"))
indexHandler := http.FileServer(http.Dir("./assets/"))
r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", cssHandler))
r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", jsHandler))
r.PathPrefix("/robots.txt").Handler(indexHandler)
r.PathPrefix("/favicon.ico").Handler(indexHandler)
r.PathPrefix("/statup.png").Handler(indexHandler)
} else {
r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(source.CssBox.HTTPBox())))
r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(source.JsBox.HTTPBox())))
r.PathPrefix("/robots.txt").Handler(http.FileServer(source.TmplBox.HTTPBox()))
r.PathPrefix("/favicon.ico").Handler(http.FileServer(source.TmplBox.HTTPBox()))
r.PathPrefix("/statup.png").Handler(http.FileServer(source.TmplBox.HTTPBox()))
}
return r
} }

View File

@ -76,6 +76,7 @@ func SaveSASSHandler(w http.ResponseWriter, r *http.Request) {
source.SaveAsset([]byte(theme), utils.Directory, "scss/base.scss") source.SaveAsset([]byte(theme), utils.Directory, "scss/base.scss")
source.SaveAsset([]byte(variables), utils.Directory, "scss/variables.scss") source.SaveAsset([]byte(variables), utils.Directory, "scss/variables.scss")
source.CompileSASS(utils.Directory) source.CompileSASS(utils.Directory)
resetRouter()
ExecuteResponse(w, r, "settings.html", core.CoreApp) ExecuteResponse(w, r, "settings.html", core.CoreApp)
} }
@ -95,7 +96,7 @@ func SaveAssetsHandler(w http.ResponseWriter, r *http.Request) {
source.CopyToPublic(source.CssBox, dir+"/assets/css", "base.css") source.CopyToPublic(source.CssBox, dir+"/assets/css", "base.css")
utils.Log(2, "Default 'base.css' was insert because SASS did not work.") utils.Log(2, "Default 'base.css' was insert because SASS did not work.")
} }
source.UsingAssets = true resetRouter()
ExecuteResponse(w, r, "settings.html", core.CoreApp) ExecuteResponse(w, r, "settings.html", core.CoreApp)
} }
@ -104,9 +105,8 @@ func DeleteAssetsHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
return return
} }
source.DeleteAllAssets(".") source.DeleteAllAssets(utils.Directory)
source.UsingAssets = false resetRouter()
LocalizedAssets(Router())
ExecuteResponse(w, r, "settings.html", core.CoreApp) ExecuteResponse(w, r, "settings.html", core.CoreApp)
} }

View File

@ -16,7 +16,6 @@
package handlers package handlers
import ( import (
"github.com/gorilla/sessions"
"github.com/hunterlong/statup/core" "github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/types" "github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils" "github.com/hunterlong/statup/utils"
@ -133,7 +132,7 @@ func ProcessSetupHandler(w http.ResponseWriter, r *http.Request) {
} }
core.InitApp() core.InitApp()
Store = sessions.NewCookieStore([]byte(core.CoreApp.ApiSecret)) resetCookies()
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
} }

View File

@ -182,7 +182,7 @@ HTML, BODY {
font-family: monospace; font-family: monospace;
position: relative; position: relative;
overflow: hidden; overflow: hidden;
height: 60vh; } height: 80vh; }
.CodeMirror-focused { .CodeMirror-focused {
/* Bootstrap Settings */ /* Bootstrap Settings */

View File

@ -233,7 +233,7 @@ HTML,BODY {
font-family: monospace; font-family: monospace;
position: relative; position: relative;
overflow: hidden; overflow: hidden;
height:60vh; height:80vh;
} }
.CodeMirror-focused { .CodeMirror-focused {

View File

@ -32,7 +32,6 @@ var (
ScssBox *rice.Box ScssBox *rice.Box
JsBox *rice.Box JsBox *rice.Box
TmplBox *rice.Box TmplBox *rice.Box
UsingAssets bool
) )
func Assets() { func Assets() {
@ -96,14 +95,12 @@ func CompileSASS(folder string) error {
return err return err
} }
func HasAssets(folder string) bool { func UsingAssets(folder string) bool {
if _, err := os.Stat(folder + "/assets"); err == nil { if _, err := os.Stat(folder + "/assets"); err == nil {
utils.Log(1, "Assets folder was found!") utils.Log(1, "Assets folder was found!")
UsingAssets = true
return true return true
} else { } else {
assetEnv := os.Getenv("USE_ASSETS") if os.Getenv("USE_ASSETS") == "true" {
if assetEnv == "true" {
utils.Log(1, "Environment variable USE_ASSETS was found.") utils.Log(1, "Environment variable USE_ASSETS was found.")
CreateAllAssets(folder) CreateAllAssets(folder)
err := CompileSASS(folder) err := CompileSASS(folder)
@ -112,9 +109,9 @@ func HasAssets(folder string) bool {
utils.Log(2, "Default 'base.css' was insert because SASS did not work.") utils.Log(2, "Default 'base.css' was insert because SASS did not work.")
return true return true
} }
UsingAssets = true
return true return true
} }
utils.Log(1, "Not using local assets in: "+folder+"/assets")
} }
return false return false
} }

View File

@ -30,23 +30,23 @@ func init() {
dir = utils.Directory dir = utils.Directory
utils.InitLogs() utils.InitLogs()
Assets() Assets()
os.RemoveAll(dir + "/cmd/assets") os.RemoveAll(dir + "/assets")
} }
func TestCore_UsingAssets(t *testing.T) { func TestCore_UsingAssets(t *testing.T) {
assert.False(t, UsingAssets) assert.False(t, UsingAssets(dir))
} }
func TestCreateAssets(t *testing.T) { func TestCreateAssets(t *testing.T) {
assert.Nil(t, CreateAllAssets(dir)) assert.Nil(t, CreateAllAssets(dir))
assert.True(t, HasAssets(dir)) assert.True(t, UsingAssets(dir))
assert.FileExists(t, "../assets/css/base.css") assert.FileExists(t, "../assets/css/base.css")
assert.FileExists(t, "../assets/scss/base.scss") assert.FileExists(t, "../assets/scss/base.scss")
} }
func TestCompileSASS(t *testing.T) { func TestCompileSASS(t *testing.T) {
assert.Nil(t, CompileSASS(dir)) assert.Nil(t, CompileSASS(dir))
assert.True(t, HasAssets(dir)) assert.True(t, UsingAssets(dir))
} }
func TestSaveAsset(t *testing.T) { func TestSaveAsset(t *testing.T) {
@ -63,11 +63,10 @@ func TestOpenAsset(t *testing.T) {
func TestDeleteAssets(t *testing.T) { func TestDeleteAssets(t *testing.T) {
assert.Nil(t, DeleteAllAssets(dir)) assert.Nil(t, DeleteAllAssets(dir))
assert.False(t, HasAssets(dir)) assert.False(t, UsingAssets(dir))
} }
func TestCopyToPluginFailed(t *testing.T) { func TestCopyToPluginFailed(t *testing.T) {
assert.Nil(t, DeleteAllAssets(dir)) assert.Nil(t, DeleteAllAssets(dir))
assert.False(t, HasAssets(dir)) assert.False(t, UsingAssets(dir))
} }

View File

@ -121,7 +121,7 @@
</div> </div>
</div> </div>
<button type="submit" class="btn btn-primary btn-block mt-2">Save Style</button> <button type="submit" class="btn btn-primary btn-block mt-2">Save Style</button>
<a href="/settings/delete_assets" class="btn btn-danger btn-block">Delete Assets</a> <a href="/settings/delete_assets" class="btn btn-danger btn-block confirm-btn">Delete All Assets</a>
</form> </form>
{{end}} {{end}}
</div> </div>