tripwire/main.go

65 lines
1.4 KiB
Go

package main
import (
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
// func notFoundStub(w http.ResponseWriter, r *http.Request) {
// err := YggError{Code: 404, Error: "Not Found", ErrorMessage: "The server has not found anything matching the request URI"}
// sendError(w, err)
// }
func logger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if config.DebugMode {
log.Printf("Got hit at %s", r.URL.Path)
}
next.ServeHTTP(w, r)
})
}
func notfoundlogger(w http.ResponseWriter, r *http.Request) {
if config.DebugMode {
log.Printf("Got hit at %s (NOT FOUND)", r.URL.Path)
}
sendError(w, YggError{
Code: 404,
Error: "Not Found",
ErrorMessage: "The server has not found anything matching the request URI",
})
}
func handleRequests() {
r := mux.NewRouter().StrictSlash(true)
r.Use(logger)
r.NotFoundHandler = http.HandlerFunc(notfoundlogger)
err := loadConfig()
if err != nil {
log.Fatalln("Failed to load config.yaml:", err)
}
// todo: make this cleaner if possible
registerAuthEndpoints(r)
registerSessionEndpoints(r)
registerWebEndpoints(r)
log.Println("Tripwire started.")
log.Fatal(http.ListenAndServe(":10000", r))
}
func main() {
log.Println("Tripwire initializing...")
os.Mkdir("skins", 0755)
os.Mkdir("capes", 0755)
initDB()
handleRequests()
defer DB.Close()
}