Reorganize things a bit
Removed the server object and moved the business layer into the main namespace. This turns the main namespace into a library instead of a server implementation. All of the http specific stuff (aside from everything in frontend/) has been moved to the server commmand utility in cmd/server.go.
This commit is contained in:
parent
3d6c1a444f
commit
eeba54808b
|
@ -4,7 +4,7 @@ import (
|
|||
"os"
|
||||
"fmt"
|
||||
|
||||
"github.com/zorchenhimer/hacker-quotes/business"
|
||||
"github.com/zorchenhimer/hacker-quotes"
|
||||
"github.com/zorchenhimer/hacker-quotes/database"
|
||||
)
|
||||
|
||||
|
@ -22,7 +22,7 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
hq, err := business.NewGeneric(db)
|
||||
hq, err := hacker.NewGeneric(db)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
|
|
|
@ -1,18 +1,105 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"github.com/zorchenhimer/hacker-quotes"
|
||||
"github.com/zorchenhimer/hacker-quotes/database"
|
||||
"github.com/zorchenhimer/hacker-quotes/frontend"
|
||||
)
|
||||
|
||||
func main() {
|
||||
server, err := hacker.New("settings.json")
|
||||
settings, err := loadSettings("settings.json")
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to load settings: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
db, err := database.New(settings.DatabaseType, settings.ConnectionString)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to load database type %s: %s\n", settings.DatabaseType, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
hack, err := hacker.NewGeneric(db)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
server.Hack()
|
||||
if db.IsNew() {
|
||||
fmt.Println("database is new")
|
||||
err = hack.InitData("word_lists.json")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("database isn't new")
|
||||
}
|
||||
|
||||
|
||||
web, err := frontend.New(hack)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to load frontend: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
//mux.Handle("/api", api)
|
||||
mux.Handle("/", web)
|
||||
|
||||
hs := &http.Server{
|
||||
Addr: ":8080",
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
if err := hs.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
fmt.Println("Error running HTTP server:", err)
|
||||
}
|
||||
}
|
||||
|
||||
type settings struct {
|
||||
DatabaseType database.DbType
|
||||
ConnectionString string
|
||||
|
||||
HttpAddr string
|
||||
}
|
||||
|
||||
func loadSettings(filename string) (*settings, error) {
|
||||
if !fileExists(filename) {
|
||||
return nil, fmt.Errorf("%q doesn't exist", filename)
|
||||
//return &settings{
|
||||
// HttpAddr: ":8080",
|
||||
//}, nil
|
||||
}
|
||||
|
||||
raw, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error reading file: %s", err)
|
||||
}
|
||||
|
||||
s := &settings{}
|
||||
if err = json.Unmarshal(raw, s); err != nil {
|
||||
return nil, fmt.Errorf("Error unmarshaling: %s", err)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// fileExists returns whether the given file or directory exists or not.
|
||||
// Taken from https://stackoverflow.com/a/10510783
|
||||
func fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -8,19 +8,19 @@ import (
|
|||
|
||||
//"github.com/zorchenhimer/hacker-quotes/models"
|
||||
//"github.com/zorchenhimer/hacker-quotes/database"
|
||||
"github.com/zorchenhimer/hacker-quotes/business"
|
||||
"github.com/zorchenhimer/hacker-quotes"
|
||||
)
|
||||
|
||||
type Frontend struct {
|
||||
//db database.DB
|
||||
bs business.HackerQuotes
|
||||
hq hacker.HackerQuotes
|
||||
//cookies *sessions.CookieStore
|
||||
templates map[string]*template.Template
|
||||
}
|
||||
|
||||
func New(bs business.HackerQuotes) (*Frontend, error) {
|
||||
func New(hq hacker.HackerQuotes) (*Frontend, error) {
|
||||
f := &Frontend{
|
||||
bs: bs,
|
||||
hq: hq,
|
||||
//cookies: sessions.NewCookieStore([]byte("some auth key"), []byte("some encrypt key")),
|
||||
}
|
||||
return f, nil
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
)
|
||||
|
||||
func (f *Frontend) home(w http.ResponseWriter, r *http.Request) {
|
||||
words, err := f.bs.Random()
|
||||
words, err := f.hq.Random()
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package business
|
||||
package hacker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -1,4 +1,4 @@
|
|||
package business
|
||||
package hacker
|
||||
|
||||
import (
|
||||
"github.com/zorchenhimer/hacker-quotes/models"
|
87
server.go
87
server.go
|
@ -1,87 +0,0 @@
|
|||
package hacker
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"fmt"
|
||||
|
||||
//"github.com/zorchenhimer/hacker-quotes/api"
|
||||
"github.com/zorchenhimer/hacker-quotes/business"
|
||||
"github.com/zorchenhimer/hacker-quotes/database"
|
||||
"github.com/zorchenhimer/hacker-quotes/frontend"
|
||||
//"github.com/zorchenhimer/hacker-quotes/models"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
db database.DB
|
||||
hs *http.Server
|
||||
bs business.HackerQuotes
|
||||
|
||||
settings *settings
|
||||
}
|
||||
|
||||
// New returns a new Server object with the settings from configFile.
|
||||
// If no file is specified, a default "settings.config" will be
|
||||
// created with default settings in the current working directory.
|
||||
func New(configFile string) (*Server, error) {
|
||||
s := &Server{}
|
||||
|
||||
if settings, err := loadSettings(configFile); err != nil {
|
||||
return nil, fmt.Errorf("Unable to load settings: %s", err)
|
||||
} else {
|
||||
s.settings = settings
|
||||
}
|
||||
|
||||
db, err := database.New(s.settings.DatabaseType, s.settings.ConnectionString)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to load database type %s: %s", s.settings.DatabaseType, err)
|
||||
}
|
||||
|
||||
s.db = db
|
||||
|
||||
bs, err := business.NewGeneric(db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.bs = bs
|
||||
|
||||
if s.db.IsNew() {
|
||||
fmt.Println("database is new")
|
||||
err = bs.InitData("word_lists.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
fmt.Println("database isn't new")
|
||||
}
|
||||
|
||||
|
||||
web, err := frontend.New(s.bs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to load frontend: %s", err)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
//mux.Handle("/api", api)
|
||||
mux.Handle("/", web)
|
||||
|
||||
hs := &http.Server{
|
||||
Addr: ":8080",
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
s.hs = hs
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Server) Hack() error {
|
||||
if err := s.hs.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
fmt.Println("Error running HTTP server:", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) Shutdown() error {
|
||||
return fmt.Errorf("Not implemented")
|
||||
}
|
51
settings.go
51
settings.go
|
@ -1,51 +0,0 @@
|
|||
package hacker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/zorchenhimer/hacker-quotes/database"
|
||||
)
|
||||
|
||||
type settings struct {
|
||||
DatabaseType database.DbType
|
||||
ConnectionString string
|
||||
|
||||
HttpAddr string
|
||||
}
|
||||
|
||||
func loadSettings(filename string) (*settings, error) {
|
||||
if !fileExists(filename) {
|
||||
return nil, fmt.Errorf("%q doesn't exist", filename)
|
||||
//return &settings{
|
||||
// HttpAddr: ":8080",
|
||||
//}, nil
|
||||
}
|
||||
|
||||
raw, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error reading file: %s", err)
|
||||
}
|
||||
|
||||
s := &settings{}
|
||||
if err = json.Unmarshal(raw, s); err != nil {
|
||||
return nil, fmt.Errorf("Error unmarshaling: %s", err)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// fileExists returns whether the given file or directory exists or not.
|
||||
// Taken from https://stackoverflow.com/a/10510783
|
||||
func fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
Loading…
Reference in New Issue