Move database and sessions to services
This commit is contained in:
parent
0883013a1b
commit
cad6e94368
6 changed files with 88 additions and 13 deletions
25
assets/templates/views/cert_list.gohtml
Normal file
25
assets/templates/views/cert_list.gohtml
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
{{ define "meta" }}
|
||||||
|
<title>Log in</title>
|
||||||
|
<meta name="description" content="Test boilerplate" />
|
||||||
|
{{ end}}
|
||||||
|
|
||||||
|
{{ define "content" }}
|
||||||
|
<section class="content">
|
||||||
|
<div class="section">
|
||||||
|
<div class="container">
|
||||||
|
<div class="columns">
|
||||||
|
<div class="column">
|
||||||
|
<h1>Certificates</h1>
|
||||||
|
{{ if .Certificates }}
|
||||||
|
{{ range .Certificates }}
|
||||||
|
<li>{{ .User }}@{{ .Name }}</li>
|
||||||
|
{{ end }}
|
||||||
|
{{ else }}
|
||||||
|
<p>You don't have certificates yet!</p>
|
||||||
|
{{ end }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
{{ end}}
|
14
main.go
14
main.go
|
@ -4,9 +4,8 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"git.klink.asia/paul/certman/services"
|
||||||
|
|
||||||
"git.klink.asia/paul/certman/models"
|
|
||||||
"git.klink.asia/paul/certman/router"
|
"git.klink.asia/paul/certman/router"
|
||||||
"git.klink.asia/paul/certman/views"
|
"git.klink.asia/paul/certman/views"
|
||||||
|
|
||||||
|
@ -17,20 +16,13 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Connect to the database
|
// Connect to the database
|
||||||
db, err := gorm.Open("sqlite3", "db.sqlite3")
|
db := services.InitDB()
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not open database: %s", err.Error())
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
// Migrate
|
|
||||||
db.AutoMigrate(models.User{}, models.ClientConf{})
|
|
||||||
|
|
||||||
// load and parse template files
|
// load and parse template files
|
||||||
views.LoadTemplates()
|
views.LoadTemplates()
|
||||||
|
|
||||||
mux := router.HandleRoutes(db)
|
mux := router.HandleRoutes(db)
|
||||||
|
|
||||||
err = http.ListenAndServe(":8000", mux)
|
err := http.ListenAndServe(":8000", mux)
|
||||||
log.Fatalf(err.Error())
|
log.Fatalf(err.Error())
|
||||||
}
|
}
|
||||||
|
|
26
services/db.go
Normal file
26
services/db.go
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"git.klink.asia/paul/certman/models"
|
||||||
|
"git.klink.asia/paul/certman/settings"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var DB *gorm.DB
|
||||||
|
|
||||||
|
func InitDB() *gorm.DB {
|
||||||
|
dsn := settings.Get("DATABASE_URL", "db.sqlite3")
|
||||||
|
|
||||||
|
// Establish connection
|
||||||
|
db, err := gorm.Open("sqlite3", dsn)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not open database: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migrate models
|
||||||
|
db.AutoMigrate(models.User{}, models.ClientConf{})
|
||||||
|
|
||||||
|
return db
|
||||||
|
}
|
21
services/sessions.go
Normal file
21
services/sessions.go
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.klink.asia/paul/certman/settings"
|
||||||
|
"github.com/gorilla/securecookie"
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Sessions sessions.Store
|
||||||
|
|
||||||
|
func InitSession() {
|
||||||
|
store := sessions.NewCookieStore(
|
||||||
|
securecookie.GenerateRandomKey(32), // signing key
|
||||||
|
securecookie.GenerateRandomKey(32), // encryption key
|
||||||
|
)
|
||||||
|
store.Options.HttpOnly = true
|
||||||
|
store.Options.MaxAge = 7 * 24 * 60 * 60 // 1 Week
|
||||||
|
store.Options.Secure = settings.Get("ENVIRONMENT", "") == "production"
|
||||||
|
|
||||||
|
Sessions = store
|
||||||
|
}
|
10
settings/settings.go
Normal file
10
settings/settings.go
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
package settings
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
func Get(key, defaultVal string) string {
|
||||||
|
if val := os.Getenv(key); val != "" {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
return defaultVal
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ func LoadTemplates() {
|
||||||
|
|
||||||
"debug": newTemplate("layouts/application.gohtml", "shared/header.gohtml", "shared/footer.gohtml", "views/debug.gohtml"),
|
"debug": newTemplate("layouts/application.gohtml", "shared/header.gohtml", "shared/footer.gohtml", "views/debug.gohtml"),
|
||||||
"login": newTemplate("layouts/application.gohtml", "shared/header.gohtml", "shared/footer.gohtml", "views/login.gohtml"),
|
"login": newTemplate("layouts/application.gohtml", "shared/header.gohtml", "shared/footer.gohtml", "views/login.gohtml"),
|
||||||
|
"cert_list": newTemplate("layouts/application.gohtml", "shared/header.gohtml", "shared/footer.gohtml", "views/cert_list.gohtml"),
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue