Skip to content

Commit

Permalink
changes to RunServer, added check for guide description
Browse files Browse the repository at this point in the history
  • Loading branch information
crmejia committed Sep 7, 2022
1 parent 85cb998 commit 43670e2
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 42 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# City Guide

Is an application that allows to create a city guide of your
favorite spots in a city and share them with other people.
Is an application to help you discover and share cool spots in cities.
These guides are useful for backpackers, digital nomads, and
people new in town that are keen to discover cool things.

current target: hello world: html with map of SC
people new in town that are keen to discover cool things.
7 changes: 5 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import "guide"
import (
"guide"
"os"
)

func main() {
guide.ServerRun(":8080")
guide.RunServer(os.Args[1:], os.Stdout)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.19

require (
github.com/mattn/go-sqlite3 v1.14.15
github.com/mitchellh/go-homedir v1.1.0
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI=
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
45 changes: 32 additions & 13 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"embed"
"errors"
"fmt"
"github.com/mitchellh/go-homedir"
"html/template"
"log"
"io"
"net/http"
"strconv"
"strings"
Expand All @@ -14,9 +15,10 @@ import (
type Server struct {
store store
*http.Server
output io.Writer
}

func NewServer(address string, store store) (Server, error) {
func NewServer(address string, store store, output io.Writer) (Server, error) {
if address == "" {
return Server{}, errors.New("server address cannot be empty")
}
Expand All @@ -29,6 +31,7 @@ func NewServer(address string, store store) (Server, error) {
Server: &http.Server{
Addr: address,
},
output: output,
}

server.Handler = server.routes()
Expand All @@ -44,7 +47,7 @@ func (s *Server) HandleIndex() http.HandlerFunc {
}
}

func (c *Server) HandleGuide() http.HandlerFunc {
func (s *Server) HandleGuide() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
p := strings.Split(r.URL.Path, "/")
if len(p) < 3 {
Expand All @@ -58,12 +61,12 @@ func (c *Server) HandleGuide() http.HandlerFunc {
http.Error(w, "not able to parse guide ID", http.StatusBadRequest)
return
}
g, err := c.store.GetGuide(id)
g, err := s.store.GetGuide(id)
if err != nil {
http.Error(w, "guide Not Found", http.StatusNotFound)
return
}
g.Pois = c.store.GetAllPois(id)
g.Pois = s.store.GetAllPois(id)

render(w, r, "templates/guide.html", g)
}
Expand All @@ -82,7 +85,7 @@ func (s *Server) HandleCreateGuide() http.HandlerFunc {
Latitude: r.PostFormValue("latitude"),
Longitude: r.PostFormValue("longitude"),
}
g, err := s.store.CreateGuide(guideForm.Name, GuideWithValidStringCoordinates(guideForm.Latitude, guideForm.Longitude))
g, err := s.store.CreateGuide(guideForm.Name, GuideWithValidStringCoordinates(guideForm.Latitude, guideForm.Longitude), GuideWithDescription(guideForm.Description))
if err != nil {
guideForm.Errors = append(guideForm.Errors, err.Error())
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -137,21 +140,37 @@ func (s *Server) HandleCreatePoi() http.HandlerFunc {
}
}
func (s *Server) Run() {
log.Println("starting http server")
fmt.Fprintln(s.output, "starting http server")
err := s.ListenAndServe()
if err != http.ErrServerClosed {
log.Fatal(err)
fmt.Fprintln(s.output, err)
return
}
}

func ServerRun(address string) {
store, err := OpenSQLiteStore("city_guide.db")
func RunServer(args []string, output io.Writer) {
if len(args) == 0 {
fmt.Fprintln(output, "no address provided")
return
}
if len(args) > 1 {
fmt.Fprintln(output, "too many args provided")
return
}
homeDir, err := homedir.Dir()
if err != nil {
log.Fatal(err)
fmt.Fprintln(output, err)
return
}
s, err := NewServer(address, &store)
store, err := OpenSQLiteStore(homeDir + "/city_guide.db")
if err != nil {
log.Fatal(err)
fmt.Fprintln(output, err)
return
}
s, err := NewServer(args[0], &store, output)
if err != nil {
fmt.Fprintln(output, err)
return
}
s.Run()
}
Expand Down
48 changes: 30 additions & 18 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)

func TestNewServerErrors(t *testing.T) {
t.Parallel()
s := guide.OpenMemoryStore()
_, err := guide.NewServer("", &s)
_, err := guide.NewServer("", &s, os.Stdout)
if err == nil {
t.Errorf("want error on empty server address")
}

_, err = guide.NewServer("address", nil)
_, err = guide.NewServer("address", nil, os.Stdout)
if err == nil {
t.Errorf("want error on nil store")
}
Expand All @@ -28,16 +29,19 @@ func TestNewServerErrors(t *testing.T) {
func TestIndexHandler(t *testing.T) {
t.Parallel()
store := guide.OpenMemoryStore()
store.CreateGuide("Nairobi", guide.GuideWithValidStringCoordinates("10", "10"))
store.CreateGuide("Fukuoka", guide.GuideWithValidStringCoordinates("10", "10"))
store.CreateGuide("Guia de Restaurates Roma, CDMX", guide.GuideWithValidStringCoordinates("10", "10"))
_, err := store.CreateGuide("Nairobi", guide.GuideWithValidStringCoordinates("10", "10"))
_, err = store.CreateGuide("Fukuoka", guide.GuideWithValidStringCoordinates("10", "10"))
_, err = store.CreateGuide("Guia de Restaurates Roma, CDMX", guide.GuideWithValidStringCoordinates("10", "10"))
if err != nil {
t.Fatal(err)
}

freePort, err := freeport.GetFreePort()
if err != nil {
t.Fatal(err)
}
address := fmt.Sprintf("localhost:%d", freePort)
server, err := guide.NewServer(address, &store)
server, err := guide.NewServer(address, &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -71,7 +75,7 @@ func TestGuideHandlerRendersMap(t *testing.T) {
t.Fatal(err)
}

server, err := guide.NewServer("localhost:8080", &store)
server, err := guide.NewServer("localhost:8080", &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand All @@ -98,7 +102,7 @@ func TestGuideHandlerRendersMap(t *testing.T) {
func TestGuideHandlerRenders404NotFound(t *testing.T) {
t.Parallel()
store := guide.OpenMemoryStore()
server, err := guide.NewServer("localhost:8080", &store)
server, err := guide.NewServer("localhost:8080", &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -131,7 +135,7 @@ func TestGuideHandlerRenders400NoId(t *testing.T) {
t.Fatal(err)
}
address := fmt.Sprintf("localhost:%d", freePort)
server, err := guide.NewServer(address, &store)
server, err := guide.NewServer(address, &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -159,7 +163,7 @@ func TestGuideHandlerRenders400NoId(t *testing.T) {
func TestCreateGuideHandlerGetRendersForm(t *testing.T) {
t.Parallel()
store := guide.OpenMemoryStore()
server, err := guide.NewServer("localhost:8080", &store)
server, err := guide.NewServer("localhost:8080", &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand All @@ -186,7 +190,7 @@ func TestCreateGuideHandlerGetRendersForm(t *testing.T) {
func TestCreateGuideHandlerPostCreatesGuide(t *testing.T) {
t.Parallel()
store := guide.OpenMemoryStore()
server, err := guide.NewServer("localhost:8080", &store)
server, err := guide.NewServer("localhost:8080", &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand All @@ -204,6 +208,14 @@ func TestCreateGuideHandlerPostCreatesGuide(t *testing.T) {
if len(store.Guides) != 1 {
t.Error("want store to contain new guide")
}

g, err := store.GetGuide(store.NextGuideKey - 1)
if err != nil {
t.Fatal(err)
}
if g.Description == "" {
t.Error("want guide description to not be empty")
}
}

func TestCreateGuideHandlerPostFormErrors(t *testing.T) {
Expand All @@ -224,7 +236,7 @@ func TestCreateGuideHandlerPostFormErrors(t *testing.T) {
t.Fatal(err)
}
address := fmt.Sprintf("localhost:%d", freePort)
server, err := guide.NewServer(address, &store)
server, err := guide.NewServer(address, &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -257,7 +269,7 @@ func TestCreatePoiHandlerGetRendersForm(t *testing.T) {
if err != nil {
t.Fatal(err)
}
server, err := guide.NewServer("localhost:8080", &store)
server, err := guide.NewServer("localhost:8080", &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -293,7 +305,7 @@ func TestCreatePoiHandlerErrors(t *testing.T) {
{"/guide/poi/create/1", http.StatusNotFound, "guide not found"},
}
store := guide.OpenMemoryStore()
server, err := guide.NewServer("localhost:8080", &store)
server, err := guide.NewServer("localhost:8080", &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -333,7 +345,7 @@ func TestCreatePoiHandlerFormErrors(t *testing.T) {
store := guide.OpenMemoryStore()
g, err := store.CreateGuide("test", guide.GuideWithValidStringCoordinates("10", "10"))

server, err := guide.NewServer("localhost:8080", &store)
server, err := guide.NewServer("localhost:8080", &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -369,12 +381,12 @@ func TestCreatePoiHandlerPost(t *testing.T) {
t.Fatal(err)
}

freeport, err := freeport.GetFreePort()
freePort, err := freeport.GetFreePort()
if err != nil {
t.Fatal(err)
}
address := fmt.Sprintf("localhost:%d", freeport)
server, err := guide.NewServer(address, &store)
address := fmt.Sprintf("localhost:%d", freePort)
server, err := guide.NewServer(address, &store, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions templates/guide.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,4 @@ <h1> {{.Name}}</h1>
<a href="/">back</a>
</div>
</body>
</html>

</html>
3 changes: 1 addition & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@
<a href="/guide/create">Create New Guide</a>
</div>
</body>
</html>

</html>

0 comments on commit 43670e2

Please sign in to comment.