Skip to content

Commit 0b97514

Browse files
authored
Merge pull request #2 from Nyae44/add-logic
add shortening url logic and db persistence
2 parents c6568b1 + 1ad88b3 commit 0b97514

File tree

8 files changed

+23
-12
lines changed

8 files changed

+23
-12
lines changed

cmd/webapp/db.sqlite3

Whitespace-only changes.

cmd/webapp/main.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
package main
22

33
import (
4+
"database/sql"
5+
_ "github.com/mattn/go-sqlite3"
6+
"github.com/nyae44/url-shortener-app/internal/controllers"
7+
"github.com/nyae44/url-shortener-app/internal/db"
48
"log"
59
"net/http"
6-
"text/template"
710
)
811

912
func main() {
10-
http.HandleFunc("/", ShowHomePage)
11-
log.Fatal(http.ListenAndServe(":8080", nil))
12-
}
13-
14-
func ShowHomePage(w http.ResponseWriter, r *http.Request) {
15-
temp, err := template.ParseFiles("internal/views/index.html")
13+
sqlite, err := sql.Open("sqlite3", "db.sqlite")
1614
if err != nil {
17-
http.Error(w, err.Error(), http.StatusInternalServerError)
18-
return
15+
log.Fatal(err)
1916
}
20-
if err = temp.Execute(w, nil); err != nil {
21-
http.Error(w, err.Error(), http.StatusInternalServerError)
22-
return
17+
defer sqlite.Close()
18+
19+
if err := db.CreateTable(sqlite); err != nil {
20+
log.Fatal(err)
2321
}
22+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
23+
if r.URL.Path == "/" {
24+
controllers.ShowIndex(w, r)
25+
} else {
26+
controllers.Proxy(sqlite)(w, r)
27+
}
28+
})
29+
http.HandleFunc("/shorten", controllers.Shorten(sqlite))
30+
log.Fatal(http.ListenAndServe(":8080", nil))
2431
}

cmd/webapp/webapp

13.5 MB
Binary file not shown.

internal/controllers/index.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package controllers

internal/controllers/shorten.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package controllers

internal/db/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package db

internal/url/url.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package url

internal/views/shorten.html

Whitespace-only changes.

0 commit comments

Comments
 (0)