Skip to content

Commit

Permalink
UI (#6)
Browse files Browse the repository at this point in the history
* add ui support
  • Loading branch information
dominikus1993 authored Mar 9, 2022
1 parent 0e6d08a commit 30ad61c
Show file tree
Hide file tree
Showing 19 changed files with 1,083 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"extensions": [
"golang.Go",
"GitHub.copilot",
"wakatime.vscode-wakatime"
"wakatime.vscode-wakatime",
"jinliming2.vscode-go-template"
],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
Expand Down
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"name": "Run Api",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
"program": "${fileDirname}",
"args": ["run-api"]
}
]
}
79 changes: 79 additions & 0 deletions cmd/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"context"
"flag"

"github.com/dominikus1993/dev-news-bot/internal/mongo"
"github.com/dominikus1993/dev-news-bot/pkg/common"
"github.com/dominikus1993/dev-news-bot/pkg/repositories"
"github.com/dominikus1993/dev-news-bot/pkg/usecase"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/template/html"
"github.com/google/subcommands"
log "github.com/sirupsen/logrus"
)

type RunDevNewsApi struct {
mongoConnectionString string
}

func (*RunDevNewsApi) Name() string { return "run-api" }
func (*RunDevNewsApi) Synopsis() string { return "run api" }
func (*RunDevNewsApi) Usage() string {
return `go run . run-api"`
}

func (p *RunDevNewsApi) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.mongoConnectionString, "mongo-connection-string", common.GetEnvOrDefault("MONGODB_CONNECTION", ""), "mongo connection string")
}

func (p *RunDevNewsApi) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
engine := html.New("./public", ".html")
log.WithField("mongo", p.mongoConnectionString).Infoln("Start DevNews Api")
mongodbClient, err := mongo.NewClient(ctx, p.mongoConnectionString)
if err != nil {
log.WithError(err).Error("can't create mongodb client")
return subcommands.ExitFailure
}
defer mongodbClient.Close(ctx)
repo := mongo.NewMongoArticlesRepository(mongodbClient, "Articles")
getArticles := usecase.NewGetArticlesUseCase(repo)
app := fiber.New(fiber.Config{
Views: engine,
})
app.Use(logger.New())

app.Get("/:page?", func(c *fiber.Ctx) error {
page := common.ParseInt(c.Params("page"), 1)
articles, err := getArticles.Execute(c.Context(), repositories.GetArticlesParams{Page: page, PageSize: 20})
if err != nil {
return err
}
return c.Render("index", fiber.Map{
"Articles": articles.Articles,
"PageTitle": "Dev News",
"NumberOfPages": articles.NumberOfPages,
"Total": articles.Total,
})
})

app.Get("/api/articles", func(c *fiber.Ctx) error {
c.Context().Logger().Printf("Get articles")
pageSize := common.ParseInt(c.Query("pageSize"), 10)
page := common.ParseInt(c.Query("page"), 1)
log.WithField("pageSize", pageSize).WithField("page", page).Infoln("get articles")
res, err := getArticles.Execute(c.Context(), repositories.GetArticlesParams{Page: page, PageSize: pageSize})
if err != nil {
return err
}
return c.Status(200).JSON(res)
})

if err := app.Listen(":3000"); err != nil {
log.WithError(err).Error("can't start server")
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}
18 changes: 12 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,49 @@ module github.com/dominikus1993/dev-news-bot
go 1.17

require (
github.com/bwmarrin/discordgo v0.23.2
github.com/bwmarrin/discordgo v0.24.0
github.com/gocolly/colly/v2 v2.1.0
github.com/gofiber/fiber/v2 v2.28.0
github.com/google/subcommands v1.2.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
go.mongodb.org/mongo-driver v1.8.3
go.mongodb.org/mongo-driver v1.8.4
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
)

require (
github.com/PuerkitoBio/goquery v1.8.0 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/antchfx/htmlquery v1.2.4 // indirect
github.com/antchfx/xmlquery v1.3.9 // indirect
github.com/antchfx/xpath v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gofiber/template v1.6.24 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/klauspost/compress v1.14.4 // indirect
github.com/klauspost/compress v1.15.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
github.com/temoto/robotstxt v1.1.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.33.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.0 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit 30ad61c

Please sign in to comment.