@ -14,7 +14,7 @@ import (
var (
port * uint = flag . Uint ( "port" , 8080 , "Specifies a port on which the helping page will be served" )
dbname string = "database.db"
tableName = "randomdata"
tableName string = "randomdata"
)
func init ( ) {
@ -35,6 +35,7 @@ func main() {
}
log . Printf ( "Created %s db\n" , tableName )
// set up patterns and handlers
mux := http . NewServeMux ( )
mux . HandleFunc ( "/" , helpPage )
mux . HandleFunc ( "/randomdata" , db . HandleGlobalWeb )
@ -65,106 +66,3 @@ func helpPage(w http.ResponseWriter, r *http.Request) {
`
fmt . Fprint ( w , helpMessage )
}
// func (dbHandler *randomDataHandler) updateSpecificRandomData(w http.ResponseWriter, r *http.Request) {
// if r.Header.Get("content-type") != "application/json" {
// w.WriteHeader(http.StatusUnsupportedMediaType)
// w.Write([]byte(fmt.Sprintf("Got `%s` instead of `application/json`", r.Header.Get("content-type"))))
// return
// }
// requestBody, err := io.ReadAll(r.Body)
// if err != nil {
// log.Println("Error reading http request (create) : ", err)
// w.WriteHeader(http.StatusBadRequest)
// r.Body.Close()
// return
// }
// defer r.Body.Close()
// var givenUpdatedRandomData RandomData
// err = json.Unmarshal(requestBody, &givenUpdatedRandomData)
// if err != nil {
// log.Println("Error unmarshalling request body (updateSpecificRandomData) : ", err)
// return
// }
// givenID := strings.Split(r.URL.String(), "/")[2]
// dbBytes, err := dbHandler.readDatabase()
// if err != nil {
// log.Println("Error reading db (updateSpecificRandomData) : ", err)
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
// var db []RandomData
// err = json.Unmarshal(dbBytes, &db)
// if err != nil {
// log.Println("Error unmarshalling database (update) : ", err)
// return
// }
// int64GivenID, _ := strconv.ParseInt(givenID, 10, 64)
// var counter int64
// for _, randomData := range db {
// if int64GivenID == randomData.ID {
// var updatedRandomData RandomData
// updatedRandomData = givenUpdatedRandomData
// updatedRandomData.ID = randomData.ID
// updatedRandomData.DateCreated = randomData.DateCreated
// updatedRandomData.LastUpdated = time.Now().UTC()
// dbHandler.removeRandomData(int64GivenID)
// dbHandler.writeRandomData(updatedRandomData)
// log.Printf("Successfully updated RandomData with id %v \n", updatedRandomData.ID)
// return
// }
// counter++
// }
// w.WriteHeader(http.StatusNotFound)
// }
// func (dbHandler *randomDataHandler) deleteSpecificRandomData(w http.ResponseWriter, r *http.Request) {
// givenID := strings.Split(r.URL.String(), "/")[2]
// int64GivenID, _ := strconv.ParseInt(givenID, 10, 64)
// err := dbHandler.removeRandomData(int64GivenID)
// if err != nil {
// log.Println("Error removing RandomData (deleteSpecificRandomData) : ", err)
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
// w.WriteHeader(http.StatusOK)
// log.Printf("Successfully deleted RandomData with id %v \n", int64GivenID)
// }
// func (dbHandler *randomDataHandler) handle(w http.ResponseWriter, r *http.Request) {
// switch r.Method {
// case "GET":
// dbHandler.get(w, r)
// case "POST":
// dbHandler.create(w, r)
// default:
// w.WriteHeader(http.StatusMethodNotAllowed)
// }
// }
// func (dbHandler *randomDataHandler) handleSpecific(w http.ResponseWriter, r *http.Request) {
// switch r.Method {
// case "GET":
// dbHandler.getSpecificRandomData(w, r)
// case "PUT":
// dbHandler.updateSpecificRandomData(w, r)
// case "DELETE":
// dbHandler.deleteSpecificRandomData(w, r)
// default:
// w.WriteHeader(http.StatusMethodNotAllowed)
// }
// }