2020-05-12 05:02:50 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-05-12 10:57:27 +02:00
|
|
|
"github.com/aeolyus/gull/handlers"
|
2020-05-12 05:02:50 +02:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-05-13 07:33:51 +02:00
|
|
|
"os"
|
2020-05-14 05:33:38 +02:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PUBLIC_DIR string = "./public"
|
|
|
|
PORT int = 8081
|
2020-05-12 05:02:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
a := &handlers.App{}
|
2020-05-13 07:33:51 +02:00
|
|
|
os.MkdirAll("./data", os.ModePerm)
|
|
|
|
a.Initialize("sqlite3", "./data/data.db")
|
2020-05-12 05:02:50 +02:00
|
|
|
defer a.DB.Close()
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2020-05-12 07:52:45 +02:00
|
|
|
r.HandleFunc("/", a.CreateShortURL).Methods("POST")
|
2020-05-12 05:02:50 +02:00
|
|
|
r.HandleFunc("/all", a.ListAll).Methods("GET")
|
2020-05-12 05:15:20 +02:00
|
|
|
r.HandleFunc("/s/{alias:.*}", a.GetURL).Methods("GET")
|
2020-05-14 05:33:38 +02:00
|
|
|
r.PathPrefix("/").Handler(http.FileServer(http.Dir(PUBLIC_DIR))).Methods("GET")
|
2020-05-12 05:02:50 +02:00
|
|
|
http.Handle("/", r)
|
|
|
|
|
2020-05-14 05:33:38 +02:00
|
|
|
log.Println("Server is listening on port", PORT)
|
|
|
|
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(PORT), nil))
|
2020-05-12 05:02:50 +02:00
|
|
|
}
|