change db from sqlite3 to postgresql
This commit is contained in:
@@ -9,29 +9,32 @@ import (
|
||||
// Enpoint that returns a list of articles given search terms in the post
|
||||
// request of a search form. Uses the content template.
|
||||
func (app *App) UpSearch(w http.ResponseWriter, req *http.Request) {
|
||||
// construct search query
|
||||
searchTerms := req.FormValue("search")
|
||||
if searchTerms == "" {
|
||||
app.Index(w, req)
|
||||
return
|
||||
}
|
||||
|
||||
// get articles
|
||||
articles, err := app.articles.Search(searchTerms)
|
||||
if err != nil {
|
||||
// treat as no result
|
||||
//http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
// construct search query
|
||||
searchTerms := req.FormValue("search")
|
||||
if searchTerms == "" {
|
||||
app.Index(w, req)
|
||||
return
|
||||
}
|
||||
|
||||
// convert to viewmodel
|
||||
articleVMs := make([]*model.ArticleViewModel, 0, len(articles))
|
||||
for _, a := range articles {
|
||||
articleVMs = append(articleVMs, a.ViewModel())
|
||||
}
|
||||
|
||||
// render template
|
||||
t := template.Must(template.ParseFiles("assets/templates/article.html"))
|
||||
err = t.ExecuteTemplate(w, "content", articleVMs)
|
||||
if err != nil { http.Error(w, "Failed to render template", http.StatusInternalServerError); return; }
|
||||
// get articles
|
||||
articles, err := app.articles.Search(searchTerms)
|
||||
if err != nil {
|
||||
// treat as no result
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// convert to viewmodel
|
||||
articleVMs := make([]*model.ArticleViewModel, 0, len(articles))
|
||||
for _, a := range articles {
|
||||
articleVMs = append(articleVMs, a.ViewModel())
|
||||
}
|
||||
|
||||
// render template
|
||||
t := template.Must(template.ParseFiles("assets/templates/article.html"))
|
||||
err = t.ExecuteTemplate(w, "content", articleVMs)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to render template", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,11 @@ package main
|
||||
import (
|
||||
"crowsnest/internal/model/database"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
@@ -19,24 +17,17 @@ type App struct {
|
||||
func main() {
|
||||
// collect environement variables
|
||||
databaseURL := os.Getenv("DB_URL")
|
||||
dbDriver := os.Getenv("DB_DRIVER")
|
||||
|
||||
// connect to database
|
||||
var db *sql.DB
|
||||
var err error
|
||||
switch {
|
||||
case dbDriver == "sqlite3":
|
||||
db, err = sql.Open("sqlite3", databaseURL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
default:
|
||||
log.Fatal(errors.New("given DB_DRIVER is not supported"))
|
||||
db, err := sql.Open("postgres", databaseURL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// define app
|
||||
app := &App{
|
||||
articles: &database.ArticleModel{DB: db, DbDriver: dbDriver},
|
||||
articles: &database.ArticleModel{DB: db},
|
||||
}
|
||||
|
||||
// start web server
|
||||
|
||||
Reference in New Issue
Block a user