gull/server.go

32 lines
673 B
Go
Raw Permalink 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 (
2020-05-25 05:17:43 +02:00
publicDir 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("/s/{alias:.*}", a.GetURL).Methods("GET")
2020-05-25 05:17:43 +02:00
r.PathPrefix("/").Handler(http.FileServer(http.Dir(publicDir))).Methods("GET")
http.Handle("/", r)
2020-05-25 05:17:43 +02:00
log.Println("Server is listening on port", port)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil))
}