forked from TripwireTeam/tripwire
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"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)
|
|
|
|
// todo: make this cleaner if possible
|
|
registerAuthEndpoints(r)
|
|
registerSessionEndpoints(r)
|
|
registerWebEndpoints(r)
|
|
|
|
splitUrl := strings.Split(config.BaseUrl, ":")
|
|
port := "8080"
|
|
if len(splitUrl) > 1 {
|
|
port = splitUrl[1]
|
|
}
|
|
|
|
log.Printf("Tripwire started. Listening on port %v.\n", port)
|
|
log.Fatal(http.ListenAndServe(":"+port, r))
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) > 1 && os.Args[1] == "gen-keys" {
|
|
genKeys()
|
|
return
|
|
}
|
|
|
|
log.Println("Tripwire initializing...")
|
|
os.Mkdir("skins", 0755)
|
|
os.Mkdir("capes", 0755)
|
|
|
|
err := loadConfig()
|
|
if err != nil {
|
|
log.Fatalln("Failed to load config.yaml:", err)
|
|
}
|
|
|
|
initDB()
|
|
initKeys()
|
|
|
|
handleRequests()
|
|
|
|
defer DB.Close()
|
|
}
|