Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
icyflame committed Jan 3, 2018
0 parents commit c0b1b90
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*]
indent_style=space
indent_size=4
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
leprechaun-prototype
6 changes: 6 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

type jsonErr struct {
Code int `json:"code"`
Text string `json:"text"`
}
38 changes: 38 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"io/ioutil"
"net/http"
// "strconv"
// "github.com/gorilla/mux"
// "encoding/json"
)

func Index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
b, err := ioutil.ReadFile("index.html")
if err != nil {
fmt.Fprintf(w, "Could not read HTML file from disk. Error: %v", err)
} else {
fmt.Fprintf(w, "%s", b)
}
}

func BeginAuth(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
roll := r.PostForm.Get("roll")
email := r.PostForm.Get("email")

if !validRoll(roll) {
fmt.Fprintf(w, "Roll number not valid. Please try again")
return
}

if !validEmail(email) {
fmt.Fprintf(w, "Email not valid. Please try again")
return
}

fmt.Fprint(w, "You have submitted valid roll and email")
}
11 changes: 11 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

// TODO
func validRoll(roll string) bool {
return true
}

// TODO
func validEmail(email string) bool {
return true
}
65 changes: 65 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!doctype html>
<html lang="en">
<head>
<title>Leprechaun - KGP ERP Authentication Service</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
</head>
<body style="margin:40px;">

<div class="container">
<header class="header clearfix">
<nav>
<ul class="nav nav-pills float-right">
<li class="nav-item">
<a class="nav-link active" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link"
href="https://github.com/metakgp"
target="_blank">Github</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://metakgp.github.io" target="_blank">About</a>
</li>
</ul>
</nav>
<h3>Leprechaun</h3>
</header>

<main role="main">

<div class="jumbotron">
<h1 class="display-3">
Leprechaun - KGP ERP Authentication service
</h1>
<h3>
Authenticate yourself now! Connect your email ID to your
roll number, and enjoy the myriad services provided
throughout the campus!
</h3>
</div>

<form method="POST" action="/auth">
<input type="text" name="roll" placeholder="Enter your roll number"
autofocus />
<br>
<input type="text" name="email" placeholder="Enter your email address"
/>
<br>
<input type="submit" value="Begin authentication process now!">
</form>

</main>

<footer class="footer">
<hr>
<p>Leprechaun - Metakgp 2017</p>
</footer>

</body>
</html>
23 changes: 23 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"log"
"net/http"
"time"
)

func Logger(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()

inner.ServeHTTP(w, r)

log.Printf(
"%s\t%s\t%s\t%s",
r.Method,
r.RequestURI,
name,
time.Since(start),
)
})
}
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"log"
"net/http"
"github.com/joho/godotenv"
"os"
)

func main() {

err := godotenv.Load()
if err != nil {
log.Print("Error loading .env file")
}

router := NewRouter()

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./public"))))

log.Fatal(http.ListenAndServe(":" + os.Getenv("PORT"), router))
}
1 change: 1 addition & 0 deletions public/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testing
41 changes: 41 additions & 0 deletions repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import "fmt"

var currentId int

var todos Todos

// Give us some seed data
func init() {
RepoCreateTodo(Todo{Name: "Write presentation"})
RepoCreateTodo(Todo{Name: "Host meetup"})
}

func RepoFindTodo(id int) Todo {
for _, t := range todos {
if t.Id == id {
return t
}
}
// return empty Todo if not found
return Todo{}
}

//this is bad, I don't think it passes race condtions
func RepoCreateTodo(t Todo) Todo {
currentId += 1
t.Id = currentId
todos = append(todos, t)
return t
}

func RepoDestroyTodo(id int) error {
for i, t := range todos {
if t.Id == id {
todos = append(todos[:i], todos[i+1:]...)
return nil
}
}
return fmt.Errorf("Could not find Todo with id of %d to delete", id)
}
27 changes: 27 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"net/http"

"github.com/gorilla/mux"
)

func NewRouter() *mux.Router {

router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler

handler = route.HandlerFunc
handler = Logger(handler, route.Name)

router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)

}

return router
}
27 changes: 27 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import "net/http"

type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}

type Routes []Route

var routes = Routes{
Route{
"Index",
"GET",
"/",
Index,
},
Route{
"BeginAuth",
"POST",
"/auth",
BeginAuth,
},
}
12 changes: 12 additions & 0 deletions todo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import "time"

type Todo struct {
Id int `json:"id"`
Name string `json:"name"`
Completed bool `json:"completed"`
Due time.Time `json:"due"`
}

type Todos []Todo

0 comments on commit c0b1b90

Please sign in to comment.