gull/server.go

33 lines
724 B
Go
Raw Normal View History

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