2024-03-18 10:34:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"arimelody.me/arimelody.me/api/v1/music"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"os"
|
|
|
|
"net/http"
|
|
|
|
"log"
|
|
|
|
"strings"
|
2024-03-18 15:04:04 +00:00
|
|
|
"time"
|
2024-03-18 20:19:04 +00:00
|
|
|
"strconv"
|
2024-03-18 10:34:43 +00:00
|
|
|
|
|
|
|
"github.com/gomarkdown/markdown"
|
|
|
|
"github.com/gomarkdown/markdown/html"
|
|
|
|
"github.com/gomarkdown/markdown/parser"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mime_types = map[string]string{
|
|
|
|
"css": "text/css; charset=utf-8",
|
|
|
|
"png": "image/png",
|
|
|
|
"jpg": "image/jpg",
|
|
|
|
"webp": "image/webp",
|
|
|
|
"html": "text/html",
|
|
|
|
"asc": "text/plain",
|
|
|
|
"pub": "text/plain",
|
|
|
|
"js": "application/javascript",
|
|
|
|
}
|
|
|
|
|
|
|
|
var templates = template.Must(template.ParseFiles(
|
|
|
|
"views/header.html",
|
|
|
|
"views/footer.html",
|
|
|
|
"views/index.html",
|
|
|
|
"views/music.html",
|
|
|
|
"views/music-gateway.html",
|
|
|
|
))
|
|
|
|
|
2024-03-18 20:19:04 +00:00
|
|
|
func log_request(req *http.Request, code int, start_time time.Time) {
|
|
|
|
now := time.Now()
|
|
|
|
difference := (now.Nanosecond() - start_time.Nanosecond()) / 1_000_000
|
|
|
|
elapsed := "<1"
|
|
|
|
if difference >= 1 {
|
|
|
|
elapsed = strconv.Itoa(difference)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("[%s] %s %s - %d (%sms) (%s)\n",
|
|
|
|
now.Format(time.UnixDate),
|
2024-03-18 15:04:04 +00:00
|
|
|
req.Method,
|
|
|
|
req.URL.Path,
|
2024-03-18 20:19:04 +00:00
|
|
|
code,
|
|
|
|
elapsed,
|
2024-03-18 15:04:04 +00:00
|
|
|
req.Header["User-Agent"][0],
|
|
|
|
)
|
2024-03-18 20:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func web_handler(writer http.ResponseWriter, req *http.Request) {
|
|
|
|
uri := req.URL.Path
|
|
|
|
start_time := time.Now()
|
2024-03-18 10:34:43 +00:00
|
|
|
|
|
|
|
if req.URL.Path == "/" {
|
2024-03-18 20:19:04 +00:00
|
|
|
code := index_handler(writer, req)
|
|
|
|
log_request(req, code, start_time)
|
2024-03-18 10:34:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if uri == "/music" {
|
2024-03-18 20:19:04 +00:00
|
|
|
code := music_handler(writer, req)
|
|
|
|
log_request(req, code, start_time)
|
2024-03-18 10:34:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(uri, "/music/") {
|
2024-03-18 20:19:04 +00:00
|
|
|
code := music_gateway_handler(writer, req)
|
|
|
|
log_request(req, code, start_time)
|
2024-03-18 10:34:43 +00:00
|
|
|
return
|
|
|
|
}
|
2024-03-18 20:19:04 +00:00
|
|
|
code := static_handler(writer, req)
|
|
|
|
log_request(req, code, start_time)
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 20:19:04 +00:00
|
|
|
func index_handler(writer http.ResponseWriter, req *http.Request) int {
|
2024-03-18 10:34:43 +00:00
|
|
|
err := templates.ExecuteTemplate(writer, "index.html", nil)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
2024-03-18 20:19:04 +00:00
|
|
|
return 500
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
2024-03-18 20:19:04 +00:00
|
|
|
return 200
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 20:19:04 +00:00
|
|
|
func music_handler(writer http.ResponseWriter, req *http.Request) int {
|
|
|
|
err := templates.ExecuteTemplate(writer, "music.html", music.QueryAllAlbums())
|
2024-03-18 10:34:43 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
2024-03-18 20:19:04 +00:00
|
|
|
return 500
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
2024-03-18 20:19:04 +00:00
|
|
|
return 200
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 20:19:04 +00:00
|
|
|
func music_gateway_handler(writer http.ResponseWriter, req *http.Request) int {
|
2024-03-18 10:34:43 +00:00
|
|
|
if len(req.URL.Path) <= len("/music/") {
|
|
|
|
http.Error(writer, "400 bad request", http.StatusBadRequest)
|
2024-03-18 20:19:04 +00:00
|
|
|
return 400
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id := req.URL.Path[len("/music/"):]
|
|
|
|
// http.Redirect(writer, req, "https://mellodoot.com/music/"+title, 302)
|
|
|
|
// return
|
|
|
|
album, ok := music.GetAlbum(id)
|
|
|
|
if !ok {
|
|
|
|
http.Error(writer, "404 not found", http.StatusNotFound)
|
2024-03-18 20:19:04 +00:00
|
|
|
return 404
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
|
|
|
err := templates.ExecuteTemplate(writer, "music-gateway.html", album)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
2024-03-18 20:19:04 +00:00
|
|
|
return 500
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
2024-03-18 20:19:04 +00:00
|
|
|
return 200
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 20:19:04 +00:00
|
|
|
func static_handler(writer http.ResponseWriter, req *http.Request) int {
|
2024-03-18 10:34:43 +00:00
|
|
|
filename := "public/" + req.URL.Path[1:]
|
2024-03-18 20:19:04 +00:00
|
|
|
|
|
|
|
// check the file's metadata
|
|
|
|
info, err := os.Stat(filename)
|
2024-03-18 10:34:43 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, "404 not found", http.StatusNotFound)
|
2024-03-18 20:19:04 +00:00
|
|
|
return 404
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Header["If-Modified-Since"]) > 0 && req.Header["If-Modified-Since"][0] != "" {
|
|
|
|
if_modified_since_time, err := time.Parse(http.TimeFormat, req.Header["If-Modified-Since"][0])
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, "400 bad request", http.StatusBadRequest)
|
|
|
|
return 400
|
|
|
|
}
|
|
|
|
if req.Header["If-Modified-Since"][0] == info.ModTime().Format(http.TimeFormat) || if_modified_since_time.After(info.ModTime()) {
|
|
|
|
writer.WriteHeader(304) // not modified
|
|
|
|
return 304
|
|
|
|
}
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
2024-03-18 20:19:04 +00:00
|
|
|
|
|
|
|
// set other nice headers
|
|
|
|
writer.Header().Set("Cache-Control", "max-age=86400")
|
|
|
|
writer.Header().Set("Last-Modified", info.ModTime().Format(http.TimeFormat))
|
|
|
|
// Last-Modified: <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
|
|
|
|
// Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
|
|
|
|
|
|
|
|
// read the file
|
|
|
|
body, err := os.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
|
|
|
return 500
|
|
|
|
}
|
|
|
|
|
|
|
|
// setting MIME types
|
2024-03-18 10:34:43 +00:00
|
|
|
filetype := filename[strings.LastIndex(filename, ".") + 1:]
|
|
|
|
if mime_type, ok := mime_types[filetype]; ok {
|
|
|
|
writer.Header().Set("Content-Type", mime_type)
|
|
|
|
} else {
|
|
|
|
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
}
|
2024-03-18 20:19:04 +00:00
|
|
|
|
2024-03-18 10:34:43 +00:00
|
|
|
writer.Write([]byte(body))
|
2024-03-18 20:19:04 +00:00
|
|
|
return 200
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parse_markdown(md []byte) []byte {
|
|
|
|
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
|
|
|
|
p := parser.NewWithExtensions(extensions)
|
|
|
|
doc := p.Parse(md)
|
|
|
|
|
|
|
|
htmlFlags := html.CommonFlags
|
|
|
|
opts := html.RendererOptions{ Flags: htmlFlags }
|
|
|
|
renderer := html.NewRenderer(opts)
|
|
|
|
|
|
|
|
return markdown.Render(doc, renderer)
|
|
|
|
}
|
|
|
|
|
2024-03-18 20:19:04 +00:00
|
|
|
func push_to_db_this_is_a_testing_thing_and_will_be_superfluous_later() {
|
|
|
|
db := InitDatabase()
|
|
|
|
|
|
|
|
for _, album := range music.QueryAllAlbums() {
|
|
|
|
PushAlbum(db, album)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, artist := range music.QueryAllArtists() {
|
|
|
|
PushArtist(db, artist)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer db.Close()
|
|
|
|
}
|
|
|
|
|
2024-03-18 10:34:43 +00:00
|
|
|
func main() {
|
2024-03-18 20:19:04 +00:00
|
|
|
// push_to_db_this_is_a_testing_thing_and_will_be_superfluous_later()
|
|
|
|
|
2024-03-18 10:34:43 +00:00
|
|
|
http.HandleFunc("/", web_handler)
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
|
}
|