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

View File

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

View File

@ -77,7 +77,7 @@ func TestRunAll(t *testing.T) {
RunDatabaseMigrations(t, dbt)
})
t.Run(dbt+" Sample Data", func(t *testing.T) {
RunInsertMysqlSample(t)
RunInsertSampleData(t)
})
t.Run(dbt+" Load Configs", func(t *testing.T) {
RunLoadConfig(t)
@ -180,7 +180,7 @@ func TestRunAll(t *testing.T) {
RunSettingsHandler(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)
}
func RunInsertMysqlSample(t *testing.T) {
func RunInsertSampleData(t *testing.T) {
err := core.LoadSampleData()
assert.Nil(t, err)
}

View File

@ -88,25 +88,25 @@ func UpdateCore(c *Core) (*Core, error) {
}
func (c Core) UsingAssets() bool {
return source.UsingAssets
return source.UsingAssets(utils.Directory)
}
func (c Core) SassVars() string {
if !source.UsingAssets {
if !source.UsingAssets(utils.Directory) {
return ""
}
return source.OpenAsset(utils.Directory, "scss/variables.scss")
}
func (c Core) BaseSASS() string {
if !source.UsingAssets {
if !source.UsingAssets(utils.Directory) {
return ""
}
return source.OpenAsset(utils.Directory, "scss/base.scss")
}
func (c Core) MobileSASS() string {
if !source.UsingAssets {
if !source.UsingAssets(utils.Directory) {
return ""
}
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) {
if Store == nil {
resetCookies()
}
session, _ := Store.Get(r, COOKIE_KEY)
r.ParseForm()
username := r.PostForm.Get("username")

View File

@ -33,7 +33,8 @@ const (
)
var (
Store *sessions.CookieStore
Store *sessions.CookieStore
httpServer *http.Server
)
func RunHTTPServer(ip string, port int) error {
@ -43,18 +44,20 @@ func RunHTTPServer(ip string, port int) error {
// info := p.GetInfo()
// for _, route := range p.Routes() {
// 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))
// }
//}
srv := &http.Server{
router = Router()
httpServer = &http.Server{
Addr: host,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: Router(),
Handler: router,
}
return srv.ListenAndServe()
resetCookies()
return httpServer.ListenAndServe()
}
func IsAuthenticated(r *http.Request) bool {

View File

@ -28,9 +28,14 @@ import (
"testing"
)
var (
dir string
)
func init() {
utils.InitLogs()
source.Assets()
dir = utils.Directory
}
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/js/main.js")
assert.DirExists(t, utils.Directory+"/assets")
assert.True(t, source.UsingAssets)
assert.True(t, source.UsingAssets(dir))
assert.True(t, IsRouteAuthenticated(req))
}
@ -472,7 +477,7 @@ func TestDeleteAssetsHandler(t *testing.T) {
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 200, rr.Code)
assert.False(t, source.UsingAssets)
assert.False(t, source.UsingAssets(dir))
assert.True(t, IsRouteAuthenticated(req))
}

View File

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

View File

@ -21,18 +21,33 @@ import (
"github.com/gorilla/sessions"
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/source"
"github.com/hunterlong/statup/utils"
"net/http"
"time"
)
var (
r *mux.Router
router *mux.Router
)
func Router() *mux.Router {
r = mux.NewRouter()
dir := utils.Directory
r := mux.NewRouter()
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("/setup", http.HandlerFunc(SetupHandler)).Methods("GET")
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("/metrics", http.HandlerFunc(PrometheusHandler))
r.NotFoundHandler = http.HandlerFunc(Error404Handler)
return r
}
func resetRouter() {
router = Router()
httpServer.Handler = router
}
func resetCookies() {
if core.CoreApp != nil {
cookie := fmt.Sprintf("%v_%v", core.CoreApp.ApiSecret, time.Now().Nanosecond())
Store = sessions.NewCookieStore([]byte(cookie))
} else {
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(variables), utils.Directory, "scss/variables.scss")
source.CompileSASS(utils.Directory)
resetRouter()
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")
utils.Log(2, "Default 'base.css' was insert because SASS did not work.")
}
source.UsingAssets = true
resetRouter()
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)
return
}
source.DeleteAllAssets(".")
source.UsingAssets = false
LocalizedAssets(Router())
source.DeleteAllAssets(utils.Directory)
resetRouter()
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}

View File

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

View File

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

View File

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

View File

@ -27,12 +27,11 @@ import (
)
var (
SqlBox *rice.Box
CssBox *rice.Box
ScssBox *rice.Box
JsBox *rice.Box
TmplBox *rice.Box
UsingAssets bool
SqlBox *rice.Box
CssBox *rice.Box
ScssBox *rice.Box
JsBox *rice.Box
TmplBox *rice.Box
)
func Assets() {
@ -96,14 +95,12 @@ func CompileSASS(folder string) error {
return err
}
func HasAssets(folder string) bool {
func UsingAssets(folder string) bool {
if _, err := os.Stat(folder + "/assets"); err == nil {
utils.Log(1, "Assets folder was found!")
UsingAssets = true
return true
} else {
assetEnv := os.Getenv("USE_ASSETS")
if assetEnv == "true" {
if os.Getenv("USE_ASSETS") == "true" {
utils.Log(1, "Environment variable USE_ASSETS was found.")
CreateAllAssets(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.")
return true
}
UsingAssets = true
return true
}
utils.Log(1, "Not using local assets in: "+folder+"/assets")
}
return false
}

View File

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

View File

@ -121,7 +121,7 @@
</div>
</div>
<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>
{{end}}
</div>