2024-03-18 10:34:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-09-03 07:07:45 +00:00
|
|
|
"errors"
|
2024-04-16 21:58:39 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-07-31 18:17:58 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-11-09 23:36:18 +00:00
|
|
|
"strconv"
|
2024-08-02 21:48:26 +00:00
|
|
|
"time"
|
2024-04-16 21:58:39 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
"arimelody-web/admin"
|
|
|
|
"arimelody-web/api"
|
|
|
|
"arimelody-web/global"
|
|
|
|
"arimelody-web/templates"
|
2024-11-09 23:36:18 +00:00
|
|
|
"arimelody-web/view"
|
2024-08-02 21:48:26 +00:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2024-08-02 23:27:30 +00:00
|
|
|
_ "github.com/lib/pq"
|
2024-03-18 10:34:43 +00:00
|
|
|
)
|
|
|
|
|
2024-11-09 23:36:18 +00:00
|
|
|
const DEFAULT_PORT int64 = 8080
|
2024-03-18 10:34:43 +00:00
|
|
|
|
|
|
|
func main() {
|
2024-08-02 21:48:26 +00:00
|
|
|
// initialise database connection
|
2024-11-10 05:34:04 +00:00
|
|
|
if env := os.Getenv("ARIMELODY_DB_HOST"); env != "" { global.Config.DB.Host = env }
|
|
|
|
if env := os.Getenv("ARIMELODY_DB_NAME"); env != "" { global.Config.DB.Name = env }
|
|
|
|
if env := os.Getenv("ARIMELODY_DB_USER"); env != "" { global.Config.DB.User = env }
|
|
|
|
if env := os.Getenv("ARIMELODY_DB_PASS"); env != "" { global.Config.DB.Pass = env }
|
|
|
|
if global.Config.DB.Host == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "FATAL: db.host not provided! Exiting...\n")
|
2024-11-09 23:36:18 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-11-10 05:34:04 +00:00
|
|
|
if global.Config.DB.Name == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "FATAL: db.name not provided! Exiting...\n")
|
2024-11-09 23:36:18 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-11-10 05:34:04 +00:00
|
|
|
if global.Config.DB.User == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "FATAL: db.user not provided! Exiting...\n")
|
2024-11-09 23:36:18 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-11-10 05:34:04 +00:00
|
|
|
if global.Config.DB.Pass == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "FATAL: db.pass not provided! Exiting...\n")
|
2024-11-09 23:36:18 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-09-03 07:07:45 +00:00
|
|
|
|
2024-07-31 03:09:22 +00:00
|
|
|
var err error
|
2024-11-10 05:34:04 +00:00
|
|
|
global.DB, err = sqlx.Connect(
|
|
|
|
"postgres",
|
|
|
|
fmt.Sprintf(
|
|
|
|
"host=%s user=%s dbname=%s password='%s' sslmode=disable",
|
|
|
|
global.Config.DB.Host,
|
|
|
|
global.Config.DB.User,
|
|
|
|
global.Config.DB.Name,
|
|
|
|
global.Config.DB.Pass,
|
|
|
|
),
|
|
|
|
)
|
2024-08-02 21:48:26 +00:00
|
|
|
if err != nil {
|
2024-09-01 03:43:32 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "FATAL: Unable to create database connection pool: %v\n", err)
|
2024-08-02 21:48:26 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
global.DB.SetConnMaxLifetime(time.Minute * 3)
|
|
|
|
global.DB.SetMaxOpenConns(10)
|
|
|
|
global.DB.SetMaxIdleConns(10)
|
|
|
|
defer global.DB.Close()
|
2024-08-03 22:24:15 +00:00
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
// start the web server!
|
2024-08-01 00:39:18 +00:00
|
|
|
mux := createServeMux()
|
2024-11-09 23:36:18 +00:00
|
|
|
port, err := strconv.ParseInt(os.Getenv("ARIMELODY_PORT"), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
port = DEFAULT_PORT
|
|
|
|
}
|
2024-09-01 03:43:32 +00:00
|
|
|
fmt.Printf("Now serving at http://127.0.0.1:%d\n", port)
|
2024-08-02 21:48:26 +00:00
|
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), global.HTTPLog(mux)))
|
2024-08-01 00:39:18 +00:00
|
|
|
}
|
2024-07-31 03:09:22 +00:00
|
|
|
|
2024-08-01 00:39:18 +00:00
|
|
|
func createServeMux() *http.ServeMux {
|
2024-07-31 03:09:22 +00:00
|
|
|
mux := http.NewServeMux()
|
2024-07-31 18:17:58 +00:00
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
mux.Handle("/admin/", http.StripPrefix("/admin", admin.Handler()))
|
|
|
|
mux.Handle("/api/", http.StripPrefix("/api", api.Handler()))
|
2024-11-01 19:33:26 +00:00
|
|
|
mux.Handle("/music/", http.StripPrefix("/music", view.MusicHandler()))
|
2024-11-10 05:34:04 +00:00
|
|
|
mux.Handle("/uploads/", http.StripPrefix("/uploads", staticHandler(filepath.Join(global.Config.DataDirectory, "uploads"))))
|
2024-08-02 21:48:26 +00:00
|
|
|
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-07-31 18:17:58 +00:00
|
|
|
if r.URL.Path == "/" || r.URL.Path == "/index.html" {
|
2024-09-01 03:43:32 +00:00
|
|
|
err := templates.Pages["index"].Execute(w, nil)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2024-07-31 18:17:58 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-01 02:54:15 +00:00
|
|
|
staticHandler("public").ServeHTTP(w, r)
|
2024-08-02 21:48:26 +00:00
|
|
|
}))
|
2024-08-01 00:39:18 +00:00
|
|
|
|
|
|
|
return mux
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
2024-07-31 12:45:34 +00:00
|
|
|
|
2024-08-01 02:54:15 +00:00
|
|
|
func staticHandler(directory string) http.Handler {
|
2024-07-31 18:17:58 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-08-01 02:54:15 +00:00
|
|
|
info, err := os.Stat(filepath.Join(directory, filepath.Clean(r.URL.Path)))
|
2024-09-03 07:07:45 +00:00
|
|
|
|
2024-07-31 18:17:58 +00:00
|
|
|
// does the file exist?
|
|
|
|
if err != nil {
|
2024-09-03 07:07:45 +00:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2024-07-31 18:17:58 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// is thjs a directory? (forbidden)
|
|
|
|
if info.IsDir() {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
http.FileServer(http.Dir(directory)).ServeHTTP(w, r)
|
2024-07-31 18:17:58 +00:00
|
|
|
})
|
|
|
|
}
|