This repository has been archived on 2024-03-23. You can view files and clone it, but cannot push or open issues or pull requests.
ambition-legacy/backend/main.go
2023-07-26 22:09:31 +01:00

50 lines
961 B
Go

package main
import (
"fmt"
"log"
"net/http"
"os"
)
type App struct {
UserHandler *UserHandler
}
// Define the serve function
func (h *App) ServeHTTP(res http.ResponseWriter, req *http.Request) {
var head string
head, req.URL.Path = ShiftPath(req.URL.Path)
switch head {
// Start the user handler should the requested user be found
case "user":
h.UserHandler.Handle(res, req)
// Return a `Not Found` if the user is not found
default:
http.Error(res, "Not Found", http.StatusNotFound)
}
}
// Run the server
func main() {
// Initialise the user handler
user_handler, err := NewUserHandler()
// Log any errors
if err != nil {
log.Fatalln(err)
}
a := &App{
UserHandler: user_handler,
}
port := os.Getenv("PORT")
if port == "" {
port = "7741"
}
// Log that the program has successfully started listening to the port
log.Println(fmt.Sprintf("Ambition backend listening to port %v", port))
http.ListenAndServe(":"+port, a)
}