Skip to content

Commit

Permalink
Merge branch 'mongodb'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcossegovia committed Oct 30, 2016
2 parents 140c9e8 + ae22d0d commit 4dafd43
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 120 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor/*/
src/src
files/*
bin
.env
64 changes: 38 additions & 26 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,44 +1,56 @@
## Prod/Staging

FROM alpine:latest

ENV GOPATH /go

RUN apk add --update git go make && \
git clone https://github.com/MarcosSegovia/MyWins.git /go/src/github.com/MarcosSegovia/MyWins &&\
go get github.com/Masterminds/glide &&\
cd /go/src/github.com/Masterminds/glide &&\
make install &&\
cd /go/src/github.com/MarcosSegovia/MyWins &&\
glide install &&\
cd /go/src/github.com/MarcosSegovia/MyWins/src &&\
go build -o /mywins *.go &&\
apk del go git &&\
mv /go/src/github.com/MarcosSegovia/MyWins/files /files &&\
rm -rf /go

EXPOSE 8080
CMD ["/mywins"]

## Local Development

#FROM alpine:latest
#MAINTAINER Marcos Segovia <velozmarkdrea@gmail.com>
#
#ENV GOPATH /go
#ENV DB_HOST localhost
#ENV DB_DBNAME mywins
#ENV DB_WINS_COLLECTION wins
#ENV DB_FAILS_COLLECTION fails
#
#COPY . /go/src/github.com/MarcosSegovia/MyWins
#
#RUN apk add --update git go make &&\
#RUN apk add --update git go make && \
# git clone https://github.com/MarcosSegovia/MyWins.git /go/src/github.com/MarcosSegovia/MyWins &&\
# go get github.com/Masterminds/glide &&\
# cd /go/src/github.com/Masterminds/glide &&\
# make install &&\
# cd /go/src/github.com/MarcosSegovia/MyWins &&\
# glide install &&\
# cd /go/src/github.com/MarcosSegovia/MyWins/src &&\
# go build -o /mywins *.go &&\
# mv /go/src/github.com/MarcosSegovia/MyWins/files /files &&\
# apk del go git &&\
# mv /go/src/github.com/MarcosSegovia/MyWins/files /files &&\
# rm -rf /go
#
#
#EXPOSE 8080
#CMD ["/mywins"]

## Local Development

FROM alpine:latest
MAINTAINER Marcos Segovia <velozmarkdrea@gmail.com>

ENV GOPATH /go

ENV DB_HOST localhost
ENV DB_DBNAME mywins
ENV DB_WINS_COLLECTION wins
ENV DB_FAILS_COLLECTION fails

COPY . /go/src/github.com/MarcosSegovia/MyWins

RUN apk add --update git go make &&\
go get github.com/Masterminds/glide &&\
cd /go/src/github.com/Masterminds/glide &&\
make install &&\
cd /go/src/github.com/MarcosSegovia/MyWins &&\
glide install &&\
cd /go/src/github.com/MarcosSegovia/MyWins/src &&\
go build -o /mywins *.go &&\
mv /go/src/github.com/MarcosSegovia/MyWins/files /files &&\
apk del go git &&\
rm -rf /go

EXPOSE 8080
CMD ["/mywins"]
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mywins:
image: marcossegovia/mywins
ports:
- 8080:8080
volumes:
- .:/data/db
links:
- db:db

db:
image: mongo
11 changes: 10 additions & 1 deletion glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ package: github.com/MarcosSegovia/MyWins
import:
- package: github.com/gorilla/mux
version: ^1.1.0
- package: github.com/joho/godotenv
version: ^1.0.0
- package: gopkg.in/mgo.v2
26 changes: 14 additions & 12 deletions src/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,41 @@ import (
"encoding/json"
"fmt"
"net/http"
"./wins/domain"

"github.com/MarcosSegovia/MyWins/src/wins/domain"
"github.com/MarcosSegovia/MyWins/src/wins/infrastructure/mongo"
)

func Welcome(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Welcome!!!")
}

func GetAllWins(w http.ResponseWriter, r *http.Request) {
api := domain.NewApi()
wins_response, err := api.FindAllWins()
api := domain.NewApi(mongo.NewMongoApiClient())
wins, err := api.FindAllWins()
if err != nil {
http.Error(w, domain.GeneralError, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
w.Write(buildResponse(wins_response.Success))
w.Write(buildResponse(wins))
}

func GetAllFails(w http.ResponseWriter, r *http.Request) {
api := domain.NewApi()
wins_response, err := api.FindAllWins()
api := domain.NewApi(mongo.NewMongoApiClient())
fails, err := api.FindAllFails()
if err != nil {
http.Error(w, domain.GeneralError, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
w.Write(buildResponse(wins_response.Fails))
w.Write(buildResponse(fails))
}

func AddWin(w http.ResponseWriter, r *http.Request) {
api := domain.NewApi()
api := domain.NewApi(mongo.NewMongoApiClient())
err := api.AddWin()
if err != nil {
http.Error(w, domain.GeneralError, http.StatusInternalServerError)
Expand All @@ -48,7 +50,7 @@ func AddWin(w http.ResponseWriter, r *http.Request) {
w.Write(buildResponse(response_string))
}
func AddFail(w http.ResponseWriter, r *http.Request) {
api := domain.NewApi()
api := domain.NewApi(mongo.NewMongoApiClient())
err := api.AddFail()
if err != nil {
http.Error(w, domain.GeneralError, http.StatusInternalServerError)
Expand All @@ -60,11 +62,11 @@ func AddFail(w http.ResponseWriter, r *http.Request) {
w.Write(buildResponse(response_string))
}

func buildResponse(slice_of_times []string) []byte {
func buildResponse(response interface{}) []byte {

json_encoded_times, err := json.Marshal(slice_of_times)
resp, err := json.Marshal(response)
if err != nil {

}
return json_encoded_times
return resp
}
3 changes: 2 additions & 1 deletion src/router.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/gorilla/mux"
"net/http"

"github.com/gorilla/mux"
)

type Route struct {
Expand Down
93 changes: 27 additions & 66 deletions src/wins/domain/api.go
Original file line number Diff line number Diff line change
@@ -1,89 +1,50 @@
package domain

import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"

"gopkg.in/mgo.v2/bson"
)

type MyWinsAPI struct{}
type Entry struct {
ID bson.ObjectId `json:"id" bson:"_id"`
Time int64 `json:"time" bson:"time"`
}

func NewEntry() *Entry {
entry := new(Entry)
entry.ID = bson.NewObjectId()
entry.Time = time.Now().Unix()
return entry
}

var api *MyWinsAPI

func NewApi() *MyWinsAPI {
type MyWinsAPI struct {
dbClient PersistenceApiClient
}

func NewApi(client PersistenceApiClient) *MyWinsAPI {

if api != nil {
return api
}
api := new(MyWinsAPI)
api := &MyWinsAPI{client}
return api
}

func (api *MyWinsAPI) FindAllWins() (*win, error) {
func (api *MyWinsAPI) FindAllWins() ([]*Entry, error) {
return api.dbClient.GetWins()
}

wins, err := readFileToDomain()
return wins, err
func (api *MyWinsAPI) FindAllFails() ([]*Entry, error) {
return api.dbClient.GetFails()
}

func (api *MyWinsAPI) AddWin() error {
wins, err := readFileToDomain()
if err != nil {
return err
}
wins.Success = append(wins.Success, time.Now().Format(time.RFC3339))
err = writeDomainToFile(wins)
if err != nil {
return err
}

return nil
return api.dbClient.AddWin()
}

func (api *MyWinsAPI) AddFail() error {
wins, err := readFileToDomain()
if err != nil {
return err
}
wins.Fails = append(wins.Fails, time.Now().Format(time.RFC3339))
err = writeDomainToFile(wins)
if err != nil {
return err
}

return nil
}

func readFileToDomain() (*win, error) {
absPath, _ := filepath.Abs("files/wins.json")
jsonFile, err := os.Open(absPath)

if err != nil {
log.Print("Error when trying to open json file", err.Error())
return nil, err
}

wins := new(win)
jsonParser := json.NewDecoder(jsonFile)
if err = jsonParser.Decode(&wins); err != nil {
log.Print("Error on parsing json file", err.Error())
return nil, err
}
return wins, nil
}

func writeDomainToFile(w *win) error {
serialized_wins, err := json.Marshal(w)
if err != nil {
return err
}
absPath, _ := filepath.Abs("files/wins.json")
err = ioutil.WriteFile(absPath, serialized_wins, 0644)
if err != nil {
log.Print("Could not write on the file", err.Error())
return err
}

return nil
return api.dbClient.AddFail()
}
9 changes: 9 additions & 0 deletions src/wins/domain/persistence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package domain

type PersistenceApiClient interface {
GetWins() ([]*Entry, error)
GetFails() ([]*Entry, error)
AddWin() error
AddFail() error
}

14 changes: 0 additions & 14 deletions src/wins/domain/wins.go

This file was deleted.

Loading

0 comments on commit 4dafd43

Please sign in to comment.