Remove database

This commit is contained in:
Dave Gallant
2022-08-21 14:35:43 +00:00
parent e9c470a817
commit dcfc3231ab
9 changed files with 37 additions and 82 deletions

1
.gitignore vendored
View File

@@ -1,5 +1,4 @@
node_modules
**.db
backend/bin/
.vscode
*.pem

25
Makefile Normal file
View File

@@ -0,0 +1,25 @@
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
## 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
## backend-server: Build and run the backend from source
backend-server:
@cd backend && go run .
.PHONY: server
## frontend-server: Build and run the frontend from source
frontend-server:
@npx vue-cli-service serve
.PHONY: server

View File

@@ -2,19 +2,11 @@
FROM golang:1.18-alpine as go-build
# hadolint ignore=DL3018
RUN apk --no-cache add \
gcc \
musl-dev
COPY . /backend
WORKDIR /backend
RUN CGO_ENABLED=1 GOOS=linux \
go build -o server \
# Additional flags are necessary for sqlite support
-a -ldflags '-linkmode external -extldflags "-static"' .
RUN CGO_ENABLED=0 go build -o server
EXPOSE 8080

View File

@@ -10,8 +10,6 @@ import (
"github.com/rs/zerolog/log"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// @title RFD FYI API
@@ -30,21 +28,14 @@ import (
// @BasePath /api/v1
type App struct {
DB *gorm.DB
Router *mux.Router
BasePath string
CurrentTopics []Topic
}
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
func (a *App) Initialize() {
a.BasePath = "/api/v1"
a.DB.AutoMigrate(&Topic{})
a.Router = mux.NewRouter().PathPrefix(a.BasePath).Subrouter()
http.Handle("/", a.Router)
@@ -56,7 +47,6 @@ func (a *App) Run(httpPort string) {
if err := http.ListenAndServe(fmt.Sprintf(":"+httpPort), nil); err != nil {
panic(err)
}
defer a.DB.Close()
}
func (a *App) initializeRoutes() {
@@ -84,20 +74,14 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
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)
// only drop deals if a timer has been met
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)
}
latestTopics := a.getDeals(9, 1, 4)
// TODO: only drop deals if a timer has been met
log.Debug().Msg("Refreshing deals")
a.CurrentTopics = latestTopics
}
func (a *App) getDeals(id int, firstPage int, lastPage int) []Topic {

View File

@@ -4,15 +4,12 @@ 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
)

View File

@@ -1,49 +1,17 @@
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=

View File

@@ -5,8 +5,6 @@ import (
_ "github.com/joho/godotenv/autoload"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@@ -33,19 +31,9 @@ func main() {
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.Initialize()
a.Run(httpPort)
}

View File

@@ -14,6 +14,7 @@ type Topic struct {
PostTime string `json:"post_time"`
LastPostTime string `json:"last_post_time"`
Votes Votes
Score string `json:",omitempty"`
} // @name Topic
type Votes struct {

View File

@@ -38,6 +38,7 @@
></a>
</td>
<td scope="col">{{ topic.total_views }}</td>
<td scope="col">{{ topic.score }}</td>
<!-- <td scope="col">{{ topic.votes.up - topic.votes.down }}</td> -->
<td scope="col">{{ formatDate(topic.last_post_time) }}</td>
</tr>