You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.8 KiB
76 lines
1.8 KiB
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"qurl/pages"
|
|
"qurl/static"
|
|
"qurl/storage"
|
|
"runtime"
|
|
)
|
|
|
|
//go:generate bindata -m Assets -r assets -p static -o static/assets.go assets
|
|
|
|
func main() {
|
|
dburl := flag.String("u", "bolt:./qurl.db", "url to database")
|
|
lsaddr := flag.String("l", "127.0.0.1:8080", "listen address/port")
|
|
jsonfile := flag.String("j", "", "path to json to load into database")
|
|
maxpro := flag.Int("m", runtime.NumCPU()+2,
|
|
"maximum number of threads to use")
|
|
flag.Parse()
|
|
|
|
if *maxpro < 3 {
|
|
fmt.Fprintf(os.Stderr, "Thread limit too low: %d (min 3)\n", *maxpro)
|
|
return
|
|
}
|
|
|
|
// Limit max processes
|
|
runtime.GOMAXPROCS(*maxpro)
|
|
|
|
// Open storage backend
|
|
stor, err := storage.NewStorage(*dburl)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Database connection error: %s\n", err.Error())
|
|
return
|
|
}
|
|
defer stor.Shutdown()
|
|
|
|
// Load data if asked
|
|
if *jsonfile != "" {
|
|
err := loadjson(stor, *jsonfile)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
// Open listener port
|
|
listen, err := net.Listen("tcp", *lsaddr)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Listen error: %s\n", err.Error())
|
|
return
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/index.html", &static.StaticContent{Content: "index.html"})
|
|
mux.Handle("/favicon.ico", &static.StaticContent{Content: "favicon.ico"})
|
|
mux.Handle("/qurl.css", &static.StaticContent{Content: "qurl.css"})
|
|
|
|
submit := &pages.SubmitHandler{Storage: stor}
|
|
err = submit.Init()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Submit init error: %s\n", err.Error())
|
|
return
|
|
}
|
|
|
|
mux.Handle("/submit.html", submit)
|
|
|
|
fmt.Fprintf(os.Stdout, "qurl listening .. \n")
|
|
err = http.Serve(listen, mux)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Serve error: %s\n", err.Error())
|
|
}
|
|
}
|