feat: tests (article)

This commit is contained in:
Florian Sylvain
2023-08-12 02:33:40 +02:00
parent 0466da2d4b
commit 46daadb8e4
7 changed files with 324 additions and 48 deletions
+11
View File
@@ -0,0 +1,11 @@
package main
import "GohCMS2/main/server"
func main() {
router := server.InitServer()
err := server.StartServer(router)
if err != nil {
panic(err)
}
}
+83
View File
@@ -0,0 +1,83 @@
package route
import (
"GohCMS2/api"
"encoding/json"
"github.com/MadAppGang/httplog"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/go-chi/jwtauth/v5"
"net/http"
"os"
"strings"
)
const keyContentType = "Content-Type"
func JsonContentTypeMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(keyContentType, "application/json")
next.ServeHTTP(w, r)
})
}
func HtmlContentTypeMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(keyContentType, "text/html")
next.ServeHTTP(w, r)
})
}
func InitJwt() {
api.TokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
}
func GetHelloWorld(w http.ResponseWriter, _ *http.Request) {
msg, _ := json.Marshal(map[string]string{"message": "Hello World"})
_, _ = w.Write(msg)
}
func InitBackendRoutes() *chi.Mux {
r := chi.NewRouter()
r.Use(httplog.LoggerWithName("backend"))
r.Use(JsonContentTypeMiddleware)
r.Get("/", GetHelloWorld)
r.Group(func(r chi.Router) {
r.Use(jwtauth.Verifier(api.TokenAuth))
r.Use(jwtauth.Authenticator)
r.Mount("/article", api.NewArticleRouter())
})
r.Mount("/auth", api.NewAuthRouter())
return r
}
func InitFrontendRoutes() *chi.Mux {
r := chi.NewRouter()
r.Use(httplog.LoggerWithName("frontend"))
r.Use(HtmlContentTypeMiddleware)
r.Mount("/", api.NewPageRouter())
return r
}
func InitRoutes() *chi.Mux {
backend := InitBackendRoutes()
frontend := InitFrontendRoutes()
apiRouter := chi.NewRouter()
apiRouter.Use(cors.Handler(cors.Options{
AllowedOrigins: strings.Split(os.Getenv("CORS_ALLOWED_ORIGINS"), ";"),
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", keyContentType, "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
}))
apiRouter.Mount("/v1", backend)
apiRouter.Mount("/", frontend)
return apiRouter
}
+47
View File
@@ -0,0 +1,47 @@
package server
import (
"GohCMS2/api"
"GohCMS2/main/route"
"fmt"
"github.com/go-chi/chi/v5"
"github.com/joho/godotenv"
"net/http"
"os"
)
var possibleEnvFileLocations = []string{".env", "../.env"}
var envVarsToLoad = []string{"PORT", "ENVIRONMENT", "CORS_ALLOWED_ORIGINS"}
func initEnvVariables() {
var err error
for _, envLocation := range possibleEnvFileLocations {
err = godotenv.Load(envLocation)
if err == nil {
break
}
}
if err != nil {
panic("Could not load .env file")
}
for _, envVar := range envVarsToLoad {
if _, ok := os.LookupEnv(envVar); !ok {
panic(fmt.Sprintf("Environment variable %s is not set", envVar))
}
}
}
func InitServer() *chi.Mux {
initEnvVariables()
api.InitContainer()
api.InitValidator()
route.InitJwt()
return route.InitRoutes()
}
func StartServer(router *chi.Mux) error {
fmt.Println("Server starting on http://localhost:" + os.Getenv("PORT"))
err := http.ListenAndServe(":"+os.Getenv("PORT"), router)
return err
}