Adding support to configuration files and an example configuration file.

This commit is contained in:
sergiotarxz 2021-02-08 01:07:43 +01:00
parent 5e15ae95fc
commit d45ae12c1d
Signed by: sergiotarxz
GPG Key ID: E5903508B6510AC2
4 changed files with 96 additions and 2 deletions

13
config.json Normal file
View File

@ -0,0 +1,13 @@
{
"db": {
"engine": "postgres",
"user": "wwwshop",
"password": "wwwshop",
"host": "localhost",
"port": 5432,
"database": "wwwshop",
"options": {
"sslmode": "disable"
}
}
}

73
wwwshop/config/config.go Normal file
View File

@ -0,0 +1,73 @@
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"regexp"
"strings"
"bytes"
"errors"
)
type Config struct {
DB DB `json:"db"`
}
type DB struct {
Engine string `json:"engine"`
User string `json:"user"`
Password string `json:"password"`
Host string `json:"host"`
Port int64 `json:"port"`
Database string `json:"database"`
Options map[string]string
}
var search_dir = "."
func New() (*Config, error) {
data, err := ioutil.ReadFile(strings.Join([]string{search_dir, "/config.json"}, ""))
if err != nil {
return nil, err
}
var config Config
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.Decode(&config)
return &config, nil
}
func (self DB) GetURL() (*string, error) {
if !(CheckWord(self.Engine) && CheckWord(self.User) && CheckWord(self.Password) && CheckWord(self.Database) && CheckHost(self.Host)) {
return nil, errors.New("Database configuration doesn't pass the constraints.")
}
return_string := fmt.Sprintf("%s://%s:%s@%s:%d/%s", self.Engine, self.User, self.Password, self.Host, self.Port, self.Database)
if self.Options != nil && len(self.Options) > 0 {
return_string = strings.Join([]string{return_string, "?"}, "")
for key, value := range self.Options {
return_string = strings.Join([]string{
return_string,
strings.Join([]string{key, value}, "="),
}, "&")
}
}
return &return_string, nil
}
func CheckHost(word string) bool {
match, err := regexp.MatchString("^(\\w|\\.)+$", word)
if err != nil {
fmt.Println(err.Error())
return false
}
return match
}
func CheckWord(word string) bool {
match, err := regexp.MatchString("^\\w+$", word)
if err != nil {
fmt.Println(err.Error())
return false
}
return match
}

View File

@ -1,6 +1,7 @@
package database
import (
"WWWShop/wwwshop/config"
"database/sql"
"fmt"
"github.com/golang-migrate/migrate/v4"
@ -12,7 +13,12 @@ import (
)
func DB() *sql.DB {
db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
configuration, err := config.New()
url_postgres, err := configuration.DB.GetURL()
if err != nil {
log.Fatal(err)
}
db, err := sql.Open("postgres", *url_postgres)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
@ -30,6 +36,6 @@ func Migrate(db *sql.DB) {
log.Fatal(err)
}
if err := m.Up(); err != nil {
log.Fatal(err)
log.Println(err)
}
}

View File

@ -4,6 +4,7 @@ import (
"net/http"
c "WWWShop/wwwshop/controller"
u "WWWShop/wwwshop/controller/usercontroller"
d "WWWShop/wwwshop/dao/database"
)
type WWWShop struct {}
@ -15,6 +16,7 @@ func New() WWWShop {
func (self WWWShop) Init() {
d.Migrate(d.DB())
user_controller := c.GenerateController(u.New())
http.Handle("/user", user_controller)
http.Handle("/user/", user_controller)