From f474d56ebef55d2788b3585aa7e5110269e56031 Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Fri, 22 Jun 2018 22:59:29 -0700 Subject: [PATCH] upgrades --- communication.go | 4 +++- core.go | 7 +++--- exporter.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ main_test.go | 4 ++-- sql/mysql_up.sql | 6 ++--- sql/postgres_up.sql | 2 +- sql/sqlite_up.sql | 2 +- users.go | 2 +- 8 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 exporter.go diff --git a/communication.go b/communication.go index 0c1a4f12..942c842f 100644 --- a/communication.go +++ b/communication.go @@ -48,7 +48,9 @@ func Create(c *types.Communication) (int64, error) { return 0, err } c.Id = uuid.(int64) - core.Communications = append(core.Communications, c) + if core != nil { + core.Communications = append(core.Communications, c) + } return uuid.(int64), err } diff --git a/core.go b/core.go index e3a9fff5..b74766a1 100644 --- a/core.go +++ b/core.go @@ -39,11 +39,12 @@ func (c Core) AllOnline() bool { } func SelectCore() (*Core, error) { - var core Core - err := dbSession.Collection("core").Find().One(&core) + var c *Core + err := dbSession.Collection("core").Find().One(&c) if err != nil { return nil, err } + core = c store = sessions.NewCookieStore([]byte(core.ApiSecret)) - return &core, err + return core, err } diff --git a/exporter.go b/exporter.go new file mode 100644 index 00000000..9bebe904 --- /dev/null +++ b/exporter.go @@ -0,0 +1,53 @@ +package main + +import ( + "bytes" + "fmt" + "html/template" + "log" +) + +var httpFunctions template.FuncMap + +func init() { + + httpFunctions = template.FuncMap{ + "js": func(html string) template.JS { + return template.JS(html) + }, + "safe": func(html string) template.HTML { + return template.HTML(html) + }, + "VERSION": func() string { + return VERSION + }, + "underscore": func(html string) string { + return UnderScoreString(html) + }, + } + +} + +func ExportIndexHTML() string { + out := index{*core, services} + nav, _ := tmplBox.String("nav.html") + footer, _ := tmplBox.String("footer.html") + render, err := tmplBox.String("index.html") + if err != nil { + panic(err) + } + t := template.New("message") + t.Funcs(httpFunctions) + t, _ = t.Parse(nav) + t, _ = t.Parse(footer) + t.Parse(render) + + var tpl bytes.Buffer + if err := t.Execute(&tpl, out); err != nil { + log.Println(err) + } + result := tpl.String() + + fmt.Println(result) + return result +} diff --git a/main_test.go b/main_test.go index c0fb73b4..00a14615 100644 --- a/main_test.go +++ b/main_test.go @@ -220,7 +220,7 @@ func TestService_Hits(t *testing.T) { assert.NotNil(t, service) hits, err := service.Hits() assert.Nil(t, err) - assert.Equal(t, 26, len(hits)) + assert.Equal(t, 23, len(hits)) } func TestService_LimitedHits(t *testing.T) { @@ -228,7 +228,7 @@ func TestService_LimitedHits(t *testing.T) { assert.NotNil(t, service) hits, err := service.LimitedHits() assert.Nil(t, err) - assert.Equal(t, 26, len(hits)) + assert.Equal(t, 23, len(hits)) } func Test(t *testing.T) { diff --git a/sql/mysql_up.sql b/sql/mysql_up.sql index 788087a0..014cb8e4 100644 --- a/sql/mysql_up.sql +++ b/sql/mysql_up.sql @@ -16,7 +16,7 @@ CREATE TABLE users ( email text, api_key VARCHAR(50), api_secret VARCHAR(50), - admin bool, + administrator BOOL NOT NULL DEFAULT '0', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, INDEX (id) ); @@ -72,8 +72,8 @@ CREATE TABLE communication ( var2 text, api_key text, api_secret text, - enabled boolean, - removable boolean, + enabled BOOL NOT NULL DEFAULT '0', + removable BOOL NOT NULL DEFAULT '0', limits integer, created_at TIMESTAMP ); \ No newline at end of file diff --git a/sql/postgres_up.sql b/sql/postgres_up.sql index 5132500c..006a4406 100644 --- a/sql/postgres_up.sql +++ b/sql/postgres_up.sql @@ -17,7 +17,7 @@ CREATE TABLE users ( email text, api_key text, api_secret text, - admin bool, + administrator bool, created_at TIMESTAMP ); diff --git a/sql/sqlite_up.sql b/sql/sqlite_up.sql index 513d2730..a67f6571 100644 --- a/sql/sqlite_up.sql +++ b/sql/sqlite_up.sql @@ -17,7 +17,7 @@ CREATE TABLE users ( email text, api_key text, api_secret text, - admin bool, + administrator bool, created_at TIMESTAMP ); diff --git a/users.go b/users.go index a6c67abb..30681868 100644 --- a/users.go +++ b/users.go @@ -12,7 +12,7 @@ type User struct { Email string `db:"email" json:"-"` ApiKey string `db:"api_key" json:"api_key"` ApiSecret string `db:"api_secret" json:"-"` - Admin bool `db:"admin" json:"admin"` + Admin bool `db:"administrator" json:"admin"` CreatedAt time.Time `db:"created_at" json:"created_at"` }