-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
233 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ vendor/*/ | |
src/src | ||
files/* | ||
bin | ||
.env |
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 |
---|---|---|
@@ -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"] |
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,11 @@ | ||
mywins: | ||
image: marcossegovia/mywins | ||
ports: | ||
- 8080:8080 | ||
volumes: | ||
- .:/data/db | ||
links: | ||
- db:db | ||
|
||
db: | ||
image: mongo |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gorilla/mux" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type Route struct { | ||
|
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 |
---|---|---|
@@ -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() | ||
} |
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,9 @@ | ||
package domain | ||
|
||
type PersistenceApiClient interface { | ||
GetWins() ([]*Entry, error) | ||
GetFails() ([]*Entry, error) | ||
AddWin() error | ||
AddFail() error | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.