diff --git a/api/article.go b/api/article.go index 065b040..2dca9c6 100644 --- a/api/article.go +++ b/api/article.go @@ -158,7 +158,7 @@ func articlePost(w http.ResponseWriter, r *http.Request) { } else { articleSummary.Tags = []string{} } - articleSummary.Link = path.Join("/", config.SubPath, "article", url.PathEscape(articleSummary.Link)) + articleSummary.Link = path.Join(config.SubPath, "article", url.PathEscape(articleSummary.Link)) w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(articleSummary) @@ -272,7 +272,7 @@ func articlePatch(w http.ResponseWriter, r *http.Request) { } else { articleSummary.Tags = []string{} } - articleSummary.Link = path.Join("/", config.SubPath, "article", url.PathEscape(articleSummary.Link)) + articleSummary.Link = path.Join(config.SubPath, "article", url.PathEscape(articleSummary.Link)) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(articleSummary) diff --git a/api/article_test.go b/api/article_test.go index b81050b..2de9e2b 100644 --- a/api/article_test.go +++ b/api/article_test.go @@ -134,7 +134,7 @@ func TestArticlePost(t *testing.T) { }, }, Tags: []string{}, - Link: "/article/testooo", + Link: "article/testooo", }, Code: http.StatusCreated, AfterExec: func(information *testInformation) { @@ -229,7 +229,7 @@ func TestArticlePatch(t *testing.T) { }, Created: created, Tags: []string{}, - Link: "/article/test-article", + Link: "article/test-article", }, AfterExec: func(information *testInformation) { var modified int64 diff --git a/api/assets.go b/api/assets.go index 1d98200..0ad8f3c 100644 --- a/api/assets.go +++ b/api/assets.go @@ -19,12 +19,6 @@ var assetsPayload struct { } func Assets(w http.ResponseWriter, r *http.Request) { - _, ok := authorizedSession(r) - if !ok { - w.WriteHeader(http.StatusUnauthorized) - return - } - switch r.Method { case http.MethodGet: assetsGet(w, r) @@ -63,7 +57,7 @@ func assetsGet(w http.ResponseWriter, r *http.Request) { request.Find(&assets) for _, asset := range assets { - asset.Link = path.Join("/", config.SubPath, "assets", asset.Link) + asset.Link = path.Join(config.SubPath, "assets", asset.Link) } w.WriteHeader(http.StatusOK) @@ -116,7 +110,7 @@ func assetsPost(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(schema.Asset{ Id: tmpDatabaseSchema.Id, Name: tmpDatabaseSchema.Name, - Link: path.Join("/", config.SubPath, "assets", tmpDatabaseSchema.Link), + Link: path.Join(config.SubPath, "assets", tmpDatabaseSchema.Link), }) } } diff --git a/api/assets_test.go b/api/assets_test.go index 6dcb9ea..814c106 100644 --- a/api/assets_test.go +++ b/api/assets_test.go @@ -20,12 +20,12 @@ func TestAssetsGet(t *testing.T) { { "name": "linux", "data": "this should be an image of tux :3", - "link": "/assets/linux", + "link": "assets/linux", }, { "name": "get test", "data": "", - "link": "/assets/get-test", + "link": "assets/get-test", }, }) @@ -47,7 +47,7 @@ func TestAssetsGet(t *testing.T) { { Id: 1, Name: "linux", - Link: "/assets/linux", + Link: "assets/linux", }, }, Code: http.StatusOK, @@ -64,7 +64,7 @@ func TestAssetsGet(t *testing.T) { { Id: 1, Name: "linux", - Link: "/assets/linux", + Link: "assets/linux", }, }, Code: http.StatusOK, @@ -78,12 +78,12 @@ func TestAssetsGet(t *testing.T) { { Id: 1, Name: "linux", - Link: "/assets/linux", + Link: "assets/linux", }, { Id: 2, Name: "get test", - Link: "/assets/get-test", + Link: "assets/get-test", }, }, Code: http.StatusOK, @@ -124,7 +124,7 @@ func TestAssetsPost(t *testing.T) { ResultBody: schema.Asset{ Id: 1, Name: "test", - Link: "/assets/test", + Link: "assets/test", }, Code: http.StatusCreated, }, @@ -150,7 +150,7 @@ func TestAssetsDelete(t *testing.T) { database.GetDB().Table("assets").Create(map[string]interface{}{ "name": "example", "data": "just a normal string", - "link": "/assets/example", + "link": "assets/example", }) server := httptest.NewServer(http.HandlerFunc(assetsDelete)) diff --git a/api/recent.go b/api/recent.go index 4ab71c2..968e234 100644 --- a/api/recent.go +++ b/api/recent.go @@ -35,7 +35,7 @@ func Recent(w http.ResponseWriter, r *http.Request) { database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors) summary.Tags = []string{} database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags) - summary.Link = path.Join("/", config.SubPath, "article", summary.Link) + summary.Link = path.Join(config.SubPath, "article", summary.Link) articleSummaries[i] = summary } diff --git a/api/recent_test.go b/api/recent_test.go index 26f22f8..d1aae0e 100644 --- a/api/recent_test.go +++ b/api/recent_test.go @@ -31,7 +31,7 @@ func TestRecent(t *testing.T) { Tags: []string{}, Created: time.Unix(0, 0).Unix(), Modified: time.Now().Unix(), - Link: "/article/test", + Link: "article/test", }, { Id: 2, @@ -40,7 +40,7 @@ func TestRecent(t *testing.T) { Authors: authors, Tags: []string{}, Created: time.Now().Unix(), - Link: "/article/recent", + Link: "article/recent", }, } diff --git a/api/search.go b/api/search.go index 5f96e7c..0026f18 100644 --- a/api/search.go +++ b/api/search.go @@ -72,7 +72,7 @@ func Search(w http.ResponseWriter, r *http.Request) { database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors) summary.Tags = []string{} database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags) - summary.Link = path.Join("/", config.SubPath, "article", summary.Link) + summary.Link = path.Join(config.SubPath, "article", summary.Link) articleSummaries[i] = summary } diff --git a/api/search_test.go b/api/search_test.go index fee2559..414c99d 100644 --- a/api/search_test.go +++ b/api/search_test.go @@ -95,7 +95,7 @@ func TestSearch(t *testing.T) { Tags: []string{ "example", }, - Link: "/article/first-article", + Link: "article/first-article", }, }, Code: http.StatusOK, @@ -121,7 +121,7 @@ func TestSearch(t *testing.T) { Created: now.Unix(), Modified: now.Add(24 * time.Hour).Unix(), Tags: []string{}, - Link: "/article/test", + Link: "article/test", }, { Id: 3, @@ -135,7 +135,7 @@ func TestSearch(t *testing.T) { Created: now.Unix(), Modified: now.Add(12 * time.Hour).Unix(), Tags: []string{}, - Link: "/article/owo", + Link: "article/owo", }, }, Code: http.StatusOK, @@ -158,7 +158,7 @@ func TestSearch(t *testing.T) { Created: now.Unix(), Modified: now.Add(12 * time.Hour).Unix(), Tags: []string{}, - Link: "/article/owo", + Link: "article/owo", }, }, Code: http.StatusOK, @@ -182,7 +182,7 @@ func TestSearch(t *testing.T) { Tags: []string{ "example", }, - Link: "/article/first-article", + Link: "article/first-article", }, }, Code: http.StatusOK, @@ -206,7 +206,7 @@ func TestSearch(t *testing.T) { Tags: []string{ "example", }, - Link: "/article/first-article", + Link: "article/first-article", }, { Id: 2, @@ -222,7 +222,7 @@ func TestSearch(t *testing.T) { Created: now.Unix(), Modified: now.Add(24 * time.Hour).Unix(), Tags: []string{}, - Link: "/article/test", + Link: "article/test", }, }, Code: http.StatusOK, diff --git a/api/tags_test.go b/api/tags_test.go index 59de65e..0e0385d 100644 --- a/api/tags_test.go +++ b/api/tags_test.go @@ -23,7 +23,7 @@ func TestTags(t *testing.T) { "title": "Upload test", "summary": "An example article to test the upload api endpoint", "created": time.Now().Unix(), - "link": "/article/upload-test", + "link": "article/upload-test", "markdown": "Oh god i have to test all this, what am i doing with my life", "html": "
Oh god i have to test all this, what am i doing with my life
", }, diff --git a/database.sqlite3 b/database.sqlite3 index e1312f0..0231d24 100644 Binary files a/database.sqlite3 and b/database.sqlite3 differ diff --git a/main.go b/main.go index c73c256..9b94c0f 100644 --- a/main.go +++ b/main.go @@ -62,18 +62,19 @@ func setupApi(r *mux.Router) { func setupFrontend(r *mux.Router) { r.HandleFunc("/article/{article}", server.Article).Methods(http.MethodGet) + r.HandleFunc("/assets/{asset}", server.Assets).Methods(http.MethodGet) - r.PathPrefix("/css/").HandlerFunc(server.ServePath) - r.PathPrefix("/img/").HandlerFunc(server.ServePath) - r.PathPrefix("/js/").HandlerFunc(server.ServePath) - r.PathPrefix("/html/").HandlerFunc(server.ServePath) + r.PathPrefix("/css/").HandlerFunc(server.ServePath).Methods(http.MethodGet) + r.PathPrefix("/img/").HandlerFunc(server.ServePath).Methods(http.MethodGet) + r.PathPrefix("/js/").HandlerFunc(server.ServePath).Methods(http.MethodGet) + r.PathPrefix("/html/").HandlerFunc(server.ServePath).Methods(http.MethodGet) landingpage := template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "landingpage.gohtml"))) r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { landingpage.Execute(w, struct { BasePath string }{BasePath: config.Address + strings.TrimSuffix(path.Join("/", config.SubPath), "/") + "/"}) - }) + }).Methods(http.MethodGet) r.NotFoundHandler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { server.Error404(w, r) diff --git a/server/article.go b/server/article.go index 6f8063c..8606999 100644 --- a/server/article.go +++ b/server/article.go @@ -36,7 +36,7 @@ func Article(w http.ResponseWriter, r *http.Request) { Error500(w, r) } else { var authors, tags []string - database.GetDB().Table("author").Select("id").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", article.Id)).Find(&authors) + database.GetDB().Table("author").Select("name").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", article.Id)).Find(&authors) database.GetDB().Table("article_tag").Where("article_id = ?", article.Id).Find(&tags) ta := tmplArticle{ diff --git a/server/assets.go b/server/assets.go new file mode 100644 index 0000000..aa00962 --- /dev/null +++ b/server/assets.go @@ -0,0 +1,23 @@ +package server + +import ( + "TheAdversary/database" + "github.com/gorilla/mux" + "mime" + "net/http" + "path" +) + +func Assets(w http.ResponseWriter, r *http.Request) { + assetName := mux.Vars(r)["asset"] + var buf []interface{} + database.GetDB().Table("assets").Select("data").Find(&buf, "link = ?", assetName) + + if buf == nil { + Error404(w, r) + } else { + data := buf[0].([]byte) + w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(assetName))) + w.Write(data) + } +}