From 28cf0f1a9387cd0f9ec59d6b018b2ebaf6df1e88 Mon Sep 17 00:00:00 2001 From: dadgam3er Date: Sat, 21 Sep 2024 15:30:36 -0400 Subject: [PATCH] HTTPSERVER: Fix 2 --- .../.codecrafters/compile.sh | 11 +++ .../.codecrafters/run.sh | 11 +++ codecrafters-http-server-go/.gitattributes | 1 + codecrafters-http-server-go/README.md | 37 +++++++++ codecrafters-http-server-go/app/server.go | 43 +++++++++++ codecrafters-http-server-go/codecrafters.yml | 11 +++ codecrafters-http-server-go/go.mod | 11 +++ codecrafters-http-server-go/go.sum | 0 codecrafters-http-server-go/your_program.sh | 24 ++++++ .../.codecrafters/compile.sh | 11 +++ codecrafters-sqlite-go/.codecrafters/run.sh | 11 +++ codecrafters-sqlite-go/.gitattributes | 1 + codecrafters-sqlite-go/.gitignore | 2 + codecrafters-sqlite-go/README.md | 76 +++++++++++++++++++ codecrafters-sqlite-go/app/main.go | 61 +++++++++++++++ codecrafters-sqlite-go/codecrafters.yml | 11 +++ .../download_sample_databases.sh | 9 +++ codecrafters-sqlite-go/go.mod | 13 ++++ codecrafters-sqlite-go/go.sum | 2 + codecrafters-sqlite-go/your_program.sh | 24 ++++++ codingCh/fibonacciSEQ/fibonacci.go | 37 +++++++++ codingCh/fibonacciSEQ/go.mod | 3 + 22 files changed, 410 insertions(+) create mode 100755 codecrafters-http-server-go/.codecrafters/compile.sh create mode 100755 codecrafters-http-server-go/.codecrafters/run.sh create mode 100644 codecrafters-http-server-go/.gitattributes create mode 100644 codecrafters-http-server-go/README.md create mode 100644 codecrafters-http-server-go/app/server.go create mode 100644 codecrafters-http-server-go/codecrafters.yml create mode 100644 codecrafters-http-server-go/go.mod create mode 100644 codecrafters-http-server-go/go.sum create mode 100755 codecrafters-http-server-go/your_program.sh create mode 100755 codecrafters-sqlite-go/.codecrafters/compile.sh create mode 100755 codecrafters-sqlite-go/.codecrafters/run.sh create mode 100644 codecrafters-sqlite-go/.gitattributes create mode 100644 codecrafters-sqlite-go/.gitignore create mode 100644 codecrafters-sqlite-go/README.md create mode 100644 codecrafters-sqlite-go/app/main.go create mode 100644 codecrafters-sqlite-go/codecrafters.yml create mode 100755 codecrafters-sqlite-go/download_sample_databases.sh create mode 100644 codecrafters-sqlite-go/go.mod create mode 100644 codecrafters-sqlite-go/go.sum create mode 100755 codecrafters-sqlite-go/your_program.sh create mode 100644 codingCh/fibonacciSEQ/fibonacci.go create mode 100644 codingCh/fibonacciSEQ/go.mod diff --git a/codecrafters-http-server-go/.codecrafters/compile.sh b/codecrafters-http-server-go/.codecrafters/compile.sh new file mode 100755 index 0000000..367db1d --- /dev/null +++ b/codecrafters-http-server-go/.codecrafters/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to compile your program on CodeCrafters +# +# This runs before .codecrafters/run.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +go build -o /tmp/codecrafters-build-http-server-go app/*.go diff --git a/codecrafters-http-server-go/.codecrafters/run.sh b/codecrafters-http-server-go/.codecrafters/run.sh new file mode 100755 index 0000000..7eead76 --- /dev/null +++ b/codecrafters-http-server-go/.codecrafters/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to run your program on CodeCrafters +# +# This runs after .codecrafters/compile.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +exec /tmp/codecrafters-build-http-server-go "$@" diff --git a/codecrafters-http-server-go/.gitattributes b/codecrafters-http-server-go/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/codecrafters-http-server-go/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/codecrafters-http-server-go/README.md b/codecrafters-http-server-go/README.md new file mode 100644 index 0000000..b63a6cc --- /dev/null +++ b/codecrafters-http-server-go/README.md @@ -0,0 +1,37 @@ +[![progress-banner](https://backend.codecrafters.io/progress/http-server/79632794-98f7-44ac-8abd-57a3a6fac4aa)](https://app.codecrafters.io/users/codecrafters-bot?r=2qF) + +This is a starting point for Go solutions to the +["Build Your Own HTTP server" Challenge](https://app.codecrafters.io/courses/http-server/overview). + +[HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is the +protocol that powers the web. In this challenge, you'll build a HTTP/1.1 server +that is capable of serving multiple clients. + +Along the way you'll learn about TCP servers, +[HTTP request syntax](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html), +and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your HTTP server implementation is in `app/server.go`. Study +and uncomment the relevant code, and push your changes to pass the first stage: + +```sh +git commit -am "pass 1st stage" # any msg +git push origin master +``` + +Time to move on to the next stage! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `go (1.19)` installed locally +1. Run `./your_program.sh` to run your program, which is implemented in + `app/server.go`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/codecrafters-http-server-go/app/server.go b/codecrafters-http-server-go/app/server.go new file mode 100644 index 0000000..d5797ad --- /dev/null +++ b/codecrafters-http-server-go/app/server.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "net" + "os" + "strings" +) + +// Ensures gofmt doesn't remove the "net" and "os" imports above (feel free to remove this!) +var ( + _ = net.Listen + _ = os.Exit +) + +func main() { + // You can use print statements as follows for debugging, they'll be visible when running tests. + fmt.Println("Logs from your program will appear here!") + + // Uncomment this block to pass the first stage + + l, err := net.Listen("tcp", "0.0.0.0:4221") + if err != nil { + fmt.Println("Failed to bind to port 4221") + os.Exit(1) + } + defer l.Close() + conn, err := l.Accept() + if err != nil { + fmt.Println("Error accepting connection: ", err.Error()) + os.Exit(1) + } + req := make([]byte, 1024) + + conn.Read(req) + + if !strings.HasPrefix(string(req), "GET / HTTP/1.1") { + conn.Write([]byte("HTTP/1.1 404 Not Found\r\n\r\n")) + conn.Close() + return + } + conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n")) +} diff --git a/codecrafters-http-server-go/codecrafters.yml b/codecrafters-http-server-go/codecrafters.yml new file mode 100644 index 0000000..77886cf --- /dev/null +++ b/codecrafters-http-server-go/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Go version used to run your code +# on Codecrafters. +# +# Available versions: go-1.22 +language_pack: go-1.22 diff --git a/codecrafters-http-server-go/go.mod b/codecrafters-http-server-go/go.mod new file mode 100644 index 0000000..28bcc97 --- /dev/null +++ b/codecrafters-http-server-go/go.mod @@ -0,0 +1,11 @@ +// DON'T EDIT THIS! +// +// Codecrafters relies on this file being intact to run tests successfully. Any changes +// here will not reflect when CodeCrafters tests your code, and might even cause build +// failures. +// +// DON'T EDIT THIS! + +module github.com/codecrafters-io/http-server-starter-go + +go 1.22 \ No newline at end of file diff --git a/codecrafters-http-server-go/go.sum b/codecrafters-http-server-go/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/codecrafters-http-server-go/your_program.sh b/codecrafters-http-server-go/your_program.sh new file mode 100755 index 0000000..ad01720 --- /dev/null +++ b/codecrafters-http-server-go/your_program.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# +# Use this script to run your program LOCALLY. +# +# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit early if any commands fail + +# Copied from .codecrafters/compile.sh +# +# - Edit this to change how your program compiles locally +# - Edit .codecrafters/compile.sh to change how your program compiles remotely +( + cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory + go build -o /tmp/codecrafters-build-http-server-go app/*.go +) + +# Copied from .codecrafters/run.sh +# +# - Edit this to change how your program runs locally +# - Edit .codecrafters/run.sh to change how your program runs remotely +exec /tmp/codecrafters-build-http-server-go "$@" diff --git a/codecrafters-sqlite-go/.codecrafters/compile.sh b/codecrafters-sqlite-go/.codecrafters/compile.sh new file mode 100755 index 0000000..abe1659 --- /dev/null +++ b/codecrafters-sqlite-go/.codecrafters/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to compile your program on CodeCrafters +# +# This runs before .codecrafters/run.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +go build -o /tmp/codecrafters-build-sqlite-go app/*.go diff --git a/codecrafters-sqlite-go/.codecrafters/run.sh b/codecrafters-sqlite-go/.codecrafters/run.sh new file mode 100755 index 0000000..39547b9 --- /dev/null +++ b/codecrafters-sqlite-go/.codecrafters/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to run your program on CodeCrafters +# +# This runs after .codecrafters/compile.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +exec /tmp/codecrafters-build-sqlite-go "$@" diff --git a/codecrafters-sqlite-go/.gitattributes b/codecrafters-sqlite-go/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/codecrafters-sqlite-go/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/codecrafters-sqlite-go/.gitignore b/codecrafters-sqlite-go/.gitignore new file mode 100644 index 0000000..db38cf4 --- /dev/null +++ b/codecrafters-sqlite-go/.gitignore @@ -0,0 +1,2 @@ +# Database files used for testing +*.db diff --git a/codecrafters-sqlite-go/README.md b/codecrafters-sqlite-go/README.md new file mode 100644 index 0000000..c730c67 --- /dev/null +++ b/codecrafters-sqlite-go/README.md @@ -0,0 +1,76 @@ +[![progress-banner](https://backend.codecrafters.io/progress/sqlite/48f6806f-f61a-405d-aca7-6fde3bb6d8b1)](https://app.codecrafters.io/users/codecrafters-bot?r=2qF) + +This is a starting point for Go solutions to the +["Build Your Own SQLite" Challenge](https://codecrafters.io/challenges/sqlite). + +In this challenge, you'll build a barebones SQLite implementation that supports +basic SQL queries like `SELECT`. Along the way we'll learn about +[SQLite's file format](https://www.sqlite.org/fileformat.html), how indexed data +is +[stored in B-trees](https://jvns.ca/blog/2014/10/02/how-does-sqlite-work-part-2-btrees/) +and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your SQLite implementation is in `app/main.go`. Study and +uncomment the relevant code, and push your changes to pass the first stage: + +```sh +git commit -am "pass 1st stage" # any msg +git push origin master +``` + +Time to move on to the next stage! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `go (1.16+)` installed locally +1. Run `./your_program.sh` to run your program, which is implemented in + `app/main.go`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. + +# Sample Databases + +To make it easy to test queries locally, we've added a sample database in the +root of this repository: `sample.db`. + +This contains two tables: `apples` & `oranges`. You can use this to test your +implementation for the first 6 stages. + +You can explore this database by running queries against it like this: + +```sh +$ sqlite3 sample.db "select id, name from apples" +1|Granny Smith +2|Fuji +3|Honeycrisp +4|Golden Delicious +``` + +There are two other databases that you can use: + +1. `superheroes.db`: + - This is a small version of the test database used in the table-scan stage. + - It contains one table: `superheroes`. + - It is ~1MB in size. +1. `companies.db`: + - This is a small version of the test database used in the index-scan stage. + - It contains one table: `companies`, and one index: `idx_companies_country` + - It is ~7MB in size. + +These aren't included in the repository because they're large in size. You can +download them by running this script: + +```sh +./download_sample_databases.sh +``` + +If the script doesn't work for some reason, you can download the databases +directly from +[codecrafters-io/sample-sqlite-databases](https://github.com/codecrafters-io/sample-sqlite-databases). diff --git a/codecrafters-sqlite-go/app/main.go b/codecrafters-sqlite-go/app/main.go new file mode 100644 index 0000000..79eb2aa --- /dev/null +++ b/codecrafters-sqlite-go/app/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "bytes" + "encoding/binary" + "fmt" + "log" + "os" + "regexp" + // Available if you need it! + // "github.com/xwb1989/sqlparser" +) + +// Usage: your_program.sh sample.db .dbinfo +func main() { + databaseFilePath := os.Args[1] + command := os.Args[2] + + switch command { + case ".dbinfo": + databaseFile, err := os.Open(databaseFilePath) + if err != nil { + log.Fatal(err) + } + + header := make([]byte, 100) + + _, err = databaseFile.Read(header) + if err != nil { + log.Fatal(err) + } + + var pageSize uint16 + if err := binary.Read(bytes.NewReader(header[16:18]), binary.BigEndian, &pageSize); err != nil { + fmt.Println("Failed to read integer:", err) + return + } + // You can use print statements as follows for debugging, they'll be visible when running tests. + fmt.Println("Logs from your program will appear here!") + + // Uncomment this to pass the first stage + fmt.Printf("database page size: %v", pageSize) + schemaBuffer := make([]byte, pageSize) + + _, err = databaseFile.Read(schemaBuffer) + if err != nil { + log.Fatal(err) + } + + re, _ := regexp.Compile("CREATE TABLE") + + tables := re.FindAll(schemaBuffer, -1) + + fmt.Printf("database page size: %v\n", pageSize) + + fmt.Printf("number of tables: %v\n", len(tables)) + default: + fmt.Println("Unknown command", command) + os.Exit(1) + } +} diff --git a/codecrafters-sqlite-go/codecrafters.yml b/codecrafters-sqlite-go/codecrafters.yml new file mode 100644 index 0000000..77886cf --- /dev/null +++ b/codecrafters-sqlite-go/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Go version used to run your code +# on Codecrafters. +# +# Available versions: go-1.22 +language_pack: go-1.22 diff --git a/codecrafters-sqlite-go/download_sample_databases.sh b/codecrafters-sqlite-go/download_sample_databases.sh new file mode 100755 index 0000000..03e0573 --- /dev/null +++ b/codecrafters-sqlite-go/download_sample_databases.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +echo "Downloading superheroes.db: ~1MB (used in stage 7)" +curl -Lo superheroes.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/superheroes.db + +echo "Downloading companies.db: ~7MB (used in stage 8)" +curl -Lo companies.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/companies.db + +echo "Sample databases downloaded." diff --git a/codecrafters-sqlite-go/go.mod b/codecrafters-sqlite-go/go.mod new file mode 100644 index 0000000..360fb15 --- /dev/null +++ b/codecrafters-sqlite-go/go.mod @@ -0,0 +1,13 @@ +// DON'T EDIT THIS! +// +// Codecrafters relies on this file being intact to run tests successfully. Any changes +// here will not reflect when CodeCrafters tests your code, and might even cause build +// failures. +// +// DON'T EDIT THIS! + +module github/com/codecrafters-io/sqlite-starter-go + +go 1.22 + +require github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 diff --git a/codecrafters-sqlite-go/go.sum b/codecrafters-sqlite-go/go.sum new file mode 100644 index 0000000..6354a21 --- /dev/null +++ b/codecrafters-sqlite-go/go.sum @@ -0,0 +1,2 @@ +github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ= +github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2/go.mod h1:hzfGeIUDq/j97IG+FhNqkowIyEcD88LrW6fyU3K3WqY= diff --git a/codecrafters-sqlite-go/your_program.sh b/codecrafters-sqlite-go/your_program.sh new file mode 100755 index 0000000..a68b4ee --- /dev/null +++ b/codecrafters-sqlite-go/your_program.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# +# Use this script to run your program LOCALLY. +# +# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit early if any commands fail + +# Copied from .codecrafters/compile.sh +# +# - Edit this to change how your program compiles locally +# - Edit .codecrafters/compile.sh to change how your program compiles remotely +( + cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory + go build -o /tmp/codecrafters-build-sqlite-go app/*.go +) + +# Copied from .codecrafters/run.sh +# +# - Edit this to change how your program runs locally +# - Edit .codecrafters/run.sh to change how your program runs remotely +exec /tmp/codecrafters-build-sqlite-go "$@" diff --git a/codingCh/fibonacciSEQ/fibonacci.go b/codingCh/fibonacciSEQ/fibonacci.go new file mode 100644 index 0000000..0070175 --- /dev/null +++ b/codingCh/fibonacciSEQ/fibonacci.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "strconv" +) + +func FibonacciLoop(n int) int { + f := make([]int, n+1, n+2) + if n < 2 { + f = f[0:2] + } + f[0] = 0 + f[1] = 1 + for i := 2; i <= n; i++ { + f[i] = f[i-1] + f[i-2] + } + return f[n] +} + +func FionaccoRecursion(n int) int { + if n <= 1 { + return n + } + return FionaccoRecursion(n-1) + FionaccoRecursion(n-2) +} + +func main() { + for i := 0; i <= 50; i++ { + fmt.Print(strconv.Itoa(FibonacciLoop(i)) + " ") + } + fmt.Println("") + for i := 0; i <= 50; i++ { + fmt.Print(strconv.Itoa(FionaccoRecursion(i)) + " ") + } + fmt.Println("") +} diff --git a/codingCh/fibonacciSEQ/go.mod b/codingCh/fibonacciSEQ/go.mod new file mode 100644 index 0000000..018c3c7 --- /dev/null +++ b/codingCh/fibonacciSEQ/go.mod @@ -0,0 +1,3 @@ +module Fibonacci + +go 1.22.7