Changed routing framework from mux to Gin.

Swagger doc is now included in the application.
A fronted can now be hosted from the application.
This commit is contained in:
2022-01-29 17:52:33 +01:00
parent 512fcd0c4f
commit f9d6c24a97
43 changed files with 28760 additions and 210 deletions

View File

@@ -1,32 +1,29 @@
package api
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"music-server/pkg/helpers"
"music-server/pkg/server"
"net/http"
)
func IndexHandler(w http.ResponseWriter, r *http.Request) {
helpers.SetCorsAndNoCacheHeaders(&w, r)
type Index struct {
}
if r.URL.Path == "/version" {
w.Header().Add("Content-Type", "application/json")
func NewIndex() *Index {
return &Index{}
}
history := server.GetVersionHistory()
_ = json.NewEncoder(w).Encode(history)
} else if r.URL.Path == "/docs" {
http.ServeFile(w, r, "./docs/swagger.yaml")
} else if r.URL.Path == "/" {
_, err := fmt.Fprint(w, "Hello, World!!")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
} else {
http.NotFound(w, r)
func (i *Index) GetVersion(ctx *gin.Context) {
versionHistory := server.GetVersionHistory()
if versionHistory.Version == "" {
helpers.NewError(ctx, http.StatusNotFound, nil)
return
}
ctx.JSON(http.StatusOK, versionHistory)
}
func (i *Index) GetDBTest(ctx *gin.Context) {
server.TestDB()
ctx.JSON(http.StatusOK, "TestedDB")
}