mirror of
				https://github.com/aclindsa/moneygo.git
				synced 2025-10-29 01:13:25 -04:00 
			
		
		
		
	First pass at reorganizing go code into sub-packages
This commit is contained in:
		
							
								
								
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -3,5 +3,5 @@ static/bundle.js | ||||
| static/codemirror | ||||
| static/react-widgets | ||||
| node_modules | ||||
| cusip_list.csv | ||||
| security_templates.go | ||||
| internal/handlers/cusip_list.csv | ||||
| internal/handlers/security_templates.go | ||||
|   | ||||
							
								
								
									
										8
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								Makefile
									
									
									
									
									
								
							| @@ -1,6 +1,6 @@ | ||||
| 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: | ||||
| 	npm install | ||||
| @@ -15,10 +15,4 @@ static/codemirror/codemirror.css: node_modules/codemirror/lib/codemirror.js node | ||||
| 	mkdir -p static/codemirror | ||||
| 	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 | ||||
|   | ||||
| @@ -47,7 +47,8 @@ You'll then want to build everything (the Golang and Javascript portions) using | ||||
| something like: | ||||
|  | ||||
| 	$ 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 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 ( | ||||
| 	"errors" | ||||
| @@ -59,7 +59,7 @@ type Config struct { | ||||
| 	MoneyGo MoneyGo | ||||
| } | ||||
| 
 | ||||
| func readConfig(filename string) (*Config, error) { | ||||
| func ReadConfig(filename string) (*Config, error) { | ||||
| 	cfg := Config{ | ||||
| 		MoneyGo: MoneyGo{ | ||||
| 			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 ( | ||||
| 	"encoding/json" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"github.com/yuin/gopher-lua" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"github.com/yuin/gopher-lua" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"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 ( | ||||
| 	"encoding/json" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"errors" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"gopkg.in/gorp.v1" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"github.com/yuin/gopher-lua" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"github.com/yuin/gopher-lua" | ||||
| @@ -104,7 +104,7 @@ def main(): | ||||
|     currency_list = get_currency_list() | ||||
|     cusip_list = get_cusip_list('cusip_list.csv') | ||||
| 
 | ||||
|     print("package main\n") | ||||
|     print("package handlers\n") | ||||
|     print("var SecurityTemplates = []Security{") | ||||
|     print(currency_list.unicode()) | ||||
|     print(cusip_list.unicode()) | ||||
| @@ -1,4 +1,6 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| //go:generate make | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"crypto/rand" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"crypto/sha256" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package main | ||||
| package handlers | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
							
								
								
									
										70
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										70
									
								
								main.go
									
									
									
									
									
								
							| @@ -3,8 +3,11 @@ package main | ||||
| //go:generate make | ||||
|  | ||||
| import ( | ||||
| 	"database/sql" | ||||
| 	"flag" | ||||
| 	"gopkg.in/gorp.v1" | ||||
| 	"github.com/aclindsa/moneygo/internal/config" | ||||
| 	"github.com/aclindsa/moneygo/internal/db" | ||||
| 	"github.com/aclindsa/moneygo/internal/handlers" | ||||
| 	"log" | ||||
| 	"net" | ||||
| 	"net/http" | ||||
| @@ -15,19 +18,19 @@ import ( | ||||
| ) | ||||
|  | ||||
| var configFile string | ||||
| var config *Config | ||||
| var cfg *config.Config | ||||
|  | ||||
| func init() { | ||||
| 	var err error | ||||
| 	flag.StringVar(&configFile, "config", "/etc/moneygo/config.ini", "Path to config file") | ||||
| 	flag.Parse() | ||||
|  | ||||
| 	config, err = readConfig(configFile) | ||||
| 	cfg, err = config.ReadConfig(configFile) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	static_path := path.Join(config.MoneyGo.Basedir, "static") | ||||
| 	static_path := path.Join(cfg.MoneyGo.Basedir, "static") | ||||
|  | ||||
| 	// Ensure base directory is valid | ||||
| 	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) | ||||
| } | ||||
|  | ||||
| func rootHandler(w http.ResponseWriter, r *http.Request) { | ||||
| 	http.ServeFile(w, r, path.Join(config.MoneyGo.Basedir, "static/index.html")) | ||||
| } | ||||
| type FileHandler func(http.ResponseWriter, *http.Request, string) | ||||
|  | ||||
| func staticHandler(w http.ResponseWriter, r *http.Request) { | ||||
| 	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 { | ||||
| func FileHandlerFunc(h FileHandler, basedir string) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		h(w, r, db) | ||||
| 		h(w, r, basedir) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func GetHandler(db *DB) http.Handler { | ||||
| 	servemux := http.NewServeMux() | ||||
| 	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)) | ||||
| func rootHandler(w http.ResponseWriter, r *http.Request, basedir string) { | ||||
| 	http.ServeFile(w, r, path.Join(basedir, "static/index.html")) | ||||
| } | ||||
|  | ||||
| 	return servemux | ||||
| func staticHandler(w http.ResponseWriter, r *http.Request, basedir string) { | ||||
| 	http.ServeFile(w, r, path.Join(basedir, r.URL.Path)) | ||||
| } | ||||
|  | ||||
| func main() { | ||||
| 	database, err := initDB(config) | ||||
| 	database, err := sql.Open(cfg.MoneyGo.DBType.String(), cfg.MoneyGo.DSN) | ||||
| 	if err != nil { | ||||
| 		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 { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	log.Printf("Serving on port %d out of directory: %s", config.MoneyGo.Port, config.MoneyGo.Basedir) | ||||
| 	if config.MoneyGo.Fcgi { | ||||
| 		fcgi.Serve(listener, handler) | ||||
| 	// Get ServeMux for API and add our own handlers for files | ||||
| 	servemux := handlers.GetHandler(dbmap) | ||||
| 	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 { | ||||
| 		http.Serve(listener, handler) | ||||
| 		http.Serve(listener, servemux) | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user