add settings and environment
This commit adds the environment to the stack so that the db connection can be used by easily without using a global. It also prepares for using the template system.
This commit is contained in:
parent
5fdfdbf5e1
commit
972d2e1412
|
@ -0,0 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gibheer/zero-blog/lib"
|
||||
)
|
||||
|
||||
func boot_system() (*lib.Environment, error) {
|
||||
env := &lib.Environment{}
|
||||
settings, err := lib.LoadConfiguration()
|
||||
if err != nil {
|
||||
return env, err
|
||||
}
|
||||
env.Config = settings
|
||||
|
||||
env.DB, err = lib.NewDatabase(settings.Connection)
|
||||
if err != nil {
|
||||
return env, err
|
||||
}
|
||||
return env, nil
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"database/sql"
|
||||
pq "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
Conn *sql.DB
|
||||
}
|
||||
|
||||
func (d *Database) Query(query string) (*sql.Rows, error) {
|
||||
res, err := d.Conn.Query(query)
|
||||
if err != nil {
|
||||
err := err.(*pq.Error)
|
||||
return nil, errors.New(err.Code.Name())
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func NewDatabase(connection string) (*Database, error) {
|
||||
if connection == "" {
|
||||
return nil, errors.New("No connection string given! Check the config.yml file!")
|
||||
}
|
||||
DB, err := sql.Open("postgres", connection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Database{DB}, nil
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"gopkg.in/yaml.v1"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
Connection string
|
||||
}
|
||||
|
||||
type Environment struct {
|
||||
// settings from the config file
|
||||
Config *Settings
|
||||
// the database connection pool
|
||||
DB *Database
|
||||
}
|
||||
|
||||
func LoadConfiguration() (*Settings, error) {
|
||||
configfile, err := ioutil.ReadFile("config.yml")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
settings := Settings{}
|
||||
err = yaml.Unmarshal(configfile, &settings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &settings, nil
|
||||
}
|
|
@ -10,6 +10,8 @@ import (
|
|||
|
||||
// this router defines each action to take for an URI including the middlewares
|
||||
type Router struct {
|
||||
// the environment of the application with settings and database connection
|
||||
env *Environment
|
||||
// path that this engine takes care of relative to the parent
|
||||
path string
|
||||
// the list of functions to run on a request
|
||||
|
@ -20,6 +22,11 @@ type Router struct {
|
|||
parent *Router
|
||||
}
|
||||
|
||||
// create a new router with the specific environment
|
||||
func NewRouter(env *Environment) *Router {
|
||||
return &Router{env, "", make([]ContextFunc, 0), httprouter.New(), nil}
|
||||
}
|
||||
|
||||
// Bundle all parameters into the context to make it easier to push important
|
||||
// data into functions.
|
||||
type Context struct {
|
||||
|
@ -34,15 +41,13 @@ type Context struct {
|
|||
funcList []ContextFunc
|
||||
// the current position in the function list
|
||||
current int
|
||||
// a direct link to the environment
|
||||
Env *Environment
|
||||
}
|
||||
|
||||
// the func type for internal routes
|
||||
type ContextFunc func(*Context) error
|
||||
|
||||
func NewRouter() *Router {
|
||||
return &Router{"", make([]ContextFunc, 0), httprouter.New(), nil}
|
||||
}
|
||||
|
||||
func (r *Router) fullpath(path string) string {
|
||||
if r.parent != nil {
|
||||
return r.parent.fullpath(r.path + path)
|
||||
|
@ -88,7 +93,7 @@ func (r *Router) createHandleFunction(target ContextFunc) httprouter.Handle {
|
|||
params httprouter.Params) {
|
||||
|
||||
ctx := &Context{
|
||||
request, response, params, append(r.fullFuncList(), target), 0,
|
||||
request, response, params, append(r.fullFuncList(), target), 0, r.env,
|
||||
}
|
||||
for i := 0; i < len(ctx.funcList); i++ {
|
||||
ctx.funcList[i](ctx)
|
||||
|
@ -101,7 +106,7 @@ func (r *Router) Use(middleware ContextFunc) {
|
|||
}
|
||||
|
||||
func (r *Router) NewGroup(path string) *Router {
|
||||
return &Router{path, make([]ContextFunc, 0), r.router, r}
|
||||
return &Router{r.env, path, make([]ContextFunc, 0), r.router, r}
|
||||
}
|
||||
|
||||
func (r *Router) Start() {
|
||||
|
|
12
main.go
12
main.go
|
@ -1,14 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"github.com/gibheer/zero-blog/lib"
|
||||
"github.com/gibheer/zero-blog/controller"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := lib.NewRouter()
|
||||
authentication := router.NewGroup("/admin")
|
||||
environment, err := boot_system()
|
||||
if err != nil {
|
||||
log.Fatal("Boot crashed with message: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
router := lib.NewRouter(environment)
|
||||
controller.DefineRoutes(router)
|
||||
router.Start()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue