HOLY REFACTOR GOOD GRIEF (also finally started some CRUD work)
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
1c310c9101
commit
442889340c
80 changed files with 1571 additions and 1330 deletions
57
main.go
57
main.go
|
@ -6,61 +6,70 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"arimelody.me/arimelody.me/admin"
|
||||
"arimelody.me/arimelody.me/music"
|
||||
"arimelody.me/arimelody.me/db"
|
||||
"arimelody.me/arimelody.me/api"
|
||||
"arimelody.me/arimelody.me/global"
|
||||
musicController "arimelody.me/arimelody.me/music/controller"
|
||||
musicView "arimelody.me/arimelody.me/music/view"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
const DEFAULT_PORT int = 8080
|
||||
|
||||
func main() {
|
||||
db := db.InitDatabase()
|
||||
defer db.Close()
|
||||
|
||||
// initialise database connection
|
||||
var err error
|
||||
music.Artists, err = music.PullAllArtists(db)
|
||||
global.DB, err = sqlx.Connect("postgres", "user=arimelody dbname=arimelody password=fuckingpassword sslmode=disable")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "unable to create database connection pool: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
global.DB.SetConnMaxLifetime(time.Minute * 3)
|
||||
global.DB.SetMaxOpenConns(10)
|
||||
global.DB.SetMaxIdleConns(10)
|
||||
defer global.DB.Close()
|
||||
|
||||
// pull artist data from DB
|
||||
global.Artists, err = musicController.PullAllArtists(global.DB)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to pull artists from database: %v\n", err);
|
||||
panic(1)
|
||||
}
|
||||
fmt.Printf("%d artists loaded successfully.\n", len(music.Artists))
|
||||
fmt.Printf("%d artists loaded successfully.\n", len(global.Artists))
|
||||
|
||||
music.Releases, err = music.PullAllReleases(db)
|
||||
// pull release data from DB
|
||||
global.Releases, err = musicController.PullAllReleases(global.DB)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to pull releases from database: %v\n", err);
|
||||
panic(1)
|
||||
}
|
||||
fmt.Printf("%d releases loaded successfully.\n", len(music.Releases))
|
||||
fmt.Printf("%d releases loaded successfully.\n", len(global.Releases))
|
||||
|
||||
// start the web server!
|
||||
mux := createServeMux()
|
||||
|
||||
port := DEFAULT_PORT
|
||||
fmt.Printf("now serving at http://127.0.0.1:%d\n", port)
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), mux))
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), global.HTTPLog(mux)))
|
||||
}
|
||||
|
||||
func createServeMux() *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/admin/", global.HTTPLog(http.StripPrefix("/admin", admin.Handler())))
|
||||
|
||||
mux.Handle("/api/v1/music/artist/", global.HTTPLog(http.StripPrefix("/api/v1/music/artist", music.ServeArtist())))
|
||||
mux.Handle("/api/v1/music/", global.HTTPLog(http.StripPrefix("/api/v1/music", music.ServeRelease())))
|
||||
mux.Handle("/api/v1/music", global.HTTPLog(music.PostRelease()))
|
||||
|
||||
mux.Handle("/music-artwork/", global.HTTPLog(http.StripPrefix("/music-artwork", music.ServeArtwork())))
|
||||
mux.Handle("/music/", global.HTTPLog(http.StripPrefix("/music", music.ServeGateway())))
|
||||
mux.Handle("/music", global.HTTPLog(music.ServeCatalog()))
|
||||
|
||||
mux.Handle("/", global.HTTPLog(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mux.Handle("/admin/", http.StripPrefix("/admin", admin.Handler()))
|
||||
mux.Handle("/api/", http.StripPrefix("/api", api.Handler()))
|
||||
mux.Handle("/music/", http.StripPrefix("/music", musicView.Handler()))
|
||||
mux.Handle("/uploads/", http.StripPrefix("/uploads", staticHandler("uploads")))
|
||||
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" || r.URL.Path == "/index.html" {
|
||||
global.ServeTemplate("index.html", nil).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
staticHandler("public").ServeHTTP(w, r)
|
||||
})))
|
||||
}))
|
||||
|
||||
return mux
|
||||
}
|
||||
|
@ -82,6 +91,6 @@ func staticHandler(directory string) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
http.FileServer(http.Dir("./public")).ServeHTTP(w, r)
|
||||
http.FileServer(http.Dir(directory)).ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue