Skip to content

Instantly share code, notes, and snippets.

View elijahomolo's full-sized avatar

Elijah Omolo elijahomolo

View GitHub Profile
@elijahomolo
elijahomolo / main.go
Created November 21, 2025 20:32
SIMPLE GO SERVER
// Simple web server in Go
// All go applications start with package main, which defines a standalone executable program
package main
// Import necessary packages
import (
"log" // Package for logging
"net/http" // Package for HTTP client and server implementations
)
23:07:57.558: [macOS] Permission for audio device access granted.
23:07:57.560: [macOS] Permission for video device access granted.
23:07:57.560: [macOS] Permission for input monitoring denied.
23:07:57.563: [macOS] Permission for screen capture granted.
23:07:57.564: CPU Name: Apple M4
23:07:57.564: Physical Cores: 10, Logical Cores: 10
23:07:57.564: Physical Memory: 16384MB Total
23:07:57.564: Model Identifier: Mac16,1
23:07:57.564: OS Name: macOS
23:07:57.564: OS Version: Version 15.6.1 (Build 24G90)
@elijahomolo
elijahomolo / insert.go
Created April 13, 2022 20:15
insert function that encrypts the user password.
func Insert(w http.ResponseWriter, r *http.Request) {
db := utils.DbConn()
//If it's a post request, assign a variable to the value returned in each field of the New page.
if r.Method == "POST" {
username := r.FormValue("username")
password := r.FormValue("password")
city := r.FormValue("city")
email := r.FormValue("email")
// encrypt password
@elijahomolo
elijahomolo / main.go
Created April 9, 2022 13:46
mvc server
package main
import (
"github.com/eomolo/user_auth/myapp/routes"
_ "github.com/lib/pq"
"log"
"net/http"
)
func main() {
@elijahomolo
elijahomolo / routes.go
Last active April 9, 2022 13:43
routes/routes.go
package routes
import (
"github.com/eomolo/user_auth/myapp/controllers"
"github.com/gorilla/mux"
)
func Handlers() *mux.Router {
r := mux.NewRouter().StrictSlash(true)
@elijahomolo
elijahomolo / db.go
Created April 8, 2022 23:16
utils/db.go
package utils
import (
"database/sql"
"fmt"
"github.com/joho/godotenv"
"log"
"os"
)
@elijahomolo
elijahomolo / user.go
Created April 8, 2022 22:58
package controllers
package controllers
import (
"github.com/eomolo/user_auth/myapp/models"
"github.com/eomolo/user_auth/myapp/utils"
_ "github.com/lib/pq"
"log"
"net/http"
"text/template"
)
@elijahomolo
elijahomolo / user.go
Created April 7, 2022 14:22
Defining a user model file
package models
// define a user model
type User struct {
Id int
Username string
City string
Email string
Password string
}
@elijahomolo
elijahomolo / main.go
Created April 2, 2022 21:13
CRUD operations
package main
import (
"database/sql"
"fmt"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"log"
"net/http"
"os"
@elijahomolo
elijahomolo / delete.go
Created April 2, 2022 21:11
remove a user from a database
func Delete(w http.ResponseWriter, r *http.Request) {
db := dbConn()
usr := r.URL.Query().Get("id")
delForm, err := db.Prepare(`DELETE FROM public."users" WHERE user_id=$1`)
if err != nil {
panic(err.Error())
}
delForm.Exec(usr)
log.Println("DELETE")
defer db.Close()