本文整理匯總了Golang中github.com/codegangsta/martini.Static函數的典型用法代碼示例。如果您正苦於以下問題:Golang Static函數的具體用法?Golang Static怎麽用?Golang Static使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Static函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
autoUpdate()
m := martini.Classic()
m.Use(martini.Static("static"))
m.Use(render.Renderer())
m.Get("/", func(r render.Render) {
r.HTML(200, "content", []interface{}{getPage(1)})
})
m.Get("/api/:id", func(params martini.Params, r render.Render) {
s := strings.Trim(params["id"], " .)(")
id := atoi(s)
r.JSON(200, getPage(id))
})
m.Get("/page/:id", func(params martini.Params, r render.Render) {
s := strings.Trim(params["id"], " .)(")
id := atoi(s)
r.HTML(200, "content", []interface{}{getPage(id)})
})
http.ListenAndServe("0.0.0.0:8000", m)
m.Run()
}
示例2: main
func main() {
m := martini.Classic()
if os.Getenv("MARTINI_ENV") == "production" {
g.devMode = false
}
if g.IsDevMode() {
m.Use(runnerMiddleware)
}
g.InitDb()
m.Use(martini.Static("public/app"))
m.Use(func(resp http.ResponseWriter, req *http.Request) {
if strings.HasPrefix(req.URL.Path, "/api/") {
token := req.Header.Get("AuthToken")
if token != "" {
n, err := g.Db().SelectInt(`select count(*) from users where token=$1`, token)
if err != nil && err != sql.ErrNoRows {
log.Fatalf(err.Error())
}
if n > 0 {
return
}
}
resp.WriteHeader(http.StatusUnauthorized)
resp.Write([]byte("You're not allowed to do this, sorry."))
}
})
m.Get("/", func(r http.ResponseWriter) {
t, err := template.ParseFiles("public/app/index.html")
if err != nil {
panic(err.Error())
}
t.Execute(r, nil)
})
m.Get("/auth", func(r http.ResponseWriter) {
username, token := makeRandomUserToken()
user := &biz.User{
Username: username,
Token: token,
Role: biz.RoleUser,
}
checkErr(g.Db().Insert(user))
resp, err := json.Marshal(map[string]interface{}{
"username": user.Username,
"token": user.Token,
})
checkErr(err)
r.Write(resp)
})
m.Get("/api/test", func(r http.ResponseWriter) {
r.Write([]byte("CLGT"))
})
m.Run()
}
示例3: main
func main() {
m := martini.Classic()
// log before and after a request
m.Use(func(c martini.Context, log *log.Logger) {
log.Println("before a request")
c.Next()
log.Println("after a request")
})
sublime.Route(m)
store := sessions.NewCookieStore([]byte("secret_words_key_xxx"))
m.Use(sessions.Sessions("weiyan_session", store))
m.Get("/set", func(session sessions.Session) string {
session.Set("hello", "world")
return "OK"
})
m.Get("/get", func(session sessions.Session) string {
v := session.Get("hello")
if v == nil {
return ""
}
return v.(string)
})
//m.Use("/admin", auth.Basic("username", "secretpassword"))
m.Use(martini.Static("assets"))
m.Run()
}
示例4: main
func main() {
m := martini.Classic()
m.Map(SetupDB())
m.Use(render.Renderer(render.Options{
Layout: "layout",
}))
m.Use(martini.Static("assets"))
m.Get("/", func(ren render.Render, r *http.Request, db *sql.DB) {
rows, err := db.Query("SELECT * from videos")
PanicIf(err)
defer rows.Close()
videos := []Video{}
for rows.Next() {
v := Video{}
err := rows.Scan(&v.Id, &v.Title)
PanicIf(err)
videos = append(videos, v)
// fmt.Fprintf(rw, "Title: %s", title)
}
ren.HTML(200, "videos", videos)
})
m.Run()
}
示例5: main
func main() {
// Get the configuration
//
if *configFile == "" {
*configFile = "./nightcrawler.ini"
}
opts := globalconf.Options{Filename: *configFile, EnvPrefix: "NC_"}
conf, _ := globalconf.NewWithOptions(&opts)
conf.ParseAll()
// Static assets should be served by nginx in production
//
println(martini.Env)
m := martini.Classic()
if martini.Env != "production" {
m.Use(martini.Static("assets"))
}
// This is just a temp route in development mode
//
if martini.Env == "development" {
m.Get("/", UserRendererMiddleware(), func(r render.Render) {
r.HTML(200, "root", nil)
})
}
// Top level of app
// This section is everything outside of authentication
//
m.Group("/app", func(r martini.Router) {
r.Get("/sign_in", controllers.SignIn)
r.Post("/sign_in", csrf.Validate, controllers.SignIn)
r.Get("/sign_up", controllers.SignUp)
r.Post("/sign_up", csrf.Validate, controllers.CreateSignUp)
r.Get("/sign_out", controllers.SignOut)
r.NotFound(func(x render.Render) {
x.HTML(404, "404", nil)
})
},
CookieStore(*userCookieSecret, *userSessionName),
UserSessionAuth(),
CSRFMiddleware(*userSessionSecret, *userSessionKey),
UserRendererMiddleware(),
)
// Admin interface
m.Group("/app/admin", func(r martini.Router) {
r.Get("/sign_in", controllers.AdminSignIn)
r.NotFound(func(x render.Render) {
x.HTML(404, "404_admin", nil)
})
},
CookieStore("admin123", "nightcrawler_admin"),
AdminSessionAuth(),
CSRFMiddleware("admin123", "adminId"),
AdminRendererMiddleware(),
)
m.Run()
}
示例6: main
func main() {
fs, err := filestore.New("s3")
if err != nil {
log.Fatal(err)
}
port := os.Getenv("PORT")
m := martini.Classic()
m.Map(fs)
m.Use(martini.Static("../web/public"))
m.Use(render.Renderer(render.Options{
Directory: "../web/templates",
Layout: "layout",
}))
m.Get("/", func(fs *filestore.S3, r render.Render) {
files, err := fs.List("builds")
sort.Sort(ByTime(files))
if err != nil {
log.Fatal(err)
}
r.HTML(200, "home", files)
})
log.Printf("starting server at %s", port)
err = http.ListenAndServe(":"+port, m)
if err != nil {
log.Fatal(err)
}
}
示例7: Start
func (s *Server) Start() error {
m := martini.Classic()
m.Use(martini.Static("public"))
m.Get("/jq", handleJQ)
return http.ListenAndServe(":"+s.Port, m)
}
示例8: martiniInit
func martiniInit() {
m := martini.New()
r := martini.NewRouter()
m.Map(db)
//m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
//m.Use(oauth2.Github(&oauth2.Options{
// ClientId: "64a641523f31dd5bfe4b",
// ClientSecret: "4fe3fbbca262835c424ca6a80aec6c6cb4228037",
// RedirectURL: "http://localhost:2714/github_callback",
// Scopes: []string{"user:email"},
//}))
//r.Get("/", index)
r.Get("/comments/:forum/:page", GetAllCommentsResource)
r.Post("/comment", PostCommentHandler)
//r.Get("/github_redirect", RedirectUrl)
//r.Get("/github_callback", func(request *http.Request) string {
// request.ParseForm()
// return fmt.Sprintf("%+v", request)
//})
m.Use(martini.Static("ui"))
m.Action(r.Handle)
fmt.Println("Running on port " + viper.GetString("port"))
sio.Handle("/", m)
//http.Handle("/", m)
//http.ListenAndServe(":"+viper.GetString("port"), m)
}
示例9: main
func main() {
// fmt.Println("Hello World!")
rand.Seed(time.Now().UTC().UnixNano())
/* database := models.GetDB()
database.Drop(tiedotmartini2.BAND_COL)
database.Drop(tiedotmartini2.LOCATION_COL)
database.Drop(tiedotmartini2.GENRE_COL)
database.Create(tiedotmartini2.BAND_COL, 1)
database.Create(tiedotmartini2.LOCATION_COL, 1)
database.Create(tiedotmartini2.GENRE_COL, 1)
col := database.Use(tiedotmartini2.BAND_COL)
col.Index([]string{"albums", "genre_id"})
database.Close()
*/
m := martini.Classic()
m.Get("/", controllers.HomeIndex)
m.Get("/home/index", controllers.HomeIndex)
m.Get("/band/add", controllers.BandAdd)
m.Post("/band/verify", controllers.BandVerify)
m.Get("/album/index/:id", controllers.AlbumIndex)
m.Get("/album/add/:id", controllers.AlbumAdd)
m.Post("/album/verify/:id", controllers.AlbumVerify)
m.Get("/home/genrelist", controllers.HomeGenreList)
m.Get("/home/bygenre/:id", controllers.HomeByGenre)
m.Use(martini.Static("assets"))
m.Run()
}
示例10: main
func main() {
m := martini.Classic()
m.Use(martini.Static("static"))
m.Use(render.Renderer())
m.Get("/", func(r render.Render) {
r.HTML(201, "index", nil)
})
// Authenticate user
m.Post("/auth", binding.Bind(User{}), func(user User, r render.Render) {
if user.UserId == ValidUser && user.Password == ValidPass {
// Create JWT token
token := jwt.New(jwt.GetSigningMethod("HS256"))
token.Claims["userid"] = user.UserId
// Expire in 5 mins
token.Claims["exp"] = time.Now().Add(time.Minute * 5).Unix()
tokenString, err := token.SignedString([]byte(SecretKey))
if err != nil {
r.HTML(201, "error", nil)
return
}
data := map[string]string{
"token": tokenString,
}
r.HTML(201, "success", data)
} else {
r.HTML(201, "error", nil)
}
})
// Check Key is ok
m.Get("/debug/:token", func(params martini.Params, r render.Render) string {
token, err := jwt.Parse(params["token"], func(token *jwt.Token) ([]byte, error) {
return []byte(SecretKey), nil
})
if err == nil && token.Valid {
return "User id: " + token.Claims["userid"].(string)
} else {
return "Invalid"
}
})
// Only accesible if authenticated
m.Post("/secret", func() {
})
m.Run()
}
示例11: main
func main() {
m := martini.Classic()
m.Get("/", func() string {
return "Hello world!"
})
m.Use(auth.Basic("username", "secretpassword"))
m.Use(martini.Static("./"))
m.Run()
}
示例12: newMartini
func newMartini() *martini.ClassicMartini {
r := martini.NewRouter()
m := martini.New()
m.Use(middleware.Logger())
m.Use(martini.Recovery())
m.Use(martini.Static("public"))
m.MapTo(r, (*martini.Routes)(nil))
m.Action(r.Handle)
return &martini.ClassicMartini{m, r}
}
示例13: configuration
/**
* Martini application configuration.
*/
func configuration(app *martini.ClassicMartini) {
app.Use(martini.Static("public"))
app.Use(render.Renderer(render.Options{
Directory: "templets",
Layout: "layout",
Extensions: []string{".tmpl", ".html"},
Charset: "UTF-8",
IndentJSON: true,
}))
}
示例14: main
func main() {
m := martini.Classic()
m.Use(martini.Static("static"))
m.Get("/api/feed", FetchFeeds)
m.Post("/api/feed", AddFeed)
m.Delete("/api/feed", DeleteFeed)
m.Post("/api/feed/read", MarkUnread)
http.ListenAndServe(":"+os.Getenv("PORT"), m)
}
示例15: main
func main() {
m := martini.Classic()
m.Use(martini.Static("views"))
m.Get("/tasks", GetTasks)
m.Post("/task", CreateTask)
m.Get("/task/:id", FindTask, GetTask)
m.Put("/task/:id", FindTask, UpdateTask)
m.Run()
}