-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (44 loc) · 1.54 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/wavly/shawty/asserts"
"github.com/wavly/shawty/config"
"github.com/wavly/shawty/handlers"
"github.com/wavly/shawty/utils"
)
func main() {
// Creating the ServerMux router
router := http.NewServeMux()
// Get the env variables and other config options
config.Init(router)
// Serving static files
router.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
// Route for index page
router.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
w.Write(utils.StaticFile("./static/index.html"))
})
// Reading the URLS-SQL schema file
fileBytes, err := os.ReadFile("./schema/urls.sql")
asserts.NoErr(err, "Failed to read URLS-SQL schema file")
db := utils.ConnectDB()
defer db.Close()
// Create the URLs table in the database
_, err = db.Exec(string(fileBytes))
asserts.NoErr(err, "Error creating the URLs table in the database")
// Ping/Pong route
router.HandleFunc("GET /ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong\n"))
})
// Route for shortening the URL
router.HandleFunc("POST /", handlers.Main)
// Route for stats page
router.HandleFunc("GET /stat/{code}", handlers.Stats)
// Route to handle redirection
router.HandleFunc("GET /s/{code}", handlers.Redirect)
// API route for shortening the URL
router.HandleFunc("POST /shawty", handlers.Shawty)
fmt.Println("Listening on:", config.PORT)
asserts.NoErr(http.ListenAndServe("0.0.0.0:"+config.PORT, router), "Failed to start the server:")
}