Skip to content

ktrahan2/spacey-bois-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spacey Bois

Spacey Bois is a text-based adventure game.

Table Of Contents

Description

Spacey Bois backend is made with Go as a very simple api. The api will allow highscores to POST into a PSQL database and also GET these entries.

Example Code

   func addScores(w http.ResponseWriter, r *http.Request) {

	setupResponse(&w, r)
	switch r.Method {
	case "POST":
		reqBody, _ := ioutil.ReadAll(r.Body)
		fmt.Fprintf(w, "%v", string(reqBody))
		var highscore HighScore
		json.Unmarshal(reqBody, &highscore)

		username := highscore.Username
		score := highscore.Score

		var err error
		sqlStatement := `
			INSERT INTO highscores (username, score)
			VALUES ($1, $2)`
		_, err = db.Exec(sqlStatement, username, score)
		if err != nil {
			panic(err)
		}
	default:
		//method not available error.
		http.Error(w, http.StatusText(405), 405)
	}
} 
  func connectToDatabase() {

	var err error
	host := os.Getenv("HOST")
	databaseUsername := os.Getenv("USERNAME")
	password := os.Getenv("PASSWORD")
	database := os.Getenv("DATABASE")
	port := os.Getenv("PORT")

	psqlInfo := fmt.Sprintf("host=%s port=%s user=%s "+
		"password=%s dbname=%s sslmode=disable",
		host, port, databaseUsername, password, database)
	//doesn't like this after I closed my VS code down. Had to use 'unset PGSYSCONFDIR'
	db, err = sql.Open("postgres", psqlInfo)
	if err != nil {
		panic(err)
	}

	err = db.Ping()
	if err != nil {
		panic(err)
	}

	fmt.Println("Successfully connected!")
}

Technology Used

  • Go
  • PSQL

Setting up for the application

To start the server run

  go run main.go

Main Features

  • GET fetch to retrieve all highscores from database
  • POST fetch to add highscores to the database

Features in Progress

  • Upgrade backend to create tables through go rather than manually making them in PSQL

Contact Information

Kyle Trahan

Link to Frontend Repo

https://github.com/ktrahan2/spacey-bois-frontend

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages