Refactor main into subdirs
This commit is contained in:
@@ -3,63 +3,20 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/db"
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/handlers"
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/metrics"
|
||||
)
|
||||
|
||||
type ParsedArguments struct {
|
||||
databaseName string
|
||||
}
|
||||
|
||||
var appConfig ParsedArguments
|
||||
|
||||
func main() {
|
||||
// Parse the arguments and construct the ParsedArguments
|
||||
appConfigRef, err := parseArguments()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
appConfig = *appConfigRef
|
||||
|
||||
db.Init(appConfig.databaseName)
|
||||
|
||||
// Export prometheus metrics
|
||||
go metrics.StartMetricsServer()
|
||||
|
||||
// Initialize Gorilla router
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/", handlers.IndexGetHandler).Methods("GET")
|
||||
router.HandleFunc("/", handlers.IndexPostHandler).Methods("POST")
|
||||
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", handlers.PasteGetHandler).Methods("GET")
|
||||
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/nr", handlers.PasteGetHandlerNoRedirect).Methods("GET")
|
||||
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/meta", handlers.PasteGetHandlerMeta).Methods("GET")
|
||||
|
||||
// Start the server
|
||||
srv := &http.Server{
|
||||
Handler: router,
|
||||
Addr: "127.0.0.1:8000",
|
||||
WriteTimeout: 15 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
}
|
||||
log.Fatal(srv.ListenAndServe())
|
||||
}
|
||||
|
||||
// Parse the input arguments and return the initialized application config struct
|
||||
func parseArguments() (*ParsedArguments, error) {
|
||||
config := ParsedArguments{}
|
||||
|
||||
flag.StringVar(&config.databaseName, "database", "", "Location of the database file")
|
||||
flag.Parse()
|
||||
|
||||
if config.databaseName == "" {
|
||||
return nil, errors.New("database not set")
|
||||
if err := db.Open(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
return &config, nil
|
||||
defer db.Close()
|
||||
|
||||
go metrics.StartMetricsServer()
|
||||
handlers.StartMainServer()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user