created some structure and a concept
This commit is contained in:
parent
d2bec96645
commit
9ded6a4615
13
internal/rest_handler/create_session.go
Normal file
13
internal/rest_handler/create_session.go
Normal file
@ -0,0 +1,13 @@
|
||||
package rest_handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gitea.elara.ws/Hazel/hangman/internal/session"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func CreateSession(c echo.Context) error {
|
||||
s := session.NewSession()
|
||||
return c.JSON(http.StatusOK, s)
|
||||
}
|
13
internal/view_handler/create_session.go
Normal file
13
internal/view_handler/create_session.go
Normal file
@ -0,0 +1,13 @@
|
||||
package view_handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func CreateSession(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "create_session", map[string]interface{}{
|
||||
"name": "create session",
|
||||
})
|
||||
}
|
13
internal/view_handler/create_user.go
Normal file
13
internal/view_handler/create_user.go
Normal file
@ -0,0 +1,13 @@
|
||||
package view_handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func CreateUser(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "create_user", map[string]interface{}{
|
||||
"name": c.Param("name"),
|
||||
})
|
||||
}
|
15
main.go
15
main.go
@ -4,9 +4,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"text/template"
|
||||
|
||||
"gitea.elara.ws/Hazel/hangman/internal/rest_handler"
|
||||
"gitea.elara.ws/Hazel/hangman/internal/view_handler"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
@ -24,12 +25,6 @@ func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c
|
||||
return tmpl.ExecuteTemplate(w, "base.html", data)
|
||||
}
|
||||
|
||||
func CreateSessionHandler(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "create_session", map[string]interface{}{
|
||||
"name": "create session",
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("wanna play hangman? Well ya cant since it isn't implemented yet..")
|
||||
|
||||
@ -37,11 +32,15 @@ func main() {
|
||||
|
||||
templates := make(map[string]*template.Template)
|
||||
templates["create_session"] = template.Must(template.ParseFiles("templates/create_session.html", "templates/base.html"))
|
||||
templates["create_user"] = template.Must(template.ParseFiles("templates/create_user.html", "templates/base.html"))
|
||||
e.Renderer = &TemplateRegistry{
|
||||
templates: templates,
|
||||
}
|
||||
|
||||
e.GET("/", CreateSessionHandler)
|
||||
e.GET("/", view_handler.CreateSession)
|
||||
e.GET("/api/session", rest_handler.CreateSession)
|
||||
|
||||
e.GET("/:name", view_handler.CreateUser)
|
||||
|
||||
e.Logger.Fatal(e.Start(":1323"))
|
||||
}
|
||||
|
@ -3,5 +3,23 @@
|
||||
{{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<button class="button is-primary">create session</button>
|
||||
<button class="button is-primary" id="createSessionBtn">create session</button>
|
||||
|
||||
<script>
|
||||
document.getElementById('createSessionBtn').addEventListener('click', async function() {
|
||||
const response = await fetch('/api/session', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
window.open(`/${data.Name}`);
|
||||
});
|
||||
</script>
|
||||
{{end}}
|
7
templates/create_user.html
Normal file
7
templates/create_user.html
Normal file
@ -0,0 +1,7 @@
|
||||
{{define "title"}}
|
||||
create user | {{index . "name"}}
|
||||
{{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<h1>Current Session: {{index . "name"}}</h1>
|
||||
{{end}}
|
Loading…
x
Reference in New Issue
Block a user