-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Much better SRP handling - now have a server and database handled pur…
…ely through model
- Loading branch information
1 parent
437ddff
commit 7d85638
Showing
10 changed files
with
113 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package lib | ||
|
||
import ( | ||
"database/sql" | ||
"journal/controller" | ||
"journal/model" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
// Server Contain the server | ||
type Server struct { | ||
db *sql.DB | ||
router Router | ||
} | ||
|
||
// CheckErr Check and fatal if applicable | ||
func (s Server) CheckErr(err error) { | ||
if err != nil { | ||
log.Fatal("Error reported: ", err) | ||
} | ||
} | ||
|
||
func (s *Server) createDb() { | ||
err := model.JournalCreateTable() | ||
s.CheckErr(err) | ||
log.Println("Database created") | ||
} | ||
|
||
func (s *Server) initRouter() { | ||
s.router = NewRouter(s, &controller.Error{}) | ||
s.router.Add("GET", "/", false, &controller.Index{}) | ||
s.router.Add("GET", "/new", false, &controller.New{}) | ||
s.router.Add("POST", "/new", false, &controller.New{}) | ||
s.router.Add("GET", "\\/([\\w\\-]+)", true, &controller.View{}) | ||
} | ||
|
||
// Run Run the server | ||
func (s *Server) Run(mode string, port string) { | ||
if mode == "create" { | ||
s.createDb() | ||
} else { | ||
s.initRouter() | ||
s.serve(port) | ||
} | ||
s.db.Close() | ||
} | ||
|
||
func (s *Server) serve(port string) { | ||
log.Printf("Listening on port %s\n", port) | ||
log.Fatal(http.ListenAndServe(":"+port, &s.router)) | ||
} | ||
|
||
// NewServer Create an instance of the server | ||
func NewServer() Server { | ||
return Server{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.