Fixing more stuff

This commit is contained in:
2022-02-06 18:27:30 +01:00
parent 8bc01e0984
commit e582a75d9e
13 changed files with 56 additions and 38 deletions

View File

@@ -158,7 +158,7 @@ func articlePost(w http.ResponseWriter, r *http.Request) {
} else { } else {
articleSummary.Tags = []string{} 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) w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(articleSummary) json.NewEncoder(w).Encode(articleSummary)
@@ -272,7 +272,7 @@ func articlePatch(w http.ResponseWriter, r *http.Request) {
} else { } else {
articleSummary.Tags = []string{} 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) w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(articleSummary) json.NewEncoder(w).Encode(articleSummary)

View File

@@ -134,7 +134,7 @@ func TestArticlePost(t *testing.T) {
}, },
}, },
Tags: []string{}, Tags: []string{},
Link: "/article/testooo", Link: "article/testooo",
}, },
Code: http.StatusCreated, Code: http.StatusCreated,
AfterExec: func(information *testInformation) { AfterExec: func(information *testInformation) {
@@ -229,7 +229,7 @@ func TestArticlePatch(t *testing.T) {
}, },
Created: created, Created: created,
Tags: []string{}, Tags: []string{},
Link: "/article/test-article", Link: "article/test-article",
}, },
AfterExec: func(information *testInformation) { AfterExec: func(information *testInformation) {
var modified int64 var modified int64

View File

@@ -19,12 +19,6 @@ var assetsPayload struct {
} }
func Assets(w http.ResponseWriter, r *http.Request) { func Assets(w http.ResponseWriter, r *http.Request) {
_, ok := authorizedSession(r)
if !ok {
w.WriteHeader(http.StatusUnauthorized)
return
}
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
assetsGet(w, r) assetsGet(w, r)
@@ -63,7 +57,7 @@ func assetsGet(w http.ResponseWriter, r *http.Request) {
request.Find(&assets) request.Find(&assets)
for _, asset := range 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) w.WriteHeader(http.StatusOK)
@@ -116,7 +110,7 @@ func assetsPost(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(schema.Asset{ json.NewEncoder(w).Encode(schema.Asset{
Id: tmpDatabaseSchema.Id, Id: tmpDatabaseSchema.Id,
Name: tmpDatabaseSchema.Name, Name: tmpDatabaseSchema.Name,
Link: path.Join("/", config.SubPath, "assets", tmpDatabaseSchema.Link), Link: path.Join(config.SubPath, "assets", tmpDatabaseSchema.Link),
}) })
} }
} }

View File

@@ -20,12 +20,12 @@ func TestAssetsGet(t *testing.T) {
{ {
"name": "linux", "name": "linux",
"data": "this should be an image of tux :3", "data": "this should be an image of tux :3",
"link": "/assets/linux", "link": "assets/linux",
}, },
{ {
"name": "get test", "name": "get test",
"data": "", "data": "",
"link": "/assets/get-test", "link": "assets/get-test",
}, },
}) })
@@ -47,7 +47,7 @@ func TestAssetsGet(t *testing.T) {
{ {
Id: 1, Id: 1,
Name: "linux", Name: "linux",
Link: "/assets/linux", Link: "assets/linux",
}, },
}, },
Code: http.StatusOK, Code: http.StatusOK,
@@ -64,7 +64,7 @@ func TestAssetsGet(t *testing.T) {
{ {
Id: 1, Id: 1,
Name: "linux", Name: "linux",
Link: "/assets/linux", Link: "assets/linux",
}, },
}, },
Code: http.StatusOK, Code: http.StatusOK,
@@ -78,12 +78,12 @@ func TestAssetsGet(t *testing.T) {
{ {
Id: 1, Id: 1,
Name: "linux", Name: "linux",
Link: "/assets/linux", Link: "assets/linux",
}, },
{ {
Id: 2, Id: 2,
Name: "get test", Name: "get test",
Link: "/assets/get-test", Link: "assets/get-test",
}, },
}, },
Code: http.StatusOK, Code: http.StatusOK,
@@ -124,7 +124,7 @@ func TestAssetsPost(t *testing.T) {
ResultBody: schema.Asset{ ResultBody: schema.Asset{
Id: 1, Id: 1,
Name: "test", Name: "test",
Link: "/assets/test", Link: "assets/test",
}, },
Code: http.StatusCreated, Code: http.StatusCreated,
}, },
@@ -150,7 +150,7 @@ func TestAssetsDelete(t *testing.T) {
database.GetDB().Table("assets").Create(map[string]interface{}{ database.GetDB().Table("assets").Create(map[string]interface{}{
"name": "example", "name": "example",
"data": "just a normal string", "data": "just a normal string",
"link": "/assets/example", "link": "assets/example",
}) })
server := httptest.NewServer(http.HandlerFunc(assetsDelete)) server := httptest.NewServer(http.HandlerFunc(assetsDelete))

View File

@@ -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) 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{} summary.Tags = []string{}
database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags) 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 articleSummaries[i] = summary
} }

View File

