-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
87 lines (69 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"database/sql"
"fmt"
"net/http"
"os"
"github.com/gorilla/mux"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
func main() {
// Open SQLite database
var err error
fmt.Println("Hello")
db, err = sql.Open("sqlite3", "url-shortener.db")
if err != nil {
fmt.Println("Error opening database:", err)
return
}
// Create a new router and endpoints
router := mux.NewRouter()
router.Use(CORSMiddleware)
router.HandleFunc("/", indexHandler)
router.HandleFunc("/{short}", redirectHandler)
http.Handle("/", router)
// Handle all assets in the assets folder
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("./assets"))))
// for Railway deployment
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
// Start the server
http.ListenAndServe("0.0.0.0:" + port, nil)
}
func CORSMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
handleShorten(w, r)
return
}
renderTemplate(w, "index.html", nil)
}
func redirectHandler(w http.ResponseWriter, r *http.Request) {
// Slug extraction
vars := mux.Vars(r)
slug := vars["short"]
// Fetch the actual URL
info, found := getURLInfo(slug)
if found {
fmt.Println("Redirecting to:", info.LongURL)
http.Redirect(w, r, info.LongURL, http.StatusFound)
} else {
fmt.Println("URL not found, returning 404")
http.NotFound(w, r)
}
}