Example #1
0
func (ch Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	path := r.URL.Path
	variables := request.GetURLVariables(path)
	switch {
	case request.Is(r, "GET") && path == "/":
		ch.handleGetAll(w, r)
	case request.Is(r, "GET") && len(variables) == 1:
		ch.handleGet(w, r, variables[0])
	case request.Is(r, "POST") && path == "/":
		ch.handleAdd(w, r)
	case request.Is(r, "DELETE") && len(variables) == 1:
		ch.handleDelete(w, r, variables[0])
	case request.Is(r, "PATCH") && len(variables) == 1:
		ch.handleModify(w, r, variables[0])
	default:
		http.Error(w, "", http.StatusNotImplemented)

	}

}
Example #2
0
File: main.go Project: wrrn/restapi
func main() {
	var (
		db                          = SetupDB()
		authentication *auth.Auth   = &auth.Auth{db}
		configHandler  http.Handler = confighandler.Handler{
			configuration.ConfigurationController{db},
		}
	)
	authentication.RegisterUser(auth.User{Username: "******", Password: "******"})
	configHandler = authentication.VerifySessions(configHandler)

	mux := http.NewServeMux()

	// Login
	mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
		if !request.Is(r, "POST") {
			response.MethodNotAllowed(w)
			return
		}
		authentication.HandleLogin(w, r)
		return
	})

	//Logout
	mux.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
		if !request.Is(r, "POST") {
			response.MethodNotAllowed(w)
			return
		}
		authentication.HandleLogout(w, r)
	})

	mux.Handle("/configurations/", http.StripPrefix("/configurations", configHandler))

	log.Fatal(http.ListenAndServe(":8080", mux))

}