implement endpoints to create and query all blog articles
Signed-off-by: Tobias Erbshäußer <tobias@tesoft.dev>
This commit is contained in:
+32
-8
@@ -12,20 +12,44 @@ func main() {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
staticFolder := os.Getenv("STATIC_FOLDER")
|
||||
if staticFolder == "" {
|
||||
staticFolder = "static"
|
||||
frontendPath := os.Getenv("FRONTEND_PATH")
|
||||
if frontendPath == "" {
|
||||
frontendPath = "static"
|
||||
}
|
||||
|
||||
fsHandler := http.FileServer(http.Dir(staticFolder))
|
||||
apiHandler := NewApiHandler()
|
||||
dbPath := os.Getenv("DB_PATH")
|
||||
if dbPath == "" {
|
||||
dbPath = "./database.sqlite"
|
||||
}
|
||||
|
||||
db, err := NewDatabase(dbPath)
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
apiHandler := &ApiHandler{db: db}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/", fsHandler)
|
||||
mux.Handle("/api/", apiHandler)
|
||||
mux.Handle("/", http.FileServer(http.Dir(frontendPath)))
|
||||
mux.Handle("GET /api/blog", apiHandler.ProcessAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
apiHandler.ServeBlogGet(writer, request)
|
||||
}), false))
|
||||
mux.Handle("PUT /api/blog", apiHandler.ProcessAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
apiHandler.ServeBlogPut(writer, request)
|
||||
}), true))
|
||||
mux.Handle("GET /api/blog/{id}", apiHandler.ProcessAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
apiHandler.ServeBlogGetSingle(writer, request)
|
||||
}), false))
|
||||
mux.Handle("GET /api/blog/{articleId}/file/{fileId}", apiHandler.ProcessAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
apiHandler.ServeBlogFileGetSingle(writer, request)
|
||||
}), false))
|
||||
mux.HandleFunc("/api/", func(writer http.ResponseWriter, request *http.Request) {
|
||||
http.NotFound(writer, request)
|
||||
})
|
||||
|
||||
log.Println("Listening on port", port)
|
||||
err := http.ListenAndServe(":"+port, mux)
|
||||
err = http.ListenAndServe(":"+port, mux)
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user