commit 4fe6fed8c4ba6ae8773e62a13c66bc7e0617f186 Author: Guillermo Serrahima Poncet Date: Tue Nov 5 18:39:30 2019 +0100 Initial commit Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..ad6e9c2 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Gochat + +Experiment to practice Golang for the first time following the "REST API with Mux" tutorial [here](https://www.youtube.com/watch?v=SonwZ6MF5BE). diff --git a/gochat b/gochat new file mode 100755 index 0000000..63a6d39 Binary files /dev/null and b/gochat differ diff --git a/main.go b/main.go new file mode 100644 index 0000000..c928fc2 --- /dev/null +++ b/main.go @@ -0,0 +1,115 @@ +package main + +import ( + "encoding/json" + "fmt" + "github.com/gorilla/mux" + "log" + "math/rand" + "net/http" + "strconv" + "time" +) + +// Message struct +type Text struct { + Content string `json:"content"` + Author string `json:"author"` +} + +type Message struct { + Id string `json:"id"` + Content Text `json:"content"` +} + +// Chat as a slice of Messages +var chat []Message + +// Possible Messages +var quickchat []string + +//Possible authors +var users []string + +var idCount int + +func getChat(writer http.ResponseWriter, request *http.Request) { + writer.Header().Set("Content-Type", "application/json") + json.NewEncoder(writer).Encode(chat) +} +func getMsg(writer http.ResponseWriter, request *http.Request) { + writer.Header().Set("Content-Type", "application/json") + params := mux.Vars(request) + for _, msg := range chat { + if msg.Id == params["id"] { + json.NewEncoder(writer).Encode(msg) + return + } + } +} + +func createMsg() (string, string) { + content := quickchat[rand.Intn(len(quickchat))] + author := users[rand.Intn(len(users))] + + return content, author +} + +func sendMsg(writer http.ResponseWriter, request *http.Request) { + writer.Header().Set("Content-Type", "application/json") + var newMsg Message + _ = json.NewDecoder(request.Body).Decode(&(newMsg.Content)) + newMsg.Id = strconv.Itoa(idCount) + idCount++ + chat = append(chat, newMsg) + json.NewEncoder(writer).Encode(newMsg) + fmt.Println("New Message:") + fmt.Println("#", newMsg.Id, newMsg.Content.Author, ":", newMsg.Content.Content) +} +func editMsg(writer http.ResponseWriter, request *http.Request) { + writer.Header().Set("Content-Type", "application/json") + +} +func deleteMsg(writer http.ResponseWriter, request *http.Request) { + writer.Header().Set("Content-Type", "application/json") + + params := mux.Vars(request) + for index, msg := range chat { + if msg.Id == params["id"] { + chat = append(chat[:index], chat[index+1:]...) + break + } + } + json.NewEncoder(writer).Encode(chat) +} + +func main() { + // Init rand + rand.Seed(time.Now().Unix()) + + // Init server router + router := mux.NewRouter() + + // Init chat variables + quickchat = []string{"Wow!", "OMG!", "Nice shot!", "What a save!", "Sorry!", "My bad."} + users = []string{"Chipper", "Beast", "Jester", "Merlin", "Bandit", "Rainmaker"} + + first := Message{"0", Text{"The chat room is open.", "SERVER"}} + chat = append(chat, first) + idCount = 1 + + // Handlers + router.HandleFunc("/api/chat", getChat).Methods("GET") + router.HandleFunc("/api/chat/{id}", getMsg).Methods("GET") + router.HandleFunc("/api/chat", sendMsg).Methods("POST") + router.HandleFunc("/api/chat/{id}", editMsg).Methods("PUT") + router.HandleFunc("/api/chat/{id}", deleteMsg).Methods("DELETE") + + // Log + log.Fatal(http.ListenAndServe(":8000", router)) + + last := Message{"-1", Text{"The chat room is closed.", "SERVER"}} + chat = append(chat, last) + + return +}