first commit! 🎉
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
commit
27d6e4f011
97 changed files with 2957 additions and 0 deletions
122
main.go
Normal file
122
main.go
Normal file
|
@ -0,0 +1,122 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"arimelody.me/arimelody.me/api/v1/music"
|
||||
|
||||
"fmt"
|
||||
"html/template"
|
||||
"os"
|
||||
"net/http"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"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",
|
||||
))
|
||||
|
||||
func web_handler(writer http.ResponseWriter, req *http.Request) {
|
||||
uri := req.URL.Path
|
||||
|
||||
fmt.Printf("%s %s (%s)\n", req.Method, req.URL.Path, req.Header["User-Agent"][0])
|
||||
|
||||
if req.URL.Path == "/" {
|
||||
index_handler(writer, req)
|
||||
return
|
||||
}
|
||||
if uri == "/music" {
|
||||
music_handler(writer, req)
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(uri, "/music/") {
|
||||
music_gateway_handler(writer, req)
|
||||
return
|
||||
}
|
||||
static_handler(writer, req)
|
||||
}
|
||||
|
||||
func index_handler(writer http.ResponseWriter, req *http.Request) {
|
||||
err := templates.ExecuteTemplate(writer, "index.html", nil)
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func music_handler(writer http.ResponseWriter, req *http.Request) {
|
||||
err := templates.ExecuteTemplate(writer, "music.html", music.QueryAll())
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func music_gateway_handler(writer http.ResponseWriter, req *http.Request) {
|
||||
if len(req.URL.Path) <= len("/music/") {
|
||||
http.Error(writer, "400 bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
err := templates.ExecuteTemplate(writer, "music-gateway.html", album)
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func static_handler(writer http.ResponseWriter, req *http.Request) {
|
||||
filename := "public/" + req.URL.Path[1:]
|
||||
body, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
http.Error(writer, "404 not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
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")
|
||||
}
|
||||
writer.Write([]byte(body))
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", web_handler)
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue