Skip to content

Commit

Permalink
add polished html, css, and js
Browse files Browse the repository at this point in the history
change verbs to adjectives for url shortening
  • Loading branch information
xvargr committed Jul 19, 2024
1 parent 17d0f20 commit a659db3
Show file tree
Hide file tree
Showing 21 changed files with 492 additions and 298 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cSpell.words": ["clippit", "joho"]
"cSpell.words": ["Clipit", "clippit", "joho"]
}
153 changes: 153 additions & 0 deletions adjectives.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"adjective": [
"happy",
"sad",
"angry",
"excited",
"tired",
"brave",
"calm",
"charming",
"clever",
"confident",
"courageous",
"curious",
"eager",
"energetic",
"friendly",
"gentle",
"graceful",
"grateful",
"helpful",
"honest",
"humble",
"joyful",
"kind",
"lively",
"loving",
"loyal",
"mature",
"modest",
"nice",
"optimistic",
"patient",
"polite",
"proud",
"reliable",
"responsible",
"sincere",
"smart",
"strong",
"thoughtful",
"understanding",
"wise",
"adventurous",
"affectionate",
"ambitious",
"artistic",
"athletic",
"bold",
"bright",
"careful",
"cheerful",
"compassionate",
"considerate",
"creative",
"daring",
"dedicated",
"disciplined",
"elegant",
"empathetic",
"faithful",
"fearless",
"forgiving",
"funny",
"generous",
"gracious",
"hardworking",
"honorable",
"hopeful",
"imaginative",
"independent",
"inspiring",
"intelligent",
"jovial",
"kindhearted",
"knowledgeable",
"likable",
"logical",
"lovable",
"meticulous",
"motivated",
"obedient",
"observant",
"passionate",
"perceptive",
"persistent",
"playful",
"practical",
"punctual",
"radiant",
"rational",
"resourceful",
"respectful",
"selfless",
"sensible",
"sensitive",
"sociable",
"spontaneous",
"steadfast",
"supportive",
"tactful",
"talented",
"tenacious",
"trustworthy",
"unique",
"versatile",
"vibrant",
"vigorous",
"warm",
"witty",
"youthful",
"zealous",
"accomplished",
"adaptable",
"admirable",
"agreeable",
"alert",
"amiable",
"analytical",
"appreciative",
"assertive",
"attentive",
"benevolent",
"brilliant",
"candid",
"capable",
"charismatic",
"dependable",
"determined",
"devoted",
"diligent",
"discerning",
"dynamic",
"eloquent",
"endearing",
"enthusiastic",
"ethical",
"exuberant",
"fair",
"flexible",
"focused",
"hospitable",
"humorous",
"impartial",
"incisive",
"innovative",
"intuitive",
"judicious",
"keen",
"luminous",
"magnanimous"
]
}
9 changes: 9 additions & 0 deletions controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"
"net/http"

"github.com/xvargr/clippit/internal/URLShortener"
Expand All @@ -13,6 +14,7 @@ func HomepageHandler(w http.ResponseWriter, r *http.Request) {

func StaticHandler(w http.ResponseWriter, r *http.Request) {
fileName := r.PathValue("resourceName")
fmt.Println(fileName)
http.ServeFile(w, r, "./static/"+fileName)
}

Expand All @@ -23,6 +25,13 @@ func ShortenHandler(w http.ResponseWriter, r *http.Request) {
}

originalURL := r.FormValue("url")
if originalURL == "" {
http.Error(w, "Incomplete form data, URL is required", http.StatusBadRequest)
return
}

// idea: reject self references

shortenedURL := URLShortener.Instance().AddMapping(r, originalURL)
json.NewEncoder(w).Encode(shortenedURL)
}
Expand Down
13 changes: 8 additions & 5 deletions internal/URLShortener/URLShortener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package URLShortener

import (
"errors"
"log"
"math/rand/v2"
"net/http"
"strconv"
Expand All @@ -15,8 +16,8 @@ var instance *URLShortener
var once sync.Once

type Vocabulary struct {
Verb []string `json:"verb"`
Noun []string `json:"noun"`
Adjective []string `json:"adjective"`
Noun []string `json:"noun"`
}

type URLShortener struct {
Expand All @@ -36,7 +37,7 @@ func Instance() *URLShortener {
shortToLong: make(map[string]UrlStore),
longToShort: make(map[string]UrlStore),
}
fileReader.Read("verbs.json", &instance.vocabulary)
fileReader.Read("adjectives.json", &instance.vocabulary)
fileReader.Read("nouns.json", &instance.vocabulary)
})

Expand Down Expand Up @@ -70,6 +71,8 @@ func (u *URLShortener) AddMapping(r *http.Request, originalURL string) string {
u.shortToLong[shortKey] = short
u.longToShort[originalURL] = long

log.Default().Printf("Added mapping: %s -> %s\n", shortKey, originalURL)

return long.content
}

Expand Down Expand Up @@ -127,8 +130,8 @@ func (u *URLShortener) ResolveOriginalToShortKey(originalURL string) (string, bo

func (u *URLShortener) generateShortKey() string {
nounList := u.vocabulary.Noun
verbList := u.vocabulary.Verb
return verbList[rand.IntN(len(verbList))] + "-" + nounList[rand.IntN(len(nounList))] + "-" + strconv.Itoa(rand.IntN(99))
adjectiveList := u.vocabulary.Adjective
return adjectiveList[rand.IntN(len(adjectiveList))] + "-" + nounList[rand.IntN(len(nounList))] + "-" + strconv.Itoa(rand.IntN(99))
}

func generateShortUrl(r *http.Request, k string) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func GetConfig() *Config {
once.Do(func() {
instance = &Config{
Port: "8081",
PruneIntervalHour: time.Minute * 1,
PruneIntervalHour: time.Hour * 1,
}
})

Expand Down
2 changes: 0 additions & 2 deletions internal/scheduler/scheduler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package scheduler

import (
"fmt"
"time"
)

Expand All @@ -12,7 +11,6 @@ func Register(t time.Duration, task func()) {
go func() {
defer scheduler.Stop()
for {
fmt.Println("Scheduler waiting")
<-scheduler.C
task()
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {

mux := http.NewServeMux()
mux.HandleFunc("/{$}", HomepageHandler)
mux.HandleFunc("GET /static/{resourceName}", StaticHandler)
mux.HandleFunc("GET /static/{resourceName...}", StaticHandler)
mux.HandleFunc("POST /shorten", ShortenHandler)
mux.HandleFunc("GET /s/{keyword}", ResolverHandler)

Expand Down
Binary file added static/fonts/Beiruti-Black.ttf
Binary file not shown.
Binary file added static/fonts/Beiruti-Bold.ttf
Binary file not shown.
Binary file added static/fonts/Beiruti-ExtraBold.ttf
Binary file not shown.
Binary file added static/fonts/Beiruti-ExtraLight.ttf
Binary file not shown.
Binary file added static/fonts/Beiruti-Light.ttf
Binary file not shown.
Binary file added static/fonts/Beiruti-Medium.ttf
Binary file not shown.
Binary file added static/fonts/Beiruti-Regular.ttf
Binary file not shown.
Binary file added static/fonts/Beiruti-SemiBold.ttf
Binary file not shown.
117 changes: 116 additions & 1 deletion static/index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,118 @@
@font-face {
font-family: Beiruti;
src: local("Beiruti"),
url("../static/fonts/Beiruti-Regular.ttf") format("truetype");
}

html {
--theme-dark: #312924;
--theme-base: #463931;
--theme-light: #68554a;
--theme-lighter: #68554a;

--text-light: #e9e9e9;
}

body {
background-color: beige;
font-family: Beiruti, Arial, Helvetica, sans-serif;
color: #e9e9e9;
background-color: #68554a;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 56 28' width='56' height='28'%3E%3Cpath fill='%23312924' fill-opacity='0.4' d='M56 26v2h-7.75c2.3-1.27 4.94-2 7.75-2zm-26 2a2 2 0 1 0-4 0h-4.09A25.98 25.98 0 0 0 0 16v-2c.67 0 1.34.02 2 .07V14a2 2 0 0 0-2-2v-2a4 4 0 0 1 3.98 3.6 28.09 28.09 0 0 1 2.8-3.86A8 8 0 0 0 0 6V4a9.99 9.99 0 0 1 8.17 4.23c.94-.95 1.96-1.83 3.03-2.63A13.98 13.98 0 0 0 0 0h7.75c2 1.1 3.73 2.63 5.1 4.45 1.12-.72 2.3-1.37 3.53-1.93A20.1 20.1 0 0 0 14.28 0h2.7c.45.56.88 1.14 1.29 1.74 1.3-.48 2.63-.87 4-1.15-.11-.2-.23-.4-.36-.59H26v.07a28.4 28.4 0 0 1 4 0V0h4.09l-.37.59c1.38.28 2.72.67 4.01 1.15.4-.6.84-1.18 1.3-1.74h2.69a20.1 20.1 0 0 0-2.1 2.52c1.23.56 2.41 1.2 3.54 1.93A16.08 16.08 0 0 1 48.25 0H56c-4.58 0-8.65 2.2-11.2 5.6 1.07.8 2.09 1.68 3.03 2.63A9.99 9.99 0 0 1 56 4v2a8 8 0 0 0-6.77 3.74c1.03 1.2 1.97 2.5 2.79 3.86A4 4 0 0 1 56 10v2a2 2 0 0 0-2 2.07 28.4 28.4 0 0 1 2-.07v2c-9.2 0-17.3 4.78-21.91 12H30zM7.75 28H0v-2c2.81 0 5.46.73 7.75 2zM56 20v2c-5.6 0-10.65 2.3-14.28 6h-2.7c4.04-4.89 10.15-8 16.98-8zm-39.03 8h-2.69C10.65 24.3 5.6 22 0 22v-2c6.83 0 12.94 3.11 16.97 8zm15.01-.4a28.09 28.09 0 0 1 2.8-3.86 8 8 0 0 0-13.55 0c1.03 1.2 1.97 2.5 2.79 3.86a4 4 0 0 1 7.96 0zm14.29-11.86c1.3-.48 2.63-.87 4-1.15a25.99 25.99 0 0 0-44.55 0c1.38.28 2.72.67 4.01 1.15a21.98 21.98 0 0 1 36.54 0zm-5.43 2.71c1.13-.72 2.3-1.37 3.54-1.93a19.98 19.98 0 0 0-32.76 0c1.23.56 2.41 1.2 3.54 1.93a15.98 15.98 0 0 1 25.68 0zm-4.67 3.78c.94-.95 1.96-1.83 3.03-2.63a13.98 13.98 0 0 0-22.4 0c1.07.8 2.09 1.68 3.03 2.63a9.99 9.99 0 0 1 16.34 0z'%3E%3C/path%3E%3C/svg%3E");
padding: 1rem;
display: flex;
justify-content: center;
align-items: center;
}
body > form {
background-color: var(--theme-base);
max-width: 40rem;
display: flex;
flex-grow: 1;
flex-direction: column;
gap: 1rem;
padding: 1rem 2rem;
justify-content: center;
align-items: center;
border-radius: 0.75rem;
}

h1 {
font-size: 2.5rem;
margin-bottom: 0.5rem;
}

a {
color: var(--text-light);
text-decoration: none;
}

input,
button {
font-family: Beiruti, Arial, Helvetica, sans-serif;
}

button {
font-size: 1.25rem;
font-weight: bold;
background-color: var(--theme-light);
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
color: var(--text-light);
border: none;
padding: 0.5rem 1rem;
border-radius: 0.75rem;
transition: background-color 0.2s;
}
button:hover {
background-color: #927765;
}
button:disabled {
background-color: inherit;
opacity: 0.5;
cursor: default;
}

.svg-icon {
width: 1.5rem;
height: 1.5rem;
}

#input-container {
display: flex;
gap: 1rem;
justify-content: center;
align-items: center;
width: 100%;
}
#input-container input {
background-color: var(--theme-base);
color: var(--text-light);
padding: 0.5rem;
border: none;
font-size: 1.25rem;
flex-grow: 1;
outline: none;
border-bottom: 1px solid rgba(255, 255, 255, 0.5);
}
#input-container input:focus {
border-bottom: 1px solid rgba(255, 255, 255, 255);
}

#result-container {
background-color: var(--theme-light);
color: var(--text-light);
width: 100%;
padding: 0.25rem;
display: flex;
gap: 1rem;
justify-items: center;
align-items: center;
border-radius: 0.75rem;
}
#result-container #url {
flex-grow: 1;
font-size: 1.25rem;
padding: 0 0.5rem;
}
Loading

0 comments on commit a659db3

Please sign in to comment.