home to your local SPACEGIRL ๐Ÿ’ซ arimelody.space
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

we are so back ๐ŸŽ‰

Signed-off-by: ari melody <ari@arimelody.me>

+109 -55
+20 -8
global/global.go
··· 7 7 "time" 8 8 ) 9 9 10 + var MimeTypes = map[string]string{ 11 + "css": "text/css; charset=utf-8", 12 + "png": "image/png", 13 + "jpg": "image/jpg", 14 + "webp": "image/webp", 15 + "html": "text/html", 16 + "asc": "text/plain", 17 + "pub": "text/plain", 18 + "txt": "text/plain", 19 + "js": "application/javascript", 20 + } 21 + 10 22 var LAST_MODIFIED = time.Now() 11 23 12 24 func IsModified(req *http.Request, last_modified time.Time) bool { ··· 23 35 return false 24 36 } 25 37 26 - type loggingResponseWriter struct { 38 + type LoggingResponseWriter struct { 27 39 http.ResponseWriter 28 - code int 40 + Code int 29 41 } 30 42 31 - func (lw *loggingResponseWriter) WriteHeader(code int) { 32 - lw.code = code 33 - lw.ResponseWriter.WriteHeader(code) 43 + func (lrw *LoggingResponseWriter) WriteHeader(code int) { 44 + lrw.Code = code 45 + lrw.ResponseWriter.WriteHeader(code) 34 46 } 35 47 36 48 func HTTPLog(next http.Handler) http.Handler { 37 49 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 38 50 start := time.Now() 39 51 40 - lw := loggingResponseWriter{w, http.StatusOK} 52 + lrw := LoggingResponseWriter{w, http.StatusOK} 41 53 42 - next.ServeHTTP(&lw, r) 54 + next.ServeHTTP(&lrw, r) 43 55 44 56 after := time.Now() 45 57 difference := (after.Nanosecond() - start.Nanosecond()) / 1_000_000 ··· 52 64 after.Format(time.UnixDate), 53 65 r.Method, 54 66 r.URL.Path, 55 - lw.code, 67 + lrw.Code, 56 68 elapsed, 57 69 r.Header["User-Agent"][0]) 58 70 })
+89 -47
main.go
··· 5 5 "html/template" 6 6 "log" 7 7 "net/http" 8 - "path/filepath" 8 + "os" 9 + "path/filepath" 9 10 10 11 "arimelody.me/arimelody.me/api/v1/admin" 11 12 "arimelody.me/arimelody.me/api/v1/music" ··· 14 15 15 16 const DEFAULT_PORT int = 8080 16 17 17 - // func handle_music(res http.ResponseWriter, req *http.Request, root *template.Template) int { 18 - // if !global.IsModified(req, global.LAST_MODIFIED) { 19 - // res.WriteHeader(304) 20 - // return 304 21 - // } 22 - // 23 - // music_template := template.Must(root.ParseFiles("views/music.html")) 24 - // err := music_template.Execute(res, music.Releases) 25 - // if err != nil { 26 - // http.Error(res, err.Error(), http.StatusInternalServerError) 27 - // return 500 28 - // } 29 - // return 200 30 - // } 31 - 32 - // func handle_music_gateway(res http.ResponseWriter, req *http.Request, root *template.Template) int { 33 - // if !global.IsModified(req, global.LAST_MODIFIED) { 34 - // res.WriteHeader(304) 35 - // return 304 36 - // } 37 - // 38 - // id := req.URL.Path[len("/music/"):] 39 - // release, err := music.GetRelease(id) 40 - // if err != nil { 41 - // return handle_not_found(res, req, root) 42 - // } 43 - // gateway_template := template.Must(root.ParseFiles("views/music-gateway.html")) 44 - // err = gateway_template.Execute(res, release) 45 - // if err != nil { 46 - // http.Error(res, err.Error(), http.StatusInternalServerError) 47 - // return 500 48 - // } 49 - // return 200 50 - // } 51 - 52 18 func main() { 53 19 db := InitDatabase() 54 20 defer db.Close() ··· 66 32 } 67 33 68 34 mux := http.NewServeMux() 35 + 36 + mux.Handle("/api/v1/admin/", global.HTTPLog(http.StripPrefix("/api/v1/admin", admin.Handler()))) 69 37 70 - mux.Handle("/api/v1/admin/", global.HTTPLog(http.StripPrefix("/api/v1/admin", admin.Handler()))) 71 - mux.Handle("/", http.FileServer(http.Dir("./public"))) 72 - // mux.Handle("/", global.HTTPLog(http.HandlerFunc(serveTemplate))) 38 + mux.Handle("/music/", global.HTTPLog(http.StripPrefix("/music", musicGatewayHandler()))) 39 + mux.Handle("/music", global.HTTPLog(serveTemplate("music.html", music.Releases))) 40 + 41 + mux.Handle("/", global.HTTPLog(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 42 + if r.URL.Path == "/" || r.URL.Path == "/index.html" { 43 + serveTemplate("index.html", nil).ServeHTTP(w, r) 44 + return 45 + } 46 + staticHandler().ServeHTTP(w, r) 47 + }))) 73 48 74 49 port := DEFAULT_PORT 75 50 fmt.Printf("now serving at http://127.0.0.1:%d\n", port) 76 51 log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), mux)) 77 52 } 78 53 79 - func serveTemplate(w http.ResponseWriter, r *http.Request) { 80 - lp_base := filepath.Join("views", "layout.html") 81 - lp_header := filepath.Join("views", "header.html") 82 - lp_footer := filepath.Join("views", "footer.html") 83 - lp_prideflag := filepath.Join("views", "prideflag.html") 84 - fp := filepath.Join("views", filepath.Clean(r.URL.Path)) 54 + func staticHandler() http.Handler { 55 + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 56 + info, err := os.Stat(filepath.Join("public", filepath.Clean(r.URL.Path))) 57 + // does the file exist? 58 + if err != nil { 59 + if os.IsNotExist(err) { 60 + http.NotFound(w, r) 61 + return 62 + } 63 + } 85 64 86 - template, _ := template.ParseFiles(lp_base, lp_header, lp_footer, lp_prideflag, fp) 87 - template.ExecuteTemplate(w, "layout", nil) 65 + // is thjs a directory? (forbidden) 66 + if info.IsDir() { 67 + http.NotFound(w, r) 68 + return 69 + } 70 + 71 + http.FileServer(http.Dir("./public")).ServeHTTP(w, r) 72 + }) 73 + } 74 + 75 + func musicGatewayHandler() http.Handler { 76 + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 77 + id := r.URL.Path[1:] 78 + release, err := music.GetRelease(id) 79 + if err != nil { 80 + http.NotFound(w, r) 81 + return 82 + } 83 + 84 + lrw := global.LoggingResponseWriter{w, http.StatusOK} 85 + 86 + serveTemplate("music-gateway.html", release).ServeHTTP(&lrw, r) 87 + 88 + if lrw.Code != http.StatusOK { 89 + fmt.Printf("Error loading music gateway for %s\n", id) 90 + return 91 + } 92 + }) 93 + } 94 + 95 + func serveTemplate(page string, data any) http.Handler { 96 + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 97 + lp_layout := filepath.Join("views", "layout.html") 98 + lp_header := filepath.Join("views", "header.html") 99 + lp_footer := filepath.Join("views", "footer.html") 100 + lp_prideflag := filepath.Join("views", "prideflag.html") 101 + fp := filepath.Join("views", filepath.Clean(page)) 102 + 103 + info, err := os.Stat(fp) 104 + if err != nil { 105 + if os.IsNotExist(err) { 106 + http.NotFound(w, r) 107 + return 108 + } 109 + } 110 + 111 + if info.IsDir() { 112 + http.NotFound(w, r) 113 + return 114 + } 115 + 116 + template, err := template.ParseFiles(lp_layout, lp_header, lp_footer, lp_prideflag, fp) 117 + if err != nil { 118 + fmt.Printf("Error parsing template files: %s\n", err) 119 + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) 120 + return 121 + } 122 + 123 + err = template.ExecuteTemplate(w, "layout.html", data) 124 + if err != nil { 125 + fmt.Printf("Error executing template: %s\n", err) 126 + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) 127 + return 128 + } 129 + }) 88 130 }