Skip to content

Commit

Permalink
/ Redirect to /s if static index.html exists
Browse files Browse the repository at this point in the history
  • Loading branch information
thbkrkr committed Oct 11, 2015
1 parent 8126f39 commit 1372393
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 10 deletions.
84 changes: 79 additions & 5 deletions example/api/_static/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,80 @@

<!doctype html>
<h1>Hello</h1>
Sun Sep 13 11:51:34 CEST 2015
<br>
Sun Sep 13 11:51:36 CEST 2015
<br>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>apish</title>
<link rel="stylesheet" href="//cdn.rawgit.com/yahoo/pure-release/v0.6.0/pure-min.css">
<link href='//fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'>
</head>
<body>

<div class="main">

<div class="header">
<h1>meow</h1>
</div>

<div class="content">
<center>
<a href="http://thecatapi.com">
<img class="pure-img-responsive" src="https://thecatapi.com/api/images/get?format=src&type=jpg">
</a>
</center>
</p>

</div>
<div class="footer">
&copy;2015 krkr
</div>
</div>

</body>
</html>
<style>
body {
color: #777;
font-family: 'Raleway', sans-serif;
}

.pure-img-responsive {
max-width: 100%;
height: auto;
}

.main {
margin: 0 auto;
padding: 0 2em;
}
/*
The content `<div>` is where all your content goes.
*/
.content {
margin: 0 auto;
padding: 2em 1em 1em;
max-width: 800px;
line-height: 1.6em;
}

.header {
margin: 0;
color: #333;
text-align: center;
padding: 1em 2em 1em;
border-bottom: 1px solid #eee;
}

.header h1 {
margin: 0.2em 0;
font-size: 3em;
font-weight: 300;
color: #444;
}

.footer {
border-top: 1px solid #eee;
text-align: center;
padding: 1em 2em;
}
</style>
26 changes: 21 additions & 5 deletions router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"net/http"
"os"

"github.com/gin-gonic/gin"
h "github.com/thbkrkr/go-apish/handlers"
m "github.com/thbkrkr/go-apish/middlewares"
Expand Down Expand Up @@ -49,12 +52,25 @@ func Router() *gin.Engine {

/** Base routes */

func indexExists() bool {
if _, err := os.Stat(*apiDir + "/_static/index.html"); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}

func index(c *gin.Context) {
c.JSON(200, gin.H{
"ok": true,
"status": 200,
"name": "go-apish",
})
if indexExists() {
c.Redirect(http.StatusMovedPermanently, "/s")
} else {
c.JSON(200, gin.H{
"ok": true,
"status": 200,
"name": "go-apish",
})
}
}

func favicon(c *gin.Context) {
Expand Down

0 comments on commit 1372393

Please sign in to comment.