-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (50 loc) · 1.5 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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"time"
"l24.dev/shortener"
"github.com/gorilla/mux"
"github.com/pressly/goose/v3"
"github.com/rs/cors"
)
func main() {
host := os.Getenv("DB_HOST")
user := os.Getenv("DB_USER")
pass := os.Getenv("DB_PASS")
name := os.Getenv("DB_NAME")
dbstring := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable", host, user, pass, name)
driver := "postgres"
db, err := sql.Open(driver, dbstring)
if err != nil {
log.Fatalf("failed to open to database: %v", err)
}
err = goose.Up(db, "migrations")
if err != nil {
log.Fatalf("failed to run migrations: %v", err)
}
router := mux.NewRouter()
dao := shortener.NewShortPostgresDao(db, driver)
getShortHandler := shortener.NewGetShortHandler(dao)
createShortHandler := shortener.NewCreateShortHandler(dao)
router.HandleFunc("/{short}", getShortHandler).Methods(http.MethodGet)
router.HandleFunc("/short", createShortHandler).Methods(http.MethodPost)
router.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { rw.WriteHeader(200) })
c := cors.New(cors.Options{
AllowedOrigins: []string{"https://shortener.dev"},
AllowCredentials: true,
AllowedHeaders: []string{"*"},
AllowedMethods: []string{"GET", "HEAD", "POST"},
})
srv := &http.Server{
Handler: c.Handler(router),
Addr: "0.0.0.0:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Print("starting server on port 8080")
log.Fatal(srv.ListenAndServe())
}