mchess-server/main.go

96 lines
2.4 KiB
Go
Raw Normal View History

2022-10-22 18:01:55 +00:00
package main
import (
2023-04-18 20:12:05 +00:00
"local/m/mchess_server/server"
2022-11-19 10:41:10 +00:00
"log"
"net/http"
2022-12-18 14:43:27 +00:00
"os"
2023-04-18 20:12:05 +00:00
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
2023-04-18 20:19:28 +00:00
"github.com/google/uuid"
2022-10-22 18:01:55 +00:00
)
var cert_path = "/etc/letsencrypt/live/chess.sw-gross.de/"
var cert_file = cert_path + "fullchain.pem"
var key_file = cert_path + "privkey.pem"
2023-04-18 20:12:05 +00:00
// album represents data about a record album.
type album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
}
// albums slice to seed record album data.
var albums = []album{
{ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99},
{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99},
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99},
}
func main() {
2022-12-18 14:43:27 +00:00
hostname, err := os.Hostname()
if err != nil {
2023-04-18 20:12:05 +00:00
log.Fatal(err)
2022-12-18 14:43:27 +00:00
}
2023-04-18 20:12:05 +00:00
router := gin.Default()
2023-04-18 20:47:51 +00:00
router.GET("/api/albums", getAlbums)
router.GET("/api/albums/:id", getAlbumByID)
router.POST("/api/albums", postAlbums)
router.GET("/api/random", playRandomGame)
http.HandleFunc("/api/ws", server.PlayHandler)
2022-11-19 10:41:10 +00:00
2023-04-18 20:12:05 +00:00
if hostname == "mbook" {
log.Println("Starting service WITHOUT TLS")
log.Fatal(router.Run("localhost:8080"))
} else {
2023-04-18 20:12:05 +00:00
log.Fatal(autotls.Run(router, "chess.sw-gross.de"))
2022-11-19 10:41:10 +00:00
}
2023-04-18 20:12:05 +00:00
}
2022-11-19 10:41:10 +00:00
2023-04-18 20:12:05 +00:00
func playRandomGame(c *gin.Context) {
2023-04-18 20:19:28 +00:00
player_uuid := uuid.New()
server.GetLobby().RegisterPlayer(server.NewPlayer(player_uuid))
c.IndentedJSON(http.StatusOK, player_uuid)
2022-10-22 18:01:55 +00:00
}
2023-04-18 20:12:05 +00:00
// getAlbums responds with the list of all albums as JSON.
func getAlbums(c *gin.Context) {
c.IndentedJSON(http.StatusOK, albums)
}
// postAlbums adds an album from JSON received in the request body.
func postAlbums(c *gin.Context) {
var newAlbum album
// Call BindJSON to bind the received JSON to
// newAlbum.
if err := c.BindJSON(&newAlbum); err != nil {
return
}
// Add the new album to the slice.
albums = append(albums, newAlbum)
c.IndentedJSON(http.StatusCreated, newAlbum)
}
// getAlbumByID locates the album whose ID value matches the id
// parameter sent by the client, then returns that album as a response.
func getAlbumByID(c *gin.Context) {
id := c.Param("id")
// Loop over the list of albums, looking for
// an album whose ID value matches the parameter.
for _, a := range albums {
if a.ID == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "album not found"})
}