Skip to content

Commit

Permalink
Merge pull request #7 from zekroTJA/dev
Browse files Browse the repository at this point in the history
release 1.4
  • Loading branch information
zekroTJA authored Dec 6, 2019
2 parents b549c8e + 90f9e6a commit c2bc619
Show file tree
Hide file tree
Showing 22 changed files with 12,588 additions and 107 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ private.*
vendor/*

node_modules
package-lock.json
dist
bin/*

Expand Down
22 changes: 18 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@ language: go
go:
- tip

go_import_path: github.com/zekroTJA/myrunes

script:
- time bash ./ci/travis.sh
jobs:
include:

- stage: build
name: Build
script: time bash ./ci/travis.sh

- stage: docker-deploy
name: Docker Deploy Branch
if: branch = master OR branch = dev
script: bash ./ci/docker-deploy.sh

- stage: docker-deploy-tag
name: Docker Deploy Tag
if: tag IS present
script: |
docker build . -t zekro/myrunes:${TRAVIS_TAG}
docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
docker push zekro/myrunes:${TRAVIS_TAG}
37 changes: 8 additions & 29 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,52 +1,31 @@
FROM golang:1.13

FROM golang:1.13 as build
LABEL maintainer="zekro <contact@zekro.de>"

#### PREPARINGS #####

# install node.js
ARG RELEASE=TRUE
RUN curl -sL https://deb.nodesource.com/setup_13.x | bash - &&\
apt-get install -y nodejs

# install vue CLI
RUN npm i -g @vue/cli

# set workdir to go application dir
WORKDIR /var/myrunes

# add nessecary repository files
ADD . .

# ensure dependencies with go mod
RUN go mod tidy

# install web dependencies
RUN cd web &&\
npm i

npm ci
RUN mkdir ./bin

#### BUILD BACK END ####

RUN go build \
-v -o /app/myrunes -ldflags "\
-X github.com/zekroTJA/myrunes/internal/static.Release=TRUE \
-X github.com/zekroTJA/myrunes/internal/static.Release=${RELEASE} \
-X github.com/zekroTJA/myrunes/internal/static.AppVersion=$(git describe --tags)" \
./cmd/server/*.go

#### BUILD FRONT END ####

RUN cd ./web &&\
npm run build &&\
cd .. &&\
mkdir -p /app/web &&\
cp -r ./web/dist /app/web/dist


#### EXPOSE AND RUN SETTINGS ####
FROM debian:stretch-slim AS final
WORKDIR /app
COPY --from=build /app .

EXPOSE 8080

RUN mkdir -p /etc/myrunes

CMD ["/app/myrunes", "-c", "/etc/myrunes/config.yml"]
CMD ["/app/myrunes", "-c", "/etc/myrunes/config.yml", "-assets", "/app/web/dist"]
15 changes: 15 additions & 0 deletions ci/docker-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BRANCH=${TRAVIS_BRANCH}
RELEASE=TRUE

if [ "${BRANCH}" == "master" ]; then
BRANCH=latest
fi

if [ "${BRANCH}" == "dev" ]; then
BRANCH=canary
RELEASE=FALSE
fi

docker build . -t zekro/myrunes:${BRANCH} --build-arg RELEASE=${RELEASE}
docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
docker push zekro/myrunes:${BRANCH}
3 changes: 2 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

var (
flagConfig = flag.String("c", "config.yml", "config file location")
flagAssets = flag.String("assets", "", "assets location")
)

func main() {
Expand Down Expand Up @@ -73,7 +74,7 @@ func main() {
}()

logger.Info("WEBSERVER :: initialization")
ws := webserver.NewWebServer(db, cfg.WebServer)
ws := webserver.NewWebServer(db, cfg.WebServer, *flagAssets)
go func() {
if err := ws.ListenAndServeBlocking(); err != nil {
logger.Fatal("WEBSERVER :: failed starting web server: %s", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion internal/database/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Middleware interface {
DeleteUser(uid snowflake.ID) error

CreatePage(page *objects.Page) error
GetPages(uid snowflake.ID, sortLess func(i, j *objects.Page) bool) ([]*objects.Page, error)
GetPages(uid snowflake.ID, champion string, sortLess func(i, j *objects.Page) bool) ([]*objects.Page, error)
GetPage(uid snowflake.ID) (*objects.Page, error)
EditPage(page *objects.Page) (*objects.Page, error)
DeletePage(uid snowflake.ID) error
Expand Down
17 changes: 14 additions & 3 deletions internal/database/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func (m *MongoDB) EditUser(user *objects.User, login bool) (bool, error) {
oldUser.PassHash = user.PassHash
}

if user.PageOrder != nil {
oldUser.PageOrder = user.PageOrder
}

return true, m.insertOrUpdate(m.collections.users,
bson.M{"uid": oldUser.UID}, oldUser)
}
Expand All @@ -155,8 +159,15 @@ func (m *MongoDB) CreatePage(page *objects.Page) error {
return m.insert(m.collections.pages, page)
}

func (m *MongoDB) GetPages(uid snowflake.ID, sortLess func(i, j *objects.Page) bool) ([]*objects.Page, error) {
count, err := m.count(m.collections.pages, bson.M{"owner": uid})
func (m *MongoDB) GetPages(uid snowflake.ID, champion string, sortLess func(i, j *objects.Page) bool) ([]*objects.Page, error) {
var query bson.M
if champion != "" && champion != "general" {
query = bson.M{"owner": uid, "champions": champion}
} else {
query = bson.M{"owner": uid}
}

count, err := m.count(m.collections.pages, query)
if err != nil {
return nil, err
}
Expand All @@ -167,7 +178,7 @@ func (m *MongoDB) GetPages(uid snowflake.ID, sortLess func(i, j *objects.Page) b
return pages, nil
}

res, err := m.collections.pages.Find(ctxTimeout(5*time.Second), bson.M{"owner": uid})
res, err := m.collections.pages.Find(ctxTimeout(5*time.Second), query)
if err != nil {
return nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions internal/objects/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ var (
)

type User struct {
UID snowflake.ID `json:"uid"`
Username string `json:"username"`
DisplayName string `json:"displayname"`
PassHash []byte `json:"passhash,omitempty"`
LastLogin time.Time `json:"lastlogin"`
Created time.Time `json:"created"`
Favorites []string `json:"favorites"`
UID snowflake.ID `json:"uid"`
Username string `json:"username"`
DisplayName string `json:"displayname"`
PassHash []byte `json:"passhash,omitempty"`
LastLogin time.Time `json:"lastlogin"`
Created time.Time `json:"created"`
Favorites []string `json:"favorites"`
PageOrder map[string][]snowflake.ID `json:"pageorder"`
}

func NewUser(username, password string, authMiddleware auth.Middleware) (*User, error) {
Expand Down
71 changes: 69 additions & 2 deletions internal/webserver/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,16 @@ func (ws *WebServer) handlerCreatePage(ctx *routing.Context) error {

func (ws *WebServer) handlerGetPages(ctx *routing.Context) error {
user := ctx.Get("user").(*objects.User)
queryArgs := ctx.QueryArgs()

sortBy := string(queryArgs.Peek("sortBy"))
champion := string(queryArgs.Peek("champion"))
short := string(queryArgs.Peek("short"))

if champion == "" {
champion = "general"
}

sortBy := string(ctx.QueryArgs().Peek("sortBy"))
var sortFunc func(i, j *objects.Page) bool

switch sortBy {
Expand All @@ -188,13 +196,45 @@ func (ws *WebServer) handlerGetPages(ctx *routing.Context) error {
sortFunc = func(i, j *objects.Page) bool {
return comparison.Alphabetically(i.Title, j.Title)
}
case "custom":
if user.PageOrder != nil {
pageOrder, ok := user.PageOrder[champion]
if ok {
sortFunc = func(i, j *objects.Page) bool {
var pix, jix int
for ix, uid := range pageOrder {
if uid == i.UID {
pix = ix
} else if uid == j.UID {
jix = ix
}
}
return jix > pix
}
}
}
}

pages, err := ws.db.GetPages(user.UID, sortFunc)
pages, err := ws.db.GetPages(user.UID, champion, sortFunc)
if err != nil {
return jsonError(ctx, err, fasthttp.StatusInternalServerError)
}

if comparison.IsTrue(short) {
m := make(map[string]int)
for _, p := range pages {
for _, c := range p.Champions {
if _, ok := m[c]; !ok {
m[c] = 1
} else {
m[c]++
}
}
}

return jsonResponse(ctx, &listResponse{N: len(m), Data: m}, fasthttp.StatusOK)
}

return jsonResponse(ctx, &listResponse{N: len(pages), Data: pages}, fasthttp.StatusOK)
}

Expand Down Expand Up @@ -616,3 +656,30 @@ func (ws *WebServer) handlerDeleteAPIToken(ctx *routing.Context) error {

return jsonResponse(ctx, nil, fasthttp.StatusOK)
}

func (ws *WebServer) handlerPostPageOrder(ctx *routing.Context) error {
user := ctx.Get("user").(*objects.User)

queryArgs := ctx.QueryArgs()
champion := string(queryArgs.Peek("champion"))

if champion == "" {
champion = "general"
}

pageOrder := new(pageOrderRequest)
if err := parseJSONBody(ctx, pageOrder); err != nil {
return jsonError(ctx, err, fasthttp.StatusBadRequest)
}

if user.PageOrder == nil {
user.PageOrder = make(map[string][]snowflake.ID)
}

user.PageOrder[champion] = pageOrder.PageOrder
if _, err := ws.db.EditUser(user, false); err != nil {
return jsonError(ctx, err, fasthttp.StatusInternalServerError)
}

return jsonResponse(ctx, nil, fasthttp.StatusOK)
}
5 changes: 5 additions & 0 deletions internal/webserver/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package webserver
import (
"time"

"github.com/bwmarrin/snowflake"
"github.com/zekroTJA/myrunes/internal/objects"
)

Expand Down Expand Up @@ -39,3 +40,7 @@ type sessionsResponse struct {

CurrentlyConnectedID string `json:"currentlyconnectedid"`
}

type pageOrderRequest struct {
PageOrder []snowflake.ID `json:"pageorder"`
}
8 changes: 7 additions & 1 deletion internal/webserver/websrever.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ type WebServer struct {
config *Config
}

func NewWebServer(db database.Middleware, config *Config) (ws *WebServer) {
func NewWebServer(db database.Middleware, config *Config, assets string) (ws *WebServer) {
ws = new(WebServer)

if assets != "" {
fileHandlerStatic.Root = assets
}

ws.config = config
ws.db = db
ws.rlm = NewRateLimitManager()
Expand Down Expand Up @@ -99,6 +103,8 @@ func (ws *WebServer) registerHandlers() {
Delete(ws.auth.CheckRequestAuth, ws.handlerDeleteMe)
users.
Get("/<uname>", ws.handlerCheckUsername)
users.
Post("/me/pageorder", ws.auth.CheckRequestAuth, ws.handlerPostPageOrder)

pages := api.Group("/pages", ws.addHeaders, rlGlobal, ws.auth.CheckRequestAuth)
pages.
Expand Down
7 changes: 7 additions & 0 deletions pkg/comparison/istrue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package comparison

import "strings"

func IsTrue(s string) bool {
return s == "1" || strings.ToLower(s) == "true"
}
8 changes: 4 additions & 4 deletions web/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @format */

module.exports = {
presets: [
'@vue/app'
]
}
presets: ['@vue/app'],
};
Loading

0 comments on commit c2bc619

Please sign in to comment.