57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
func main() {
|
|
config, err := loadConfig()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Println("config loaded")
|
|
|
|
_, err = initDatabase(config.DatabasePath)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Println("database initialized")
|
|
|
|
if err := os.MkdirAll(config.UploadDir, 0700); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
pages, err := template.ParseFiles("templates/home.html", "templates/paste.html")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
|
|
data := HomePageData{
|
|
Title: "minibin",
|
|
Description: "simple file and paste hosting",
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
if err := pages.ExecuteTemplate(w, "home.html", data); err != nil {
|
|
log.Println("render home page:", err)
|
|
}
|
|
})
|
|
mux.HandleFunc("GET /health", health)
|
|
|
|
log.Println("listening on http://localhost:8080")
|
|
|
|
err = http.ListenAndServe("localhost:8080", mux)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|