mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-12-25 23:23:21 -05:00
First pass at reorganizing go code into sub-packages
This commit is contained in:
parent
d9ddef250a
commit
d0a5980b85
4
.gitignore
vendored
4
.gitignore
vendored
@ -3,5 +3,5 @@ static/bundle.js
|
|||||||
static/codemirror
|
static/codemirror
|
||||||
static/react-widgets
|
static/react-widgets
|
||||||
node_modules
|
node_modules
|
||||||
cusip_list.csv
|
internal/handlers/cusip_list.csv
|
||||||
security_templates.go
|
internal/handlers/security_templates.go
|
||||||
|
8
Makefile
8
Makefile
@ -1,6 +1,6 @@
|
|||||||
JS_SOURCES = $(wildcard js/*.js) $(wildcard js/*/*.js)
|
JS_SOURCES = $(wildcard js/*.js) $(wildcard js/*/*.js)
|
||||||
|
|
||||||
all: static/bundle.js static/react-widgets static/codemirror/codemirror.css security_templates.go
|
all: static/bundle.js static/react-widgets static/codemirror/codemirror.css
|
||||||
|
|
||||||
node_modules:
|
node_modules:
|
||||||
npm install
|
npm install
|
||||||
@ -15,10 +15,4 @@ static/codemirror/codemirror.css: node_modules/codemirror/lib/codemirror.js node
|
|||||||
mkdir -p static/codemirror
|
mkdir -p static/codemirror
|
||||||
cp node_modules/codemirror/lib/codemirror.css static/codemirror/codemirror.css
|
cp node_modules/codemirror/lib/codemirror.css static/codemirror/codemirror.css
|
||||||
|
|
||||||
security_templates.go: cusip_list.csv
|
|
||||||
./scripts/gen_security_list.py > security_templates.go
|
|
||||||
|
|
||||||
cusip_list.csv:
|
|
||||||
./scripts/gen_cusip_csv.sh > cusip_list.csv
|
|
||||||
|
|
||||||
.PHONY = all
|
.PHONY = all
|
||||||
|
@ -47,7 +47,8 @@ You'll then want to build everything (the Golang and Javascript portions) using
|
|||||||
something like:
|
something like:
|
||||||
|
|
||||||
$ export GOPATH=`pwd`
|
$ export GOPATH=`pwd`
|
||||||
$ go get -v github.com/aclindsa/moneygo
|
$ go get -d github.com/aclindsa/moneygo
|
||||||
|
$ go generate -v github.com/aclindsa/moneygo/internal/handlers
|
||||||
$ go generate -v github.com/aclindsa/moneygo
|
$ go generate -v github.com/aclindsa/moneygo
|
||||||
$ go install -v github.com/aclindsa/moneygo
|
$ go install -v github.com/aclindsa/moneygo
|
||||||
|
|
||||||
|
48
db.go
48
db.go
@ -1,48 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
_ "github.com/lib/pq"
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
"gopkg.in/gorp.v1"
|
|
||||||
)
|
|
||||||
|
|
||||||
func initDB(cfg *Config) (*gorp.DbMap, error) {
|
|
||||||
db, err := sql.Open(cfg.MoneyGo.DBType.String(), cfg.MoneyGo.DSN)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var dialect gorp.Dialect
|
|
||||||
if cfg.MoneyGo.DBType == SQLite {
|
|
||||||
dialect = gorp.SqliteDialect{}
|
|
||||||
} else if cfg.MoneyGo.DBType == MySQL {
|
|
||||||
dialect = gorp.MySQLDialect{
|
|
||||||
Engine: "InnoDB",
|
|
||||||
Encoding: "UTF8",
|
|
||||||
}
|
|
||||||
} else if cfg.MoneyGo.DBType == Postgres {
|
|
||||||
dialect = gorp.PostgresDialect{}
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("Don't know gorp dialect to go with '%s' DB type", cfg.MoneyGo.DBType.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
dbmap := &gorp.DbMap{Db: db, Dialect: dialect}
|
|
||||||
dbmap.AddTableWithName(User{}, "users").SetKeys(true, "UserId")
|
|
||||||
dbmap.AddTableWithName(Session{}, "sessions").SetKeys(true, "SessionId")
|
|
||||||
dbmap.AddTableWithName(Account{}, "accounts").SetKeys(true, "AccountId")
|
|
||||||
dbmap.AddTableWithName(Security{}, "securities").SetKeys(true, "SecurityId")
|
|
||||||
dbmap.AddTableWithName(Transaction{}, "transactions").SetKeys(true, "TransactionId")
|
|
||||||
dbmap.AddTableWithName(Split{}, "splits").SetKeys(true, "SplitId")
|
|
||||||
dbmap.AddTableWithName(Price{}, "prices").SetKeys(true, "PriceId")
|
|
||||||
dbmap.AddTableWithName(Report{}, "reports").SetKeys(true, "ReportId")
|
|
||||||
|
|
||||||
err = dbmap.CreateTablesIfNotExists()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return dbmap, nil
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -59,7 +59,7 @@ type Config struct {
|
|||||||
MoneyGo MoneyGo
|
MoneyGo MoneyGo
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfig(filename string) (*Config, error) {
|
func ReadConfig(filename string) (*Config, error) {
|
||||||
cfg := Config{
|
cfg := Config{
|
||||||
MoneyGo: MoneyGo{
|
MoneyGo: MoneyGo{
|
||||||
Fcgi: false,
|
Fcgi: false,
|
45
internal/db/db.go
Normal file
45
internal/db/db.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"github.com/aclindsa/moneygo/internal/config"
|
||||||
|
"github.com/aclindsa/moneygo/internal/handlers"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
"gopkg.in/gorp.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetDbMap(db *sql.DB, cfg *config.Config) (*gorp.DbMap, error) {
|
||||||
|
var dialect gorp.Dialect
|
||||||
|
if cfg.MoneyGo.DBType == config.SQLite {
|
||||||
|
dialect = gorp.SqliteDialect{}
|
||||||
|
} else if cfg.MoneyGo.DBType == config.MySQL {
|
||||||
|
dialect = gorp.MySQLDialect{
|
||||||
|
Engine: "InnoDB",
|
||||||
|
Encoding: "UTF8",
|
||||||
|
}
|
||||||
|
} else if cfg.MoneyGo.DBType == config.Postgres {
|
||||||
|
dialect = gorp.PostgresDialect{}
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("Don't know gorp dialect to go with '%s' DB type", cfg.MoneyGo.DBType.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
dbmap := &gorp.DbMap{Db: db, Dialect: dialect}
|
||||||
|
dbmap.AddTableWithName(handlers.User{}, "users").SetKeys(true, "UserId")
|
||||||
|
dbmap.AddTableWithName(handlers.Session{}, "sessions").SetKeys(true, "SessionId")
|
||||||
|
dbmap.AddTableWithName(handlers.Account{}, "accounts").SetKeys(true, "AccountId")
|
||||||
|
dbmap.AddTableWithName(handlers.Security{}, "securities").SetKeys(true, "SecurityId")
|
||||||
|
dbmap.AddTableWithName(handlers.Transaction{}, "transactions").SetKeys(true, "TransactionId")
|
||||||
|
dbmap.AddTableWithName(handlers.Split{}, "splits").SetKeys(true, "SplitId")
|
||||||
|
dbmap.AddTableWithName(handlers.Price{}, "prices").SetKeys(true, "PriceId")
|
||||||
|
dbmap.AddTableWithName(handlers.Report{}, "reports").SetKeys(true, "ReportId")
|
||||||
|
|
||||||
|
err := dbmap.CreateTablesIfNotExists()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return dbmap, nil
|
||||||
|
}
|
9
internal/handlers/Makefile
Normal file
9
internal/handlers/Makefile
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
all: security_templates.go
|
||||||
|
|
||||||
|
security_templates.go: cusip_list.csv scripts/gen_security_list.py
|
||||||
|
./scripts/gen_security_list.py > security_templates.go
|
||||||
|
|
||||||
|
cusip_list.csv:
|
||||||
|
./scripts/gen_cusip_csv.sh > cusip_list.csv
|
||||||
|
|
||||||
|
.PHONY = all
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
31
internal/handlers/handlers.go
Normal file
31
internal/handlers/handlers.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gopkg.in/gorp.v1"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Create a closure over db, allowing the handlers to look like a
|
||||||
|
// http.HandlerFunc
|
||||||
|
type DB = gorp.DbMap
|
||||||
|
type DBHandler func(http.ResponseWriter, *http.Request, *DB)
|
||||||
|
|
||||||
|
func DBHandlerFunc(h DBHandler, db *DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
h(w, r, db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetHandler(db *DB) *http.ServeMux {
|
||||||
|
servemux := http.NewServeMux()
|
||||||
|
servemux.HandleFunc("/session/", DBHandlerFunc(SessionHandler, db))
|
||||||
|
servemux.HandleFunc("/user/", DBHandlerFunc(UserHandler, db))
|
||||||
|
servemux.HandleFunc("/security/", DBHandlerFunc(SecurityHandler, db))
|
||||||
|
servemux.HandleFunc("/securitytemplate/", SecurityTemplateHandler)
|
||||||
|
servemux.HandleFunc("/account/", DBHandlerFunc(AccountHandler, db))
|
||||||
|
servemux.HandleFunc("/transaction/", DBHandlerFunc(TransactionHandler, db))
|
||||||
|
servemux.HandleFunc("/import/gnucash", DBHandlerFunc(GnucashImportHandler, db))
|
||||||
|
servemux.HandleFunc("/report/", DBHandlerFunc(ReportHandler, db))
|
||||||
|
|
||||||
|
return servemux
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gopkg.in/gorp.v1"
|
"gopkg.in/gorp.v1"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
@ -104,7 +104,7 @@ def main():
|
|||||||
currency_list = get_currency_list()
|
currency_list = get_currency_list()
|
||||||
cusip_list = get_cusip_list('cusip_list.csv')
|
cusip_list = get_cusip_list('cusip_list.csv')
|
||||||
|
|
||||||
print("package main\n")
|
print("package handlers\n")
|
||||||
print("var SecurityTemplates = []Security{")
|
print("var SecurityTemplates = []Security{")
|
||||||
print(currency_list.unicode())
|
print(currency_list.unicode())
|
||||||
print(cusip_list.unicode())
|
print(cusip_list.unicode())
|
@ -1,4 +1,6 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
|
//go:generate make
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
70
main.go
70
main.go
@ -3,8 +3,11 @@ package main
|
|||||||
//go:generate make
|
//go:generate make
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"flag"
|
"flag"
|
||||||
"gopkg.in/gorp.v1"
|
"github.com/aclindsa/moneygo/internal/config"
|
||||||
|
"github.com/aclindsa/moneygo/internal/db"
|
||||||
|
"github.com/aclindsa/moneygo/internal/handlers"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -15,19 +18,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var configFile string
|
var configFile string
|
||||||
var config *Config
|
var cfg *config.Config
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
var err error
|
||||||
flag.StringVar(&configFile, "config", "/etc/moneygo/config.ini", "Path to config file")
|
flag.StringVar(&configFile, "config", "/etc/moneygo/config.ini", "Path to config file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
config, err = readConfig(configFile)
|
cfg, err = config.ReadConfig(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
static_path := path.Join(config.MoneyGo.Basedir, "static")
|
static_path := path.Join(cfg.MoneyGo.Basedir, "static")
|
||||||
|
|
||||||
// Ensure base directory is valid
|
// Ensure base directory is valid
|
||||||
dir_err_str := "The base directory doesn't look like it contains the " +
|
dir_err_str := "The base directory doesn't look like it contains the " +
|
||||||
@ -46,57 +49,48 @@ func init() {
|
|||||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
type FileHandler func(http.ResponseWriter, *http.Request, string)
|
||||||
http.ServeFile(w, r, path.Join(config.MoneyGo.Basedir, "static/index.html"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func staticHandler(w http.ResponseWriter, r *http.Request) {
|
func FileHandlerFunc(h FileHandler, basedir string) http.HandlerFunc {
|
||||||
http.ServeFile(w, r, path.Join(config.MoneyGo.Basedir, r.URL.Path))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a closure over db, allowing the handlers to look like a
|
|
||||||
// http.HandlerFunc
|
|
||||||
type DB = gorp.DbMap
|
|
||||||
type DBHandler func(http.ResponseWriter, *http.Request, *DB)
|
|
||||||
|
|
||||||
func DBHandlerFunc(h DBHandler, db *DB) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
h(w, r, db)
|
h(w, r, basedir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetHandler(db *DB) http.Handler {
|
func rootHandler(w http.ResponseWriter, r *http.Request, basedir string) {
|
||||||
servemux := http.NewServeMux()
|
http.ServeFile(w, r, path.Join(basedir, "static/index.html"))
|
||||||
servemux.HandleFunc("/", rootHandler)
|
}
|
||||||
servemux.HandleFunc("/static/", staticHandler)
|
|
||||||
servemux.HandleFunc("/session/", DBHandlerFunc(SessionHandler, db))
|
|
||||||
servemux.HandleFunc("/user/", DBHandlerFunc(UserHandler, db))
|
|
||||||
servemux.HandleFunc("/security/", DBHandlerFunc(SecurityHandler, db))
|
|
||||||
servemux.HandleFunc("/securitytemplate/", SecurityTemplateHandler)
|
|
||||||
servemux.HandleFunc("/account/", DBHandlerFunc(AccountHandler, db))
|
|
||||||
servemux.HandleFunc("/transaction/", DBHandlerFunc(TransactionHandler, db))
|
|
||||||
servemux.HandleFunc("/import/gnucash", DBHandlerFunc(GnucashImportHandler, db))
|
|
||||||
servemux.HandleFunc("/report/", DBHandlerFunc(ReportHandler, db))
|
|
||||||
|
|
||||||
return servemux
|
func staticHandler(w http.ResponseWriter, r *http.Request, basedir string) {
|
||||||
|
http.ServeFile(w, r, path.Join(basedir, r.URL.Path))
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
database, err := initDB(config)
|
database, err := sql.Open(cfg.MoneyGo.DBType.String(), cfg.MoneyGo.DSN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
handler := GetHandler(database)
|
defer database.Close()
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", ":"+strconv.Itoa(config.MoneyGo.Port))
|
dbmap, err := db.GetDbMap(database, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Serving on port %d out of directory: %s", config.MoneyGo.Port, config.MoneyGo.Basedir)
|
// Get ServeMux for API and add our own handlers for files
|
||||||
if config.MoneyGo.Fcgi {
|
servemux := handlers.GetHandler(dbmap)
|
||||||
fcgi.Serve(listener, handler)
|
servemux.HandleFunc("/", FileHandlerFunc(rootHandler, cfg.MoneyGo.Basedir))
|
||||||
|
servemux.HandleFunc("/static/", FileHandlerFunc(staticHandler, cfg.MoneyGo.Basedir))
|
||||||
|
|
||||||
|
listener, err := net.Listen("tcp", ":"+strconv.Itoa(cfg.MoneyGo.Port))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Serving on port %d out of directory: %s", cfg.MoneyGo.Port, cfg.MoneyGo.Basedir)
|
||||||
|
if cfg.MoneyGo.Fcgi {
|
||||||
|
fcgi.Serve(listener, servemux)
|
||||||
} else {
|
} else {
|
||||||
http.Serve(listener, handler)
|
http.Serve(listener, servemux)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user