diff --git a/cmd/generate.go b/cmd/generate.go index cf8627d..b8d5967 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -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) diff --git a/cmd/server.go b/cmd/server.go index f93127f..a161ee7 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -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 } diff --git a/frontend/frontend.go b/frontend/frontend.go index 3982659..675a224 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -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 diff --git a/frontend/home.go b/frontend/home.go index 325c55e..5c9c626 100644 --- a/frontend/home.go +++ b/frontend/home.go @@ -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 diff --git a/business/generic.go b/generic.go similarity index 99% rename from business/generic.go rename to generic.go index 4d3818a..decb930 100644 --- a/business/generic.go +++ b/generic.go @@ -1,4 +1,4 @@ -package business +package hacker import ( "encoding/json" diff --git a/business/business.go b/hacker.go similarity index 97% rename from business/business.go rename to hacker.go index 420a78c..e4b1ba1 100644 --- a/business/business.go +++ b/hacker.go @@ -1,4 +1,4 @@ -package business +package hacker import ( "github.com/zorchenhimer/hacker-quotes/models" diff --git a/server.go b/server.go deleted file mode 100644 index 948058d..0000000 --- a/server.go +++ /dev/null @@ -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") -} diff --git a/settings.go b/settings.go deleted file mode 100644 index 2038396..0000000 --- a/settings.go +++ /dev/null @@ -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 -}