Add initial frontend and backend

This commit is contained in:
Dave Gallant
2022-07-31 02:58:08 +00:00
parent 6709422ba0
commit 3173b47951
23 changed files with 6760 additions and 1 deletions

3
backend/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"makefile.extensionOutputFolder": "./.vscode"
}

59
backend/Makefile Normal file
View File

@@ -0,0 +1,59 @@
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
BASE_PATH ?= "http://localhost:8080"
## help: Print this help message
help:
@echo
@echo "Usage:"
@echo
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' | sort
@echo
.PHONY: help
## build: Build the binary
build:
@mkdir -p bin
go build -o bin/rfd-launcher
.PHONY: build
## test: Run tests in colour
test:
@go install github.com/rakyll/gotest@latest
gotest -v -count=1
.PHONY: test
## fmt: Format code (with gofumpt)
fmt:
@go install mvdan.cc/gofumpt@latest
gofumpt -w .
.PHONY: fmt
## swagger: Generate swagger docs
swagger:
@go install github.com/swaggo/swag/cmd/swag@latest
swag init --outputTypes yaml
.PHONY: swagger
## server: Build and run server from source
server:
@go run .
.PHONY: server
## container: Build a container image with Docker
container:
docker build . -t rfd-launcher
.PHONY: container
## container-run: Build and run a container with Docker
container-run: container
@docker run \
--network host \
-u "$$(id -u)":"$$(id -g)"\
-v "$$PWD":"/opt/rfd-launcher" \
rfd-launcher
.PHONY: container-run

124
backend/app.go Normal file
View File

@@ -0,0 +1,124 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
_ "github.com/joho/godotenv/autoload"
"github.com/rs/zerolog/log"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// @title RFD LAUNCHER API
// @version 1.0
// @description An API for issue tracking
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url https://linktr.ee/davegallant
// @contact.email davegallant@gmail.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /api/v1
type App struct {
DB *gorm.DB
Router *mux.Router
BasePath string
}
func (a *App) Initialize(dbDriver string, dbURI string) {
db, err := gorm.Open(dbDriver, dbURI)
if err != nil {
panic("failed to connect database")
}
a.DB = db
a.BasePath = "/api/v1"
a.DB.AutoMigrate(&Topic{})
a.Router = mux.NewRouter().PathPrefix(a.BasePath).Subrouter()
http.Handle("/", a.Router)
a.initializeRoutes()
}
func (a *App) Run(httpPort string) {
log.Info().Msgf("Serving requests on port " + httpPort)
if err := http.ListenAndServe(fmt.Sprintf(":"+httpPort), nil); err != nil {
panic(err)
}
defer a.DB.Close()
}
func (a *App) initializeRoutes() {
a.Router.HandleFunc("/topics", a.listTopics).Methods("GET")
}
func respondWithError(w http.ResponseWriter, code int, message string) {
respondWithJSON(w, code, map[string]string{"error": message})
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
// listtopics godoc
// @Summary Lists all topics stored in the database
// @Description All topics will be listed. There is currently no pagination implemented.
// @ID list-topics
// @Router /topics [get]
// @Success 200 {array} Topic
func (a *App) listTopics(w http.ResponseWriter, r *http.Request) {
var topics []Topic
a.refreshDeals()
a.DB.Find(&topics)
respondWithJSON(w, http.StatusOK, topics)
}
func (a *App) refreshDeals() {
topics := a.getDeals(9, 1, 10)
log.Debug().Msg("Dropping deals")
a.DB.DropTable(&Topic{})
log.Debug().Msg("Refreshing the deals")
a.DB.CreateTable(&Topic{})
for _, topic := range topics {
a.DB.Create(topic)
}
}
func (a *App) getDeals(id int, firstPage int, lastPage int) []Topic {
// TODO: Fetch multiple pages
requestURL := fmt.Sprintf("https://forums.redflagdeals.com/api/topics?forum_id=%d&per_page=40&page=%d", id, firstPage)
res, err := http.Get(requestURL)
if err != nil {
log.Warn().Msgf("error fetching deals: %s\n", err)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Warn().Msgf("could not read response body: %s\n", err)
}
var response TopicsResponse
err = json.Unmarshal([]byte(body), &response)
if err != nil {
log.Warn().Msgf("could not unmarshal response body: %s\n", err)
}
return response.Topics
}

18
backend/go.mod Normal file
View File

@@ -0,0 +1,18 @@
module github.com/davegallant/rfd-launcher
go 1.18
require (
github.com/gorilla/mux v1.8.0
github.com/jinzhu/gorm v1.9.16
github.com/joho/godotenv v1.4.0
github.com/rs/zerolog v1.27.0
)
require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.0 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
)

49
backend/go.sum Normal file
View File

@@ -0,0 +1,49 @@
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM=
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o=
github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.27.0 h1:1T7qCieN22GVc8S4Q2yuexzBb1EqjbgjSH9RohbMjKs=
github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Qc0U=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

51
backend/main.go Normal file
View File

@@ -0,0 +1,51 @@
package main
import (
"os"
_ "github.com/joho/godotenv/autoload"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
utils "github.com/davegallant/rfd-launcher/pkg/utils"
)
// @title RFD Launcher API
// @version 1.0
// @description An API for an issue tracking service
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /api/v1
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
a := &App{}
dbType := utils.GetEnv("DB_TYPE", "sqlite3")
httpPort := utils.GetEnv("HTTP_PORT", "8080")
sqlitePath := utils.GetEnv("SQLITE_DB_PATH", "rfd.db")
// Determine database
switch {
case dbType == "sqlite3":
log.Debug().Msgf("Using sqlite3 (path: " + sqlitePath + ")")
a.Initialize(dbType, sqlitePath)
default:
log.Fatal().Msgf("Unsupported database: " + dbType)
}
a.Run(httpPort)
}

22
backend/model.go Normal file
View File

@@ -0,0 +1,22 @@
package main
type TopicsResponse struct {
Topics []Topic `json:"topics"`
} // @name Topics
type Topic struct {
TopicID uint `json:"topic_id"`
ForumID uint `json:"forum_id"`
Title string `json:"title"`
Views int `json:"total_views"`
Replies int `json:"total_replies"`
WebPath string `json:"web_path"`
PostTime string `json:"post_time"`
LastPostTime string `json:"last_post_time"`
Votes Votes
} // @name Topic
type Votes struct {
Up int `json:"total_up"`
Down int `json:"total_down"`
} // @name Votes

View File

@@ -0,0 +1,12 @@
package utils
import (
"os"
)
func GetEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}