@@ -31,7 +31,7 @@ func TestRecent(t *testing.T) {
Tags: []string{}, Tags: []string{},
Created: time.Unix(0, 0).Unix(), Created: time.Unix(0, 0).Unix(),
Modified: time.Now().Unix(), Modified: time.Now().Unix(),
Link: "/article/test", Link: "article/test",
}, },
{ {
Id: 2, Id: 2,
@@ -40,7 +40,7 @@ func TestRecent(t *testing.T) {
Authors: authors, Authors: authors,
Tags: []string{}, Tags: []string{},
Created: time.Now().Unix(), Created: time.Now().Unix(),
Link: "/article/recent", Link: "article/recent",
}, },
} }

View File

@@ -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) 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{} summary.Tags = []string{}
database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags) 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 articleSummaries[i] = summary
} }

View File

@@ -95,7 +95,7 @@ func TestSearch(t *testing.T) {
Tags: []string{ Tags: []string{
"example", "example",
}, },
Link: "/article/first-article", Link: "article/first-article",
}, },
}, },
Code: http.StatusOK, Code: http.StatusOK,
@@ -121,7 +121,7 @@ func TestSearch(t *testing.T) {
Created: now.Unix(), Created: now.Unix(),
Modified: now.Add(24 * time.Hour).Unix(), Modified: now.Add(24 * time.Hour).Unix(),
Tags: []string{}, Tags: []string{},
Link: "/article/test", Link: "article/test",
}, },
{ {
Id: 3, Id: 3,
@@ -135,7 +135,7 @@ func TestSearch(t *testing.T) {
Created: now.Unix(), Created: now.Unix(),
Modified: now.Add(12 * time.Hour).Unix(), Modified: now.Add(12 * time.Hour).Unix(),
Tags: []string{}, Tags: []string{},
Link: "/article/owo", Link: "article/owo",
}, },
}, },
Code: http.StatusOK, Code: http.StatusOK,
@@ -158,7 +158,7 @@ func TestSearch(t *testing.T) {
Created: now.Unix(), Created: now.Unix(),
Modified: now.Add(12 * time.Hour).Unix(), Modified: now.Add(12 * time.Hour).Unix(),
Tags: []string{}, Tags: []string{},
Link: "/article/owo", Link: "article/owo",
}, },
}, },
Code: http.StatusOK, Code: http.StatusOK,
@@ -182,7 +182,7 @@ func TestSearch(t *testing.T) {
Tags: []string{ Tags: []string{
"example", "example",
}, },
Link: "/article/first-article", Link: "article/first-article",
}, },
}, },
Code: http.StatusOK, Code: http.StatusOK,
@@ -206,7 +206,7 @@ func TestSearch(t *testing.T) {
Tags: []string{ Tags: []string{
"example", "example",
}, },
Link: "/article/first-article", Link: "article/first-article",
}, },
{ {
Id: 2, Id: 2,
@@ -222,7 +222,7 @@ func TestSearch(t *testing.T) {
Created: now.Unix(), Created: now.Unix(),
Modified: now.Add(24 * time.Hour).Unix(), Modified: now.Add(24 * time.Hour).Unix(),
Tags: []string{}, Tags: []string{},
Link: "/article/test", Link: "article/test",
}, },
}, },
Code: http.StatusOK, Code: http.StatusOK,

View File

@@ -23,7 +23,7 @@ func TestTags(t *testing.T) {
"title": "Upload test", "title": "Upload test",
"summary": "An example article to test the upload api endpoint", "summary": "An example article to test the upload api endpoint",
"created": time.Now().Unix(), "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", "markdown": "Oh god i have to test all this, what am i doing with my life",
"html": "<p>Oh god i have to test all this, what am i doing with my life<p>", "html": "<p>Oh god i have to test all this, what am i doing with my life<p>",
}, },

Binary file not shown.

11
main.go
View File

@@ -62,18 +62,19 @@ func setupApi(r *mux.Router) {
func setupFrontend(r *mux.Router) { func setupFrontend(r *mux.Router) {
r.HandleFunc("/article/{article}", server.Article).Methods(http.MethodGet) 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("/css/").HandlerFunc(server.ServePath).Methods(http.MethodGet)
r.PathPrefix("/img/").HandlerFunc(server.ServePath) r.PathPrefix("/img/").HandlerFunc(server.ServePath).Methods(http.MethodGet)
r.PathPrefix("/js/").HandlerFunc(server.ServePath) r.PathPrefix("/js/").HandlerFunc(server.ServePath).Methods(http.MethodGet)
r.PathPrefix("/html/").HandlerFunc(server.ServePath) r.PathPrefix("/html/").HandlerFunc(server.ServePath).Methods(http.MethodGet)
landingpage := template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "landingpage.gohtml"))) landingpage := template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "landingpage.gohtml")))
r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
landingpage.Execute(w, struct { landingpage.Execute(w, struct {
BasePath string BasePath string
}{BasePath: config.Address + strings.TrimSuffix(path.Join("/", config.SubPath), "/") + "/"}) }{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) { r.NotFoundHandler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
server.Error404(w, r) server.Error404(w, r)

View File

@@ -36,7 +36,7 @@ func Article(w http.ResponseWriter, r *http.Request) {
Error500(w, r) Error500(w, r)
} else { } else {
var authors, tags []string 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) database.GetDB().Table("article_tag").Where("article_id = ?", article.Id).Find(&tags)
ta := tmplArticle{ ta := tmplArticle{

23
server/assets.go Normal file
View File

@@ -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)
}
}