Skip to content

Commit

Permalink
clean main, create web package
Browse files Browse the repository at this point in the history
  • Loading branch information
kubegu committed Mar 1, 2024
1 parent 0d25d6f commit 0c801ad
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 35 deletions.
42 changes: 7 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,18 @@
package main

import (
"embed"
"flag"
"fmt"
"log"
"net/http"
"strings"
"text/template"

"github.com/gunni1/leipzig-library-game-stock-api/domain"
libClient "github.com/gunni1/leipzig-library-game-stock-api/library-le"
"github.com/gunni1/leipzig-library-game-stock-api/web"
)

//go:embed templates
var htmlTemplates embed.FS

func indexHandler(respWriter http.ResponseWriter, request *http.Request) {
templ := template.Must(template.ParseFS(htmlTemplates, "templates/index.html"))
templ.Execute(respWriter, nil)
}

func gameIndexHandler(respWriter http.ResponseWriter, request *http.Request) {
log.Print("received htmx game-index")
branch := strings.ToLower(request.PostFormValue("branch"))
platform := strings.ToLower(request.PostFormValue("platform"))
branchCode, exists := libClient.GetBranchCode(branch)
if !exists {
log.Printf("Requested branch: %s does not exists.", branch)
return
}
client := libClient.Client{}
games := client.FindAvailabelGames(branchCode, platform)
data := map[string][]domain.Game{
"Games": games,
}
templ := template.Must(template.ParseFS(htmlTemplates, "templates/games.html"))
templ.Execute(respWriter, data)
}

func main() {
mux := http.NewServeMux()
port := flag.Int("port", 8080, "Webserver Port")
flag.Parse()

mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/game-index/", gameIndexHandler)
log.Fatal(http.ListenAndServe(":8080", mux))
mux := web.InitMux()
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), mux))
}
49 changes: 49 additions & 0 deletions web/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package web

import (
"embed"
"log"
"net/http"
"strings"
"text/template"

"github.com/gunni1/leipzig-library-game-stock-api/domain"
libClient "github.com/gunni1/leipzig-library-game-stock-api/library-le"
)

//go:embed templates
var htmlTemplates embed.FS

type Server struct {
Mux *http.ServeMux
}

func InitMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/game-index/", gameIndexHandler)
return mux
}

func indexHandler(respWriter http.ResponseWriter, request *http.Request) {
templ := template.Must(template.ParseFS(htmlTemplates, "templates/index.html"))
templ.Execute(respWriter, nil)
}

func gameIndexHandler(respWriter http.ResponseWriter, request *http.Request) {
log.Print("received htmx game-index")
branch := strings.ToLower(request.PostFormValue("branch"))
platform := strings.ToLower(request.PostFormValue("platform"))
branchCode, exists := libClient.GetBranchCode(branch)
if !exists {
log.Printf("Requested branch: %s does not exists.", branch)
return
}
client := libClient.Client{}
games := client.FindAvailabelGames(branchCode, platform)
data := map[string][]domain.Game{
"Games": games,
}
templ := template.Must(template.ParseFS(htmlTemplates, "templates/games.html"))
templ.Execute(respWriter, data)
}
File renamed without changes.
File renamed without changes.

0 comments on commit 0c801ad

Please sign in to comment